Lecture 4 - Iteration Statements
Lecture 4 - Iteration Statements
velvet cakes
cake
3
Iterating decisions
4
How do we translate this into
programming?
5
Basic thermostat
● It has to keep the temperature fixed in a
certain range
● Assume that the range is between 20
degrees celsius and 25 degrees celsius
If temperature > 25 → Decrease temperature
If temperature < 20 → Increase temperature
Else → Do nothing
6
Basic thermostat
if(t > 25)
// Decrease temperature
else if(t < 20)
// Increase temperature
else
// Do nothing
7
Basic thermostat
● But we can’t check just once
● Must repeat it every 5-10 minutes
9
Iteration statements
● Repeatedly execute some statement → Loop body
● Check the controlling expression each time loop
body is executed
○ If the expression is True (non-zero value), keep
looping
○ If the expression is False (zero value), exit the
loop
10
Logical data in C
Does not have a dedicated boolean datatype
1 True
0 False
Others True
11
Iteration statements
Iteration statements are also
called loops
10 < 13
Types of loops:
● while
● do while 1.3 >= 12
● for
12
while statement
13
while statement
Repeat expressions by testing a controlling expression
while(expression) {statement}
int number = 1;
while(number <= 5)
{
printf(“Hello”);
} 14
while statement
int iterator = 1;
while(iterator <= 3) // check this condition every time
{
printf(“%d\n”, iterator); // printing the value in iterator
iterator = iterator + 1; // increasing iterator by 1
}
15
while statement
int i = 1; i = 1; i is now 1
while(i <= 2) Is i <= 2? Yes. Continue
{ printf() prints i = 1
printf(“%d\n”, i); i = i + 1 i is now 2
i = i + 1; Is i <= 2? Yes. Continue
} printf() prints i = 2
i = i + 1 i is now 3
Is i <= 2? No. Exit loop
16
while statement
The while loop may not be executed at all, since the controlling
expression is checked before execution
int i = 10;
while(i < 5)
{
printf(“Hello\n”);
i++;
}
printf(“We are done.”);
17
Pop Quiz
18
while statement
How to keep a while loop running forever?
19
while statement
How to keep a while loop running
forever?
while(1)
{
// expressions
}
20
while statement
An infinite loop will execute forever unless its body contains a
statement that transfers control out of the loop (break, goto,
return) or calls a function that causes the program to terminate
(exit).
21
Pop Quiz
22
FizzBuzz
Write a program that prints the numbers from 1 to n. But
● for multiples of 3, print ‘Fizz’ instead of the number,
● and for multiples of 5, print ‘Buzz’.
● For numbers that are multiples of both 3 and 5, print
‘FizzBuzz’
23
do while statement
24
do while statement
This is very similar to the while statement with one simple difference
It executes loop body first, then evaluates controlling expression
do {statement} while(expression);
If value of the expression is nonzero, execute the loop body and check again
int i = 10;
do
{
printf(“%d\n”, i); // prints the number in i
i = i - 1; // decrements i by 1
}while(i > 0);
25
do while statement
In case of do while statement, the loop body is execute at least once.
26
Pop Quiz
27
do while statement
Will these produce the same output?
int x = 5; int y = 5;
while(x > 0) do
{ {
printf("%d ", x); printf("%d ", y);
x--; y--;
} }while (y > 0);
28
for statement
29
for statement
● Probably the most used looping statement
● Best for counting variables
for(expr1; expr2; expr3) {statement}
expr1 → initialization step → performed only once before the loop begins
expr2 → controlling expression → continues looping as long as expr2 is nonzero
expr3 → performed every time at the end of each iteration.
30
for statement
Translate a while loop into a for loop
31
for statement
Count up or down:
● for(i = 0; i < 10; i++) // Count up from 0 to 9
● for(i = 1; i <= 10; i++) // Count up from 1 to 10
● for(i = 10; i >= 0; i--) // Countdown from 10 to 0
● for(i = 10; i > 0; i--) // Countdown from 10 to 1
32
for statement
int i; i = 1; i is now 1
for(i = 1; i <= 2; i++) Is i <= 2? Yes. Continue
{ printf() prints i = 1
printf(“%d\n”, i); i++ i is now 2
} Is i <= 2? Yes. Continue
printf() prints i = 2
i++ i is now 3
Is i <= 2? No. Exit loop
33
Pop Quiz
34
for statement
Omitting expressions from for loop can have exciting results
int i;
for(i = 1; ; i++) // keeping expr2 empty
{
printf(“Hello\n”);
}
35
for statement
Omitting expressions from for loop can have exciting results
int i;
for(i = 1; ; i++) // keeping expr2 empty
{
printf(“Hello\n”);
}
36
Loop controls
37
Loop controls
We can use certain keywords to manipulate loops that
are not controlling expressions
● break statement
● continue statement
38
break statement
39
break statement
Normal exit-points in loops
● At the beginning
● At the end
To exit the loop at any point in the loop → break statement
int i = 0;
while(1)
{
if(++i == 10) {break;} // breaks out of the infinite loop
}
40
continue statement
41
continue statement
● Forces the loop to iterate once
● Transfers control to a just before the end of the loop body
● Control remains inside the loop
int i;
for(i = 1; i <= 10; i++)
{
if(i % 2 == 0)
{
continue;
}
printf(“%d\n”, i);
}
42
Pop Quiz
43
continue statement
Write a program which takes sum of only positive numbers from the user
46
Nested loops
● Loops inside loops
● Imagine you’re doing a treasure hunt
● You are given some clues
● Each clue has multiple sub-clues
● Only when you solve every clue, you get the treasure
47
Nested loops
for(i = 0; i < 5; i++) while(i < 5)
{ {
for(j = 0; j < 5; j++) for(j = 0; j < 5; j++)
{ {
printf(“%d %d”, i, j); printf(“%d %d”, i, j);
} }
} i++;
}
Try to trace i & j 48
Pop Quiz
49
Nested loops
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(j == 1)
{break;}
}
}
50
Nested loops
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
Only the inner loop
if(j == 1)
will be broken out of
{break;}
}
}
51
Reading Tasks
● C Programming - A Modern Approach | Chapter 6
52
Thank You
53