The Type int
The type int represents whole numbers.
A variable of type int cannot be assigned a value that is not a whole number.
Both types num and int are commonly needed in many programs. A count of anything (chairs, apples, people) is always of type int. Knowing that a value is of a particular type can be immensely helpful, especially when writing computer programs.
A getter is a member that produces a value.
The getter .round converts a value of type num to type int.
In the other direction, a value of type int is automatically converted to type num.
Note: A type of a variable cannot be changed.
This program has an error: a value of type num cannot be assigned to a variable of type int.
Correct the mistake and run the program.
A value of type num can be turned into an int in various ways:
- The getter .round rounds the number.
- The getter .int keeps only the whole part of the number.
- The getter .floor returns the first lesser or equal integer.
- The getter .ceil returns the first greater or equal integer.
In this example, getters .round and .int are applied on several values.
This program contains an error.
A literal expression containing a dot (.) symbol is considered to be of type num.
To correct the error, write the literal expression without a decimal dot.
Every literal expression is a primary expression.
Most loop control variables are of type int, since a for loop commonly counts the number of iterations. A count is always a whole number.
A for loop can automatically infer the type of control variable. In this example, the variable i is of type int.
Type int has a getter .roman.
Type num does not have a getter .roman because Roman system has no way of writing non-whole numbers.
A value of type int can be aligned on printout by using the getter function .pad.
The getter function .pad prepends the integer with blanks on the left. This causes the printout of the integer to be aligned to the right.
The argument of the getter function .pad specifies a minimum number of places that the integer occupies.
Getters can be applied directly on variables, but not on most other expressions.
To apply a getter on an expression, put the expression inside parentheses.
This program contains an error.
The result of the operator of common division (/) is always of type num.
To correct the error, use the getter .int.
#int j = (x/3).int;
The operator // performs the integer division.
The result of operator // is always an integer.
The modulo operator %% returns the remainder of the integer division operation.
The expression 9 %% 4 evaluates to value 1 because 9 divided by 4 equals 2 with reminder 1.
The expression 3 %% 4 evaluates to value 3 because 3 divided by 4 equals 0 with reminder 3.
When the left operand of operator %% is divisible by the right operand, the result is 0. For example, 15 %% 5 evaluates to 0, 21 %% 7 evaluates to 0 and 6 %% 6 evaluates to 0.
The result of operator %% is always an integer.
This program uses operators // and %% to recompute length of a movie given in minutes into hours and remaining minutes.
This program uses modulo operator (%%) to compute position and color of a moving disc.
You should analyze in detail the role of modulo operator in this program in order to get a better understanding.
Tip: analyze the behaviour of numbers in the upper left corner of the screen while the program is running; observe how those numbers affect the color and position of the disc.
* * *
-- Static Type Systems --
All type errors are detected before a program is started. No type errors can happen while a program is running.
This is accomplished by a static type system. An advantage of static type systems is that type errors are detected before the program is run.
Static type systems also enable faster execution of programs.
Static type systems are especially suitable for beginners, as they reduce the number of errors in programs.
Other popular languages that also use a static type system include: C++, C#, Java.