# Difference between revisions of "Integer"

(→Definition) |
(→Notes) |
||

Line 36: | Line 36: | ||

==Notes== | ==Notes== | ||

− | + | If the integer needed will always be positive, unsigned integers can be useful. By making an integer unsigned, the variable will use the previously negative values as more positive values, effectively increasing how high the positive value can be. The total number of values will remain the same. | |

+ | A way to think of this is if there was a variable that could only count from -10 to 10. If told to be unsigned, it will now be able to count from 0 to 20. | ||

==External Links== | ==External Links== |

## Latest revision as of 16:09, 8 August 2019

## Contents

# Definition

Integers are a data type that are 32 bits in size. They cannot hold any decimal places, are whole numbers, and can be positive or negative.

# Relevance

Integers are commonly used for counting in programming such as for indexes in collections or keeping track of iterations in loops.

# Explanation

Integers can be used for arithmetic expressions such as addition, multiplication, division in programming. However, if the result is being saved into an integer, the result should be a whole number. Otherwise, the result will truncate (round to a whole number) which may create undesired results.

```
int a = 5;
int b = 2;
int c = 2.5; // not allowed as integers cannot hold decimal places.
int result1, result2, result3; // You can also declare several variables of the same type this way.
result1 = a + b; // result1 = 7
result2 = a * b; // result2 = 10
result3 = a / b; // result3 = 2 because the decimal places will not be saved.
int counter;
for(counter = 0; counter < 10; counter++)
{
Console.WriteLine(counter); // This will display the numbers 0 to 9
}
```

# Resources

## See also

## Notes

If the integer needed will always be positive, unsigned integers can be useful. By making an integer unsigned, the variable will use the previously negative values as more positive values, effectively increasing how high the positive value can be. The total number of values will remain the same. A way to think of this is if there was a variable that could only count from -10 to 10. If told to be unsigned, it will now be able to count from 0 to 20.