Lesson #11. While & Repeat Loops
Sintax:
*************************************************************************
Example Task: To output the sequence 0 1 2 3 4
Solution:
While | Repeat |
---|---|
*****************************************************************************************
Modelling while loop through repeat loop & repeat through while
While => Repeat:
Repeat => While :
*****************************************************************************************
✎
1. {0.2 points}[task-01-while-repeat.pas]
Output the sequence 15 16 17 18 19 20 ... 30
(from 15 to 30). Make it twice: with while loop
and with repeat loop
.
2. {0.2 points}[task-02-while-repeat.pas]
Output the sequence 3 5 7 9 ... 21
(from 3 to 21 with a step = 2). Make it twice: with while loop
and with repeat loop
.
3. {0.2 points}[task-03-while-repeat.pas]
Output the sequence 20 18 16 ... 2
(from 20 downto 2 with a step = 2). Make it twice: with while loop
and with repeat loop
.
4. {0.2 points}[task-04-while-repeat.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. Make it twice: with while loop
and with repeat loop
.
5. {0.2 points}[task-05-while-repeat.pas]
Output the sequence 0.1 0.3 0.5 0.7 0.9 1.1
. Make it twice: with while loop
and with repeat loop
.
6. {0.2 points}[task-06-while-repeat.pas]
Output the sequence 0.0 0.5 1.0 1.5 2.0 2.5
. Make it twice: with while loop
and with repeat loop
.
7. {0.3 points}[task-07-while-repeat.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. Make it twice: with while loop
and with repeat loop
.
****************************************************************************************
The Infinite Loop
while | repeat |
---|---|
var x:=1; | var x:=0; |
******************************************************************************
✎
1. {0.2 points}[task-1-infinite.pas]
Print the word 'Hello' using one of the infinite loops.
Sums and products in the while and repeat loops
Example task: Find the sum:
s = 11 + 13 + 15 + ... + 99
While | Loop |
---|---|
*******************************************************************************
✎
1.{0.3 points}[task-01-sum-while.pas]
Calculate a sum of all odd 2-digit integers (11+ 13 + 15 + 17 + 19 + 21 + ... + 99). Make the task two times - with while
and repeat
:
'the sum of all odd 2-digits =' >>> 2475
2.{0.3 points}[task-02-sum-while.pas]
Calculate a product of 2-digit even integers in the interval [10;30] (10 * 12 * 14 * ... * 28 * 30). Make the task two times - with while
and repeat
.
'the product =' >>> 667418624
3.{0.3 points}[task-03-sum-while.pas]
5 real numbers are entered. The program should output their sum only once, just before the end of the program. Make the task two times - with while
and repeat
:
entered numbers: 4.2 7.1 3.1 2.5 8.6 => sum = 25.5
4.{0.3 points}[task-04-sum-while.pas]
5 numbers are entered. The program should output their product (multiplication) only once, just before the end of the program. Make the task two times - with while
and repeat
:
entered numbers: 4 7 3 2 8 => multiplication = 1344
******************************************************
Sequences in While loop and in Nested Loops
1.{0.4 points}[task-01-nested_loops.pas]
A sequence of integers is given. The indication of completion of the sequence is entered number 0
(if 0
is entered the input of the numbers of the set is finished). The program has to print 0
in the case the sequence form a non-increasing sequence of numbers, otherwise the program has to print the number 1
.
Example:
'please, enter the sequence, print 0 if you want to stop:' 1 5 9 5 0 >>> output: 0 (non-increasing sequence) +++++++++++++++++++++++++++++++++++++++++++++++++ 'please, enter the sequence, print 0 if you want to stop:' 1 2 5 9 11 0 >>> output: 1 (increasing sequence)Solution 1:
Solution 2:
**********************************************************************************************
2.{1 point} [task-2-nested_loops.pas]
Integer K
is given (K > 0) and the set of K
sequences. The indication of completion of the sequences are numbers 0
(for every sequence). The program has to output the number of the elements in every sequence and also to output the total number of the elements in all the sequences together.
Example:
'please, enter the quantity of the sequences:' 3 'input the sequence #1:' 1 5 9 5 0 >>> output: 4 elements 'input the sequence #2:' 2 5 0 >>> output: 2 elements 'input the sequence #3:' 5 7 3 0 >>> output: 3 elements 'total quantity of elements: ' 93.
{1 point} [task-3-nested_loops.pas]
Integer K
is given (K > 0) and the set of K
none-zero numbers are inputted (the sequences). The indication of completion of the sequences are numbers 0
(for every sequence). The program has to output the number of odd elements in every sequence and also to output the total number of the elements in all the sequences together.Example:
'please, enter the quantity of the sequences:' 3 'input the sequence #1:' 1 5 9 4 0 >>> output: 3 odd elements 'input the sequence #2:' 2 5 0 >>> output: 1 odd element 'input the sequence #3:' 5 7 3 0 >>> output: 3 odd elements 'total quantity of elements: ' 9
4.{1 point}[task-4-nested_loops.pas]
Integer N
is given (N >= 1) and the set of N
none-zero numbers are inputted (the sequences). The indication of completion of the sequences are numbers 0
(for every sequence). The program has to output the elements in every sequence which are less than the number from the left in the sequence.
Example:
'please, enter the quantity of the sequences:' 3 'input the sequence #1:' 1 5 3 0 >>> output: 3 'input the sequence #2:' 2 6 0 >>> output: 0 'input the sequence #3:' 5 7 4 0 >>> output: 4
5.{1 point}[task-5-nested_loops.pas]
Integer N
is given (N >= 1) and the set of N
none-zero numbers are inputted (the sequences). The indication of completion of the sequences are numbers 0
(for every sequence). The program has to output minimal element in every sequence .
'please, enter the quantity of the sequences:' 3 'input the sequence #1:' 1 9 12 4 0 >>> output: 1 'input the sequence #2:' 12 6 15 0 >>> output: 6 'input the sequence #3:' 5 -3 8 2 0 >>> output: -3