CodeGym /Courses /C# SELF /Integers: int type, operations with int type

Integers: int type, operations with int type

C# SELF
Level 2, Lesson 1
Available

1. Digging deeper into the int type

If you want to store whole numbers in variables, you need to use the int type. int is short for Integer (whole in English), which kinda hints that this type lets you store whole numbers.

Variables of type int can store whole numbers in the range from -2,147,483,648 to 2,147,483,647 (or, simply put, from -2 billion to +2 billion).

Fun fact

These weird, non-round values are because of how computer memory works. In C#, the int type gets 4 bytes of memory. Each byte is made up of 8 bits. Each bit can only be 0 or 1. An int variable has 32 bits and can take 4,294,967,296 different values.

Half of that range is for negative numbers, the other half for positive. That's how you get from -2,147,483,648 to 2,147,483,647.

2. Creating an int variable

The int type is for storing whole numbers. To create a variable in your code that can store whole numbers, use a command like this:

int name;
Declaring an int variable

Where name is the variable name. Examples:

Command Description
int x;
Creates an integer variable x
int count;
Creates an integer variable count
int currentYear;
Creates an integer variable currentYear

Letter case matters: the commands int color and int Color will declare two different variables.

But Int Color and INT COLOR won't even compile. int is a special word for the integer type, and it's always written in lowercase.

3. Short way to declare variables

If you need to create a bunch of variables of the same type in one place, you can use a shorter way:


int name1, name2, name3;
        
Declaring int variables

Examples:

Commands Short version of the same commands
int x;
int y;
int z;
int x, y, z;
int count;
int totalCount;
int count, totalCount;
int day;
int month;
int year;
int day, month, year;

4. Assigning values

To put a value into an int variable, use this command:


name = value;
        
Assigning a value to a variable

The value can be any integer expression. Examples:

Command Note
int a;
a = 5;
int b;
b = 2*1000*1000*1000;
The variable will have the value "2 billion"
int c;
c = -10000000;
The variable will have the value "minus 10 million"
int d;
d = 3000000000;
Won't compile because 3,000,000,000 is bigger than the max int value 2,147,483,647

5. Short way to declare and initialize a variable

You can declare (create) a variable and assign it a value in one line. That's how it's usually done, since you usually declare a variable when you need to store some value.

The command looks like this:


int name = value;
        
Short way to declare and initialize a variable

Examples:

Command Note
int a = 5;
int b = 2*1000*1000*1000;
The variable will have the value "2 billion"
int c = -10000000;
The variable will have the value "minus 10 million"
int d = 3000000000;
Won't compile because 3,000,000,000 is bigger than the max int value: 2,147,483,647

You can also declare several variables in one line. The command will look like this:


int name1 = value1, name2 = value2;
Short way to declare and initialize several variables

Examples:

Command Note
int a = 5, b = 10, c = a + b;
a is 5, b is 10, c is 15

6. Calculating integer expressions

On the right side of the assignment operator (the equals sign), you can have any expression—a combo of numbers, variables, and the +, -, *, / signs.

You can also use parentheses ( ). In C#, just like in math, stuff inside parentheses gets calculated first, then outside. Multiplication and division have the same priority, and it's higher than addition and subtraction.

Examples:

Command Note
int a = (2 + 2) * 2;
The variable will have the value 8
int b = (6 - 3) / (9 - 6);
The variable will have the value 1
int c = (-2) * (-3);
The variable will have the value 6
int d = 3 / 0;
This command will cause an error and the program will crash.

Variables can also be used in expressions:

int a = 1;			// Variable a will have value 1
int b = 2;			// Variable b will have value 2
int c = a * b + 2;	// Variable c will have value 4

And yeah, the same variable can be on both sides of the assignment operator:

int x = 5;		// Variable x will have value 5
x = x + 1;		// Variable x will have value 6
x = x + 1;		// Variable x will have value 7
x = x + 1;		// Variable x will have value 8
x = x + 1;		// Variable x will have value 9
x = x + 1;		// Variable x will have value 10

The thing is, in C#, the = symbol isn't equality, it's a command that puts the value of the expression on the right into the variable on the left of the = sign.

7. Integer division

In C#, when you divide a whole number by a whole number, you always get a whole number. The remainder gets thrown away. Or you could say, the fractional part is dropped.

Examples:

Command Division result Note
int a = 5 / 2;
2.5 Variable a will have value 2
int b = 20 / 3;
6.6667 Variable b will have value 6
int c = -6 / 5;
-1.2 Variable c will have value -1
int d = 1 / 2;
0.5 Variable d will have value 0

8. Remainder of integer division

Besides addition, subtraction, multiplication, and division, C# has an operator for the remainder of division. It's the percent sign – %. This is the actual remainder of dividing one whole number by another, not the fractional part.

Examples:

Command Division result Note
int a = 5 % 2;
2 and 1 left over Variable a will have value 1
int b = 20 % 4;
5 and 0 left over Variable b will have value 0
int c = 9 % 5;
1 and 4 left over Variable c will have value 4
int d = 1 % 2;
0 and 1 left over Variable d will have value 1

This is a super useful operator, and you'll use it a lot. For example, to check if a number is even or not, just divide it by 2 and compare the remainder to zero. If the remainder is zero, the number is even; if it's one, it's odd.

The check looks like this:


            (a % 2) == 0

Where, as you probably guessed, a % 2 gets the remainder when dividing by 2 (so 0 or 1), and == is used to compare to zero.

9. Increment and decrement

In programming, you often need to increase or decrease a variable by one. For this, C# has special commands:

The increment operator (add one) looks like this:


            a++;
Increase by one

This command does the same thing as a = a + 1; – it increases variable a by one.

The decrement operator (subtract one) looks like this:


            a--;
Decrease by one

This command does the same thing as a = a - 1; – it decreases variable a by one.

Example 1

int x = 5;		// Variable x will have value 5
x++;			// Variable x will have value 6
x++;			// Variable x will have value 7
x++;			// Variable x will have value 8
x++;			// Variable x will have value 9
x++;			// Variable x will have value 10

Example 2

int x = 5;		// Variable x will have value 5
x--;			// Variable x will have value 4
x--;			// Variable x will have value 3
x--;			// Variable x will have value 2
x--;			// Variable x will have value 1
x--;			// Variable x will have value 0
x--;			// Variable x will have value -1
2
Task
C# SELF, level 2, lesson 1
Locked
Back to the Future
Back to the Future
2
Task
C# SELF, level 2, lesson 1
Locked
Back to the Future 2
Back to the Future 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION