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;
Where name is the variable name. Examples:
| Command | Description |
|---|---|
|
Creates an integer variable x |
|
Creates an integer variable count |
|
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;
Examples:
| Commands | Short version of the same commands |
|---|---|
|
|
|
|
|
|
4. Assigning values
To put a value into an int variable, use this command:
name = value;
The value can be any integer expression. Examples:
| Command | Note |
|---|---|
|
|
|
The variable will have the value "2 billion" |
|
The variable will have the value "minus 10 million" |
|
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;
Examples:
| Command | Note |
|---|---|
|
|
|
The variable will have the value "2 billion" |
|
The variable will have the value "minus 10 million" |
|
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;
Examples:
| Command | Note |
|---|---|
|
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 |
|---|---|
|
The variable will have the value 8 |
|
The variable will have the value 1 |
|
The variable will have the value 6 |
|
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 |
|---|---|---|
|
2.5 | Variable a will have value 2 |
|
6.6667 | Variable b will have value 6 |
|
-1.2 | Variable c will have value -1 |
|
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 |
|---|---|---|
|
2 and 1 left over |
Variable a will have value 1 |
|
5 and 0 left over |
Variable b will have value 0 |
|
1 and 4 left over |
Variable c will have value 4 |
|
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++;
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--;
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
GO TO FULL VERSION