You are on page 1of 91

COMP10060

Semester II, 2022-2023

Computer Science for Engineers, 1


( Language: C )

Cormac Murray (TA)

Lab 4
1
Loops
Going round and round!

2
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update)
{
code statements;
}

while( condition )
{
code statements;
}

do
{
code statements;
}
while( condition );

3
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update) for loop
{
code statements;
}

while( condition )
{
code statements;
}

do
{
code statements;
}
while( condition );

4
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update) while loop
{
code statements;
}

while( condition )
{
code statements;
}

do
{
code statements;
}
while( condition );

5
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update) do ... while loop
{
code statements;
}

while( condition )
{
code statements;
}

do
{
code statements;
}
while( condition );

6
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update)
{
code statements;
}
Never put a semicolon ;
while( condition ) after condition braces ()
{ of for and while loops.
code statements;
}

do
{
code statements;
}
while( condition );

7
Loops are used to execute a block of code repeatedly
3 Kinds of Loops
for( intialisation; condition; update)
{
code statements;
}
Always put a semicolon ;
while( condition ) after condition braces ()
{ of do ... while loops.
code statements;
}

do
{
code statements;
}
while( condition );

8
for Loop
(for a specified number of repeats)

9
10
3 components, ...

11
... separated by
semicolons

12
initialises the loop.

13
Sets the condition
and sets the bound.

14
Updates the loop.

15
Updates the loop, before
the condition is checked.

16
Loop body

17
18
Another for loop example
Task: Sum all digits from 0 to less than 10

19
20
sum has been declared outside loop body,
and initialised to a value (0 in this example).

21
Because sum was declared outside loop body, ...

22
... it is understood both inside the loop, ...

23
... as well as outside it.

24
25
while Loop
(ideal for unspecified number of repeats)

26
but can take a specified number of repeats ...

while loop example


Task: Sum all digits from 0 to less than 10
(as in example just shown using the for loop)

27
28
initialises the loop.

29
Sets the condition
and sets the bound.

30
Updates the loop.

31
Don’t forget to
update the loop!!

32
Don’t forget to
update the loop!!

Could give rise to an


infinite loop otherwise.

33
Same result as in previous for loop

34
while Loops are Unbounded...
(we don’t always know when they are
going to terminate)

35
while Loop example
(taking an int input)

36
37
Variable answer declared, of type int.

38
Asks for, and takes in, your
answer so we can get started.

39
A chance to give your answer again,
within the loop body.

40
sets condition.

41
... but we don’t know when you are not
going to type 99 (in this case) to end loop, ...

42
... that’s why we say that the loop is
unbounded.

43
Anything other than 99 halts the loop.

44
Same while Loop example
(but this time taking a char input)

45
char with scanf() need its space!

46
47
Variable answer declared, of type char.

48
Uppercase or lower case char

49
Note the blank space before the %c – it is needed to
fix a problem with scanf () when taking multiple char
readings into the same variable (answer, in this case).
50
There is no problem with scanf () and %c when
taking first char reading for variable answer, …

51
There is no problem with scanf () and %c when
taking first char reading for variable answer, …

... but a blank space before the %c will be


needed for taking any subsequent scanf() with
%c char readings for char variable answer, ...

52
... but a blank space before the %c will be
needed for taking any subsequent scanf() with
%c char readings for char variable answer, ...

... whether readings are inside or outside loop. 53


y or Y starts the loop.

54
y or Y continues the loop.

55
Anything other than y or Y halts the loop.

56
do ... while Loops
(they go round at least once)

57
58
Will have already gone round once, ...

59
... then will take in your answer.

60
As before, note the space before char.

61
Will have gone round once.

62
y or Y continues the loop.

63
Anything other than y or Y halts the loop.

64
Nested Loops
(loops within loops)

65
66
Outer loop

67
Outer loop

68
Inner (nested) loop

69
Inner (nested) loop

70
Outer loop
can be thought of as the parent
of its next inner loop.

71
Outer loop starts,
goes round just once, then hands
control to its next inner loop.

72
The innermost loop always finishes
completely before handing back
control to its parent (next outer) loop.

73
i is the counter for the outer loop.

74
j is the counter for the inner loop.

75
The outer loop prints I.

76
The inner loop prints V.

77
Note:
The iteration cycle of any loop can be
modified by code inside the loop body,
even before the loop’s limit

78
79
i count Outer loop
is now at 0

Outer starts,
goes just once,
hands over to inner.

80
Inner loop j counts
from 0 to 5

inner iterates fully


(if inner is the innermost),
hands back to outer.

81
i count Outer loop
is now at 1

Outer continues,
goes just once,
hands over to inner.

82
Inner loop j counts
from 0 to 5

inner iterates fully


(if inner is the innermost),
hands back to outer.

83
i count Outer loop
is now at 2

Outer continues,
goes just once,
hands over to inner.

84
Inner loop j counts
from 0 to 5

inner iterates fully


(if inner is the innermost),
hands back to outer.

85
i count outer complete
is now at 3

86
i count outer complete
is now at 3

i is no longer less than 3

87
outer complete

When outer completes


(reaches its limit), either ...

88
outer complete

... control gets handed back up


to outer’s own parent, if any, or, ...

89
outer complete

... if no parent,
all loops terminate,
and are exited.

90
Thank you!
Good Luck Programming.

91

You might also like