Lesson #8. For Loop
- For loop is a loop with counter
- After each iteration counter increments by 1
Output: numbers from '1' up to '10' (1 2 3 4 5 6 7 8 9 10)
✎
1. {0.3 points}[task-01-for.pas]
Two integers K
and N
(N
> 0) are given. Output N
times the number K
.
Example:
'enter the number to output, please: K =' 4 'enter how many times to output: N =' 3 >>> 4 4 4
2. {0.4 points}[task-02-for.pas]
Two integers A
and B
are given (A < B). Output integers between A
and B
(including A and B themselves) in ascending order and also output the number (quantity) of these numbers.
Example:
'enter two numbers, please: A= , B =' 0 5 >>> 0 1 2 3 4 5 quantity = 6 ---- 'enter two numbers, please: A= , B =' 2 7 >>> 2 3 4 5 6 7 quantity = 6
3. {0.4 points}[task-03-for.pas]
Calculate a value of the function z (x) = x3
for all x
varying on the interval [-3, 3]
Example:
'the rezult of z(x) : ' -27 -8 -1 0 2 8 27
4. {0.4 points}[task-04-for.pas]
Calculate a value of the function z (x) = sin (x)
for all x
varying on the interval [1, 5]
Example:
'the rezult of z(x) : ' 0.84 0.91 0.14 -0.76 -0.96
5. {0.4 points}[task-05-for.pas]
Calculate a value of the function z (x,y) = x + y
for all x
varying on the interval [1, 5] and for all y
varying on the interval [0, 4]
Example:
'the rezult of z(x) : ' >>> 1 + 0 = 1 2 + 1 = 3 3 + 2 = 5 4 + 3 = 7 5 + 4 = 9
6. {0.4 points}[task-06-for.pas]
Calculate a value of the function z (x,y) = x * y
for all x
varying on the interval [2, 6] and for all y
varying on the interval [3, 7]
Example:
'the rezult of z(x) : ' >>> 2 * 3 = 6 3 * 4 = 12 4 * 5 = 20 5 * 6 = 30 6 * 7 = 42
For … Downto Loop
After each iteration counter decrements by 1:Output: numbers from '10' downto '1' (10 9 8 7 6 5 4 3 2 1)
✎
7. {0.4 points}[task-07-for.pas]
Two integers A
and B
are given (A < B). Output integers between A
and B
(including A and B themselves) in discending order.
Example:
'enter two numbers, please: A=, B=' 0 5 >>> result: 5 4 3 2 1 0 ----- 'enter two numbers, please: A=, B=' 2 7 >>> result: 7 6 5 4 3 2
How to use arbitrary step in "for" & "loop"
Task: Output all 2-digit odd numbers from 11 till 21.
11 13 15 17 19 21
Solution 1. With "loop"
Solution 2. With "for"
✎
8. {0.4 points}[task-08-for.pas]
Output the sequence 3 5 7 9 ... 21
(from 3 to 21 with a step = 2). Make it twice: with loop
and with for
loop.
begin println('the result with a loop'); var ...; loop ...; ... println('the result with a FOR loop'); for var ... ... end.
9. {0.4 points}[task-09-for.pas]
Output the sequence 20 18 16 ... 2
(from 20 downto 2 with a step = 2). Make it twice: with loop
and with for
loop.
10. {0.4 points}[task-10-for.pas]
Output the sequence 15 12 9 6 3 0
(from 15 downto 0 with a step = 3). Make it twice: with loop
and with for
loop.
11. {0.4 points}[task-11-for.pas]
Output the sequence 5 10 15 20 ... 100
(from 5 to 100 with a step = 5). Make it twice: with loop
and with for
loop.
12. [task-12-for.pas]
Calculate a value of the function z (x) = x3
for all x
varying on the interval [1, 7] with a step = 2. Make it twice: with loop
and with for
loop.
Example:
1*1*1 = 1 3*3*3 = 27 5*5*5 = 125 7*7*7 = 343
Solution with for loop:
13. {0.7 points}[task-13-for.pas]
Calculate a value of the function z (x) = x2
for all x
varying on the interval [3, 12] with a step = 3. Make it twice: with loop
and with for
loop.
Example:
3*3 = 9 6*6 = 36 9*9 = 81 12*12 = 144
14. {0.7 points}[task-14-for.pas]
Calculate a value of the function z (x) = √x
for all x
varying on the interval [5, 25] with a step = 5. Make it twice: with loop
and with for
loop.
Example:
√5 = 2.23606797749979 √10 = 3.16227766016838 √15 = 3.87298334620742 √20 = 4.47213595499958 √25 = 5
Solution with for loop:
15. {0.7 points}[task-15-for.pas]
Calculate a value of the function z (x) = 2x-2
for all x
varying on the interval [5, 20] with a step = 3. Make it twice: with loop
and with for
loop.
Example:
2*5-2 = 8 2*8-2 = 14 2*11-2 = 20 2*14-2 = 26 2*17-2 = 32 2*20-2 = 38
How to use real step in "for" & "loop" cycles
Task: Output sequence:
1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
Solution 1. With "loop"
Solution 2. With "for"
16. {0.4 points}[task-16-for.pas]
Output the sequence 0.1 0.3 0.5 0.7 0.9 1.1
. Make it twice: with loop
and with for
loop
17. {0.4 points}[task-17-for.pas]
Output the sequence 0.0 0.5 1.0 1.5 2.0 2.5
. Make it twice: with loop
and with for
loop
18. {0.4 points}[task-18-for.pas]
A real number is given— the price of 1 kg of candy. The program has to output the prices of 1, 2, ..., 10 kg of candy. Use for
loop.
Note. Use friendly variables' names (it's better to use writelnFormat
statement).
Example:
'enter a price of 1 kg of candy, please: ' 150.5
>>>
'the cost of 1 kg = ' 150.5
'the cost of 2 kg = ' 301
'the cost of 3 kg = ' 451.5
...
'the cost of 10 kg = ' 1505
19. {0.4 points}[task-19-for.pas]
A real number — the price of 1 kg of candy is given. Print the prices of 1.2, 1.4, 1.6 1.8, 2 kg of candy.
Example:
'enter a price of 1 kg of candy, please: ' 100.2
>>>
'the cost of 1.2 kg = ' 120.24
'the cost of 1.4 kg = ' 140.28
...
'the cost of 2 kg = ' 200.4
20. {0.4 points}[task-20-for.pas]
Two integers A
and B
are given. Print the squares of all integers between A
and B
in ascending order and including these numbers themselves.
Example:
'enter two integers, please: A = , B = ' 1 -2 >>> 4, 1, 0, 1 --- 'enter two integers, please: A = , B = ' 2 3 >>> 4, 9 --- 'enter two integers, please: A = , B = ' 2 2 >>> 4
21. {0.4 points}[task-21-for.pas]
Two integers A
and B
are given. Print the the square roots of all integers between A
and B
in ascending order and including these numbers themselves.
Example:
'enter two integers, please: A = , B = ' 1 5
>>> √1 = 1 √2 = 1.4142135623731 √3 = 1.73205080756888 √4 = 2 √5 = 2.23606797749979
Extra tasks
1. {0.5 points}[task-1-extra-for.pas]
Positive numbers A
and B
(A >= B) are given. The segment of length A
contains the maximum possible number of segments of length B
(without overlays). Without using the multiplication (*
) and division(/
, div
) operations, find the length of the unoccupied part of segment A
. To exit the loop you have to use exit
statement (if ... then exit;
)
Example:
A = 10, B = 4 >>> 2 A = 12, B = 4 >>> 0
Note: Find out how the program behaves if the input numbers A, B are not positive (check both negative and zero values). To explicitly specify that these values are not valid, add operators to the program:
Assert((A > 0) and (B > 0)); Assert(A >= B);
They must be placed after the data is entered, but before the calculations begin. Check how Assert
works on incorrect input data.
Instruction. From now on, all your programs must contain an Assert input check.
2. {0.5 points}[task-2-extra-for.pas]
Positive integers N
and K
are given. Using only the addition (+
) and subtraction (-
) operations, find the quotient of the division of N
by K
, as well as the remainder of this division.
Example:
N = 12, K = 4 >>> Q = 3, R = 0 N = 27, K = 5 >>> Q = 5, R = 2