Difference between revisions of "Integer"
esse quam videri
(→Explanation) |
(→Definition) |
||
Line 2: | Line 2: | ||
=Definition= | =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. | 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. | ||
+ | |||
+ | [[File:IntegerVisual.jpg | 400px]] | ||
=Relevance= | =Relevance= |
Revision as of 17:23, 29 July 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
}