# Lesson #6. Loops. Simple tasks

## Theory

Lection # 6 in pdf format

• Loops are used to repeat actions
• The loop consists of: a loop header and a loop body
• One loop iteration is one repetition of the loop body
• The simpliest type of loops is loop.
Syntax:

loop n do
some operator;


### For loop

• For loop is a loop with a counter
• After each iteration counter increments by 1

### While and Repeat loop

Syntax of While loop:

while condition do
statements


Syntax of Repeat loop:

repeat
statements
until condition


### Loops

Lab 1:

To do: Print digit 1 ten times.

Expected output:

1 1 1 1 1 1 1 1 1 1


✍ Algorithm:

begin loop 10 do print(1); end.

Note: Since this moment you should implement protection against invalid input with Assert function.
{0.3 points} Task 1, loop:
To do: An integer A (A > 0) is given. Output the word «Hello» A times. You need to put commas between the words.

Note: You should check if the number is positive:

assert(a>0,'a bad input; the variable a must be > 0');

Expected output:

'Please enter how many times: A ='
>>> 3
Hello, Hello, Hello


[Program name: task-01-loop.pas]

{0.3 points} Task 2, loop:
To do: Two integers K and N (N > 0) are given. The number K should be output N times (loop must be used)

Note: You should check if input numbers are positive (assert()).

Expected output:

'enter the number to output:  K= '
>>> 4
'enter how many times to output: N= '
>>> 3
4 4 4


[Program name: task-02-loop.pas]

Lab 2:

To do: Print numbers from 1 to 10.

Expected output:

1 2 3 4 5 6 7 8 9 10


✍ Algorithm:

{0.4 points} Task 3, loop:
To do: An integer A (A > 0) is given. Output integers between 0 and A (including the value of A) in ascending order and output the number (quantity) of these numbers.

Expected output:

'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


[Program name: task-03-loop.pas]

Lab 3:

To do: Print numbers from 10 downto 1.

Expected output:

10 9 8 7 6 5 4 3 2 1


✍ Algorithm:

{0.4 points} Task 4, loop:
To do: An integer A (A > 0) is given. Output integers between A and 0 (including the value of A) in discending order.

Expected output:

'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


[Program name: task-04-loop.pas]

Lab 4:

To do: Print a power of 2 starting at 0; eight powers (1 2 4 8 … 128).

Expected output:

1 2 4 8 16 32 64 128


✍ Algorithm:

{0.5 points} Task 5, loop:

To do: An integer A (A > 0) is given. Print 3^A (3 in a power of A). You should use multiplication (*). It is forbidden to use standard functions.

Note: You should use the writelnFormat function:

WritelnFormat('3 in the power of {0} = {1}', a, ?);

Expected output:

'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


[Program name: task-05-loop.pas]

### For loops

Lab 5:

To do: Print a sequence of numbers from 1 to 10.

Expected output:

1 2 3 4 5 6 7 8 9 10


✍ Algorithm:

{0.3 points} Task 1, for loop:

To do: Two integers K and N (N > 0) are given. The number K must be output N times.

Note: To check if N > 0 you should use assert function:

assert(n > 0, 'bad input, n must be > 0');

Expected output:

'enter the number to output, please: K ='
>>> 4
'enter how many times to output: N ='
>>> 3
4 4 4


[Program name: task-01-for.pas]

{0.4 points} Task 2, for loop:

To do: Two integers A and B are given (A < B). Output integers between A and B (including the values of A and B themselves) in ascending order and output the number (quantity) of these numbers. To check if A < B you should use assert function.

Expected output:

'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


[Program name: task-02-for.pas]

Lab:

To do: Output the numbers from 10 downto 1 (10 9 8 7 6 5 4 3 2 1).

✍ Algorithm:

 begin for var i:=10 downto 1 do begin print(i) end; end.

{0.4 points} Task 3, for loop:

To do: Two integers A and B are given (A > B). Output integers between values of A and B (including A and B themselves) in discending order. To check if A > B you should use assert function.

Expected output:

'enter two numbers, please: A= , B ='
>>> 5  >>> -2
5 4 3 2 1 0 -1 -2


[Program name: task-03-for.pas]

### While and Repeat loops

Lab 6:

To do: Print a sequence of numbers: 0 1 2 3 4.

Expected outpute:

0 1 2 3 4


✍ Algorithm:

{0.2 points} Task 1, while & repeat loop:

To do: Output the sequence 15 16 17 18 19 20 ... 30 (from 15 to 30). Make it twice: with while loop and with repeat loop.

Note: you should use different variables for loops counters.

Expected output:

results with while loop:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
results with repeat loop:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


[Program name: task-01-while-repeat.pas]

{0.2 points} Task 2, while & repeat loop:

To do: Output the sequence of integers: 3 5 7 9 ... 21 (from 3 to 21 with a step = 2). Make it twice: with while loop and with repeat loop.

[Program name: task-02-while-repeat.pas]

{0.2 points} Task 3, while & repeat loop:

To do: Output the sequence of integers: 20 18 16 ... 2 (from 20 downto 2 with a step = 2). Make it twice: with while loop and with repeat loop.

[Program name: task-03-while-repeat.pas]

{0.2 points} Task 4, while & repeat loop:

To do: Output the sequence of integers: 15 12 9 6 3 0 (from 15 downto 0 with a step = 3). Make it twice: with while loop and with repeat loop.

[Program name: task-04-while-repeat.pas]

{0.2 points} Task 5, while & repeat loop:

To do: Output the sequence of real numbers: 0.1   0.3  0.5   0.7  0.9   1.1. Make it twice: with while loop and with repeat loop.

[Program name: task-05-while-repeat.pas]

{0.2 points} Task 6, while & repeat loop:

To do: Output the sequence of real numbers: 0.0   0.5  1.0   1.5  2.0   2.5. Make it twice: with while loop and repeat loop.

[Program name: task-06-while-repeat.pas]

{0.3 points} Task 7, while & repeat loop:

To do: Two integers A and B are given (A < B). Output integers between A and B (including A and B themselves) in ascending order and output the number (quantity) of these numbers. Make it twice: with while loop and repeat loop.

Expected output:

'enter two numbers, please: A= , B ='
>>> -1  >>> 5
results with a while loop:
-1 0 1 2 3 4 5  quantity = 7
results with a repeat loop:
-1 0 1 2 3 4 5  quantity = 7


[Program name: task-07-while-repeat.pas]

{0.5 points} * Task 8, while loop:

To do: Positive integers N and K are given. Using only the operations of addition and subtraction, find the quotient of dividing N by K, as well as the remainder of this division.

Note 1. Use meaningfull variable names for quotient and remainder. For example: quotient is quotient, remainder is remainder. Possibly: quot / rem.

Note 2. Don't forget to implement protection against invalid input (Assert function).

Expected output:

'N = '
>>> 12
'K = '
>>> 4
quotient = 3, remainder = 0

'N = '
>>> 27
'K = '
>>> 5
quotient = 5, remainder = 2


[Program name: Extask-08-while.pas]

{0.5 points} * Task 9, while loop:

To do: 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.

Note 1. To specify that input values are not valid, use Assert functions. They must be placed after the data is entered, but before the calculations begin.

Assert((A > 0) and (B > 0));
Assert(A >= B);


Expected output:

A =
>>> 10
B =
>>> 4
result: 2

A =
>>> 12
B =
>>> 4
result: 0


[Program name: Extask-09-while.pas]