Lesson #7. Loops
- Loops are used to repeat actions
- Loop consists of: loop header and loop body
- One iteration of loop is one repetition of loop body
Syntax:
loop n do some operator;
Example 1:
Output: digit '1' ten times (1111111111)
Example 2:
Output: numbers from '1' up to '10' (12345678910)
✎
1. {0.3 points}[task-01-loop.pas]
Integer A
(A
> 0) is given. Output the word "Hello" A
times. The commas have to be between the words.
'Please enter how many times: A =' 3
>>> Hello, Hello, Hello
2. {0.3 points}[task-02-loop.pas]
Two integers K
and N
(N
> 0) are given. Output N
times the number K
.
'enter the number to output, please: K= ' 4 'enter how many times to output: N= ' 3 >>> 4 4 4
3. {0.4 points}[task-03-loop.pas]
Integer A
(A
> 0) is given. Output integers between 0 and A
(including the number A
) in ascending order and also output the number (quantity) of these numbers.
'Enter a number where to stop: A= ' 5 >>> 0 1 2 3 4 5 quantity = 6 'Enter a number where to stop: A= ' 3 >>> 0 1 2 3 quantity = 4
Example 3:
Output: numbers from '10' downto '1' (10 9 8 7 6 5 4 3 2 1)
✎
4. {0.4 points}[task-04-loop.pas]
Integer A
(A
> 0) is given. Output integers between A and 0
(including the number A
) in discending order.
'Enter a number to begin the output:' 5 >>> result: 5 4 3 2 1 0 'Enter a number to begin the output:' 3 >>> result: 3 2 1 0
Example 4:
Output: power of 2 from '0' ; eight powers (1 2 4 8 ... 128).
✎
5. {0.5 points}[task-05-loop.pas]
Integer A
(A
> 0) is given. Output 3 A (3 in power of A). For calculating use only multiplication (*). It is forbidden to use functions. To output use the WritelnFormat()
statement.
'Enter a number - power of 3: A= ' 4 >>> 3 in the power of 4 = 81 'Enter a number - power of 3: A= ' 2 >>> 3 in the power of 2 = 9