Difference between revisions of "Integer"

esse quam videri
Jump to: navigation, search
(Explanation)
Line 18: Line 18:
 
result2 = a * b; // result2 = 10
 
result2 = a * b; // result2 = 10
 
result3 = a / b; // result3 = 2 because the decimal places will not be saved.
 
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
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
=Resources=
 
=Resources=

Revision as of 19:36, 16 July 2019

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

External Links