Getters [10/12]
Getters and Statement Lists
A getter is a member that produces a value.
Type num has a getter .int. It produces a value equal to the whole part of the given number. In other words, it returns the whole part of the number.
Type color has members .r, .g and .b. These three members are getters and setters, meaning that they can also be assigned a value.
Getters create a new value without modifying the variable.
The getter .round returns the value of a variable rounded to the nearest whole number.
This example applies getters .round and .int on several values.
The print statement is the same as println except that it doesn't advance to the next line.
The getter .str from type num returns the common decimal system representation of a number.
The text after symbols << is called a single-line comment. Comments are ignored by the computer.
The text between symbols /* and */ is called a multiline comment. A multiline comment can span multiple lines.
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.
A Tab key can be used to indent a statement.
Tidy programs have statement blocks indented by one tab.
If a for loop needs to repeat a single statement only, a statement list can be used instead of a statement block.
A statement list begins with a colon (:) symbol.
Statements of a statement list must be separated by colon (:) symbols. A semicolon (;) symbol ends a statement list.
A statement list can contain many plain statements. The last statement of a statement list can be any statement.
Plain statements are statements that must always be ended by a semicolon (;) or colon (:) symbol. A println statement is a plain statement.
A for statement is not a plain statement, because it can end with a statement block.
Here is the same program written in a disorderly manner.
Why doesn't this code make a pause on each iteration?
Answer
A semicolon (;) symbol ends a statement list. Therefore, the sleepMs statement is not part of the statement list, thus it is outside the for loop.
The code is badly indented. It looks like the sleepMs statement is a part of the loop, but it isn't.
How would you make the sleepMs statement be a part of the statement list?
Solution
for #i=1..20 : println i*i << NOT ended by semicolon : sleepMs(200);
Additional Chapter
Additional chapter Graph Drawing is available. It explains how to draw graphs of mathematical functions.
To read the additional chapter, click on the following link:
<< F2:Prev - - F4:Next >>