Читайте также: |
|
Continuing our presentation of programming fundamentals, we will discover how to make decisions in a program and how to repeatedly execute instructions. We will also learn about arrays and functions.
Making decisions
A sequence of instructions executed by a program often depends on some conditions. Checking these conditions, a program makes decisions which instructions to execute.
For making decisions mainly the if statement is used.
It has the following form (in Java the word "then" is omitted):
if ( condition ) then instruction;
where:
A very useful extension to the above instruction is the if-else statement.
if (condition) then instruction1;
else instruction2;
which works as follows: if the condition is fulfilled, the instruction1 is executed, otherwise the instruction2 is executed.
Note: in Java the word then is omitted.
Let us consider some simple examples.
c = 1; if (a < b) then c = a + b; d = c + 1; | If a = 10, b = 11 (the condition is true) the variable c gets the value of 21 and the variable d gets the value of 22 If a = 2, b = 1 (the condition is false), instruction c = a + b is not executed and the variable d gets the value of 2. |
if (a = b) then c = 1; else c = 2; d = c + 1; | If a = b (condition is true) c gets value 1, the instruction after "else" is not executed and d has value 2. If a is not equal to b (condition is false) the "else" instruction is executed (c = 2) and as result of d = c+1, d gets the value of 3. |
if (a > b) then max = a; else max = b; | Finds the maximum of two numbers: a and b, and assigns it to the variable max. |
if (a > b) then txt = "a> b"; else if (a <b) then txt = "a < b"; else txt = "a = b"; say txt; | Displays message informing whether a is greater than, less than or equal to b. |
Now we are ready to write the program implementing the algorithm for tax calculation discussed earlier.
/* Calculation of a tax */ say "Give your income:";doch = linein(); if (doch > 74048) then pod = 17048.44 + 0.4 * (doch - 74048);else if (doch > 37024) then podk = 6541.24 + 0.3 * (doch - 37024) else pod = 0.19 * doch - 493.32; say "The tax:" pod;
The instruction after the "if" or "else" may be a block consisting of a number of instructions.
Note: in Java the braces ({ and }) are used to group instructions in a block.
Indeed, often after fulfilling some condition we would like to execute many instructions, not just one.
Instructions are grouped with the help of keywords: do and end.
Iterations
Often instructions or blocks of instructions are to be executed repeatedly a given number of times or until some condition is fulfilled.
For this task control statements called iteration loops are used.
We will consider two forms of these statements:
The first of these is designed to execute instruction(s) a given number of times, each time modifying the value of the special variable called counter.
It has the following form:
do counter = init_value to fin_value [ by step ]
ins1;
ins2;
...
insN;
end
where:
Such s loop is executed in the following way:
Variables specifying counters are usually named shortly: i, j, k, l
Consider some examples. The following program displays squares of integer numbers from 1 to 100.
/* squares of integer numbers from 1 to 100 */ do i = 1 to 100 say i i*i;endTo the variable i the value of 1 is assigned. This value is less than 100, therefore the instruction contained in the loop's body (say...) is executed displaying the variable's value and - followed by the space character - its square. Reaching the end keyword the loop condition (i <= 100) is checked. As it is true, the variable i is increased by 1 and loop's body is executed again (the value of i and its square are displayed: 2 4).
This process continues until the value of i reaches 101, in which case the loop's condition (i <= 100) is false and therefore control is passed to the first instruction after the end keyword.
The next example demonstrates a common method of adding (accumulating) values. It may be applied not only to numerical values, but also to concatenate strings and in other cases.
The following program displays a string consisting of the words "hot" and "dog" repeated n times.
Here is the result of execution of this program:
hotdoghotdoghotdoghotdog
At the beginning the variable string is initialized with an empty string (""). Inside the loop's body, to the current value of the variable string, the words word1 and word2 are appended alternating, depending on the value of the variable i (and the number of the current iteration at the same time): if its value is even the value of the variable word2 is used, whereas in other case, when its value is odd - the value of the variable word1 is appended.
Note: In the Java language, the remainder operator is denoted with %.
Whether a number is odd or even is checked with the help of the remainder operator (which in REXX is denoted with "//"). The remainder of division by 2 for even numbers is equal to 0, whereas for odd numbers it has some other value. Notice, that operator precedence was used: arithmetic operator precedence (here: the remainder operator) is higher than equality operator ("=") precedence. For this reason in the expression i // 2 = 0, first the // operator is applied, and then the = operator which compares the value of the expression i // 2 with zero.
Дата добавления: 2015-11-16; просмотров: 66 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Operations on data (operators and expressions) | | | Write down and run this program on your computer |