# Lecture 10: Loops I: For

## For loop

The most common loop in PASCAL is the For loop. This loop is used to execute things a predetermined number of times in a countable way. This in contrast to loops that will run while a certain condition is true, as we will learn in the next lecture. The general structure of  the For loop is
 For variable := start_value To end_value Do       instruction;

The instruction is repeated a number of times, determined by the control parameters start_value and end_value.
Since the loop is for doing things in a countable way, the control parameters variable, start_value and end_value all have to be of any integer type. Do not forget to declare the variable (see lecture 5) Example:

 program code PROGRAM ForLoopExample;  Var i: integer; begin   For i := 1 To 4 Do      WriteLn('Ola'); end. output    Ola   Ola   Ola   Ola

This program is doing the following
1) it is attributing 1 to i
2) it is checking if i is larger than 4
3) if so: EXIT LOOP else
4) execute WriteLn('Ola');
5) add 1 to i
6) go to step 2)

## Multiple instructions

Just like with the if ... then ... else structure, we can also group instructions together with begin and end in loops:

 program code For i := 1 To 4 Do   begin     WriteLn('Ola');     WriteLn('Eu chamo-me Peter');   end; output Ola Eu chamo-me Peter Ola Eu chamo-me Peter Ola Eu chamo-me Peter Ola Eu chamo-me Peter

## Use of the loop variable

Inside the loop the variable can be used, but don't mess with it

Good code:

 program code For i := 1 To 4 Do    WriteLn(i, ' Ola'); output   1 Ola    2 Ola   3 Ola   4 Ola

Bad code:

 program code For i := 1 To 4 Do   begin     WriteLn(i, ' Ola');     i := i + 1;   end; output   1 Ola   3 Ola

The program on the right is an example of bad code. Such style of programming, although at some times it will save space and maybe executing time, makes your program unstructured and very difficult to understand for others! If you want to achieve things like in the program on the right, use other loops, like while or repeat-until, or , better, use something like in the program below.

 program code For i := 1 To 2 Do   WriteLn(2*i-1, ' Ola'); output  1 Ola  3 Ola

## Expressions

The start_value and end_value can also be variables or expressions that return a value of integer type or variables of the same type, for example:
 program code PROGRAM ForLoopExample;  Var i: integer;     j: integer; begin   j := 5;   For i := j To 2*4+1 Do      WriteLn(i, 'Ola'); end. output 5 Ola 6 Ola 7 Ola 8 Ola 9 Ola

## Nested loops

The For loops (and any other loop as well) can also be 'nested', which means that they can be put within eachother. We can create double loops, or triple loops (like in the figure below on the left) or any other level. Such structures look like nests of birds and hence the name 'nesting' of loops. Here are some examples

 program code -------------------------------------- PROGRAM NestedLoop3;  Var i, j, k: integer; begin   For i := 1 To 2 Do     For j := 1 To 2 Do       For k := 1 To 2 Do         WriteLn('i=',i,' j=', j,           ' k=',k); end. output ------------- i=1 j=1 k=1 i=1 j=1 k=2 i=1 j=2 k=1 i=1 j=2 k=2 i=2 j=1 k=1 i=2 j=1 k=2 i=2 j=2 k=1 i=2 j=2 k=2
 program code ------------------------------------------ PROGRAM NestedLoop3;  Var i, j, k: integer; begin   For i := 1 To 2 Do     For j := 1 To 2 Do       begin         For k := 1 To 2 Do           WriteLn('i=',i,' j=', j,              ' k=',k);         For k := 1 To 2 Do           WriteLn('i=',i,' j=', j,              ' k=',k);       end; end. output ------------- i=1 j=1 k=1 i=1 j=1 k=2 i=1 j=1 k=1 i=1 j=1 k=2 i=1 j=2 k=1 i=1 j=2 k=2 i=1 j=2 k=1 i=1 j=2 k=2 i=2 j=1 k=1 i=2 j=1 k=2 i=2 j=1 k=1 i=2 j=1 k=2 i=2 j=2 k=1 i=2 j=2 k=2  i=2 j=2 k=1 i=2 j=2 k=2

## Down-counting loops

Instead of a loop with a variable that is counting up, we can also make a loop with a variable that is counting down. This we do by substituing the word 'To' in the structure with the word 'DownTo'. For example:

 program code For i := 4 DownTo 1 Do    WriteLn(i, ' Ola'); output    4 Ola    3 Ola   2 Ola   1 Ola

This program is doing the following
1) assign 4 to i
2) check if i is smaller than 1
3) if so: EXIT loop,
if not
4) execute WriteLn('Ola');
5) subtract 1 from i
6) go to step 2)

## Quick test:

To test your knowledge of what you have learned in this lesson, click here for an on-line test.

Peter Stallinga. Universidade do Algarve, 9 março 2002