Chapter5-Looping and Repetition
Chapter5-Looping and Repetition
Yongseok Son
Department of Computer Science and Engineering
Chung-Ang University
Repetition Structure
❖ A repetition structure allows the programmer to specify
that an action is to be repeated while some condition
remains true
slide 2
Why is Repetition Needed?
❖ Suppose you want to add five numbers to find their average
▪ From what you have learned so far, you could proceed as
follows (assume that all variables are properly declared)
▪ Example 1
• scanf(“%d, %d, %d, %d, %d”, &num1, &num2, &num3, &num4,
&num5);
• sum = num1 + num2 + num3 + num4 + num5;
• average = sum / 5;
slide 3
While statement
❖ The while statement evaluates expression/condition,
which must return a boolean value
▪ If the expression/condition is true, the statement(s) in the
while block is/are executed
while (expression/condition)
statement(s);
slide 4
While statement
❖ In C, while is a reserved word
▪ The expression acts as a decision maker and is usually a logical
expression
▪ The statement can be either a simple or compound statement
❖ The expression provides an entry condition
▪ If it initially evaluates to true, the statement executes
▪ The loop condition is then reevaluated
▪ If it again evaluates to true, the statement executes again
▪ The statement (body of the loop) continues to execute until the
expression is no longer true
▪ A loop that continues to execute endlessly is called an infinite loop
• To avoid an infinite loop, make sure that the loop’s body contains
statement(s) that assure that the exit condition
slide 5
While statement
❖ While statement
while (expression){
statement(s)
}
repetition
true
expression statement
false
slide 6
While statement
❖ While statement
num = 1;
sum = 0;
while (num <= 10){
sum = sum + num;
num = num + 1;
}
num = 1;
sum = 0;
false
slide 7
While statement
❖ Example
#include <stdio.h>
int main()
{ Hello, world! 0
int i = 0; Hello, world! 1
…
while (i < 100) { Hello, world! 97
printf("Hello, world! %d\n", i); Hello, world! 98
i++; Hello, world! 99
}
return 0;
}
slide 8
do-while statement
❖ It is similar to while loops except it first executes the
statement(s) inside the loop, and then evaluates the
expression/condition
▪ If the expression is true, the statement(s) in the do loop
is/are executed again
do
statement(s);
while (expression/condition);
slide 9
do-while statement
❖ do-while statement
do{
statement(s);
}while (expression);
statement
true
expression
false
slide 10
do-while statement
❖ The body (block) of do-while statement is executed at
least once
num = 1;
sum = 0;
num = 1;
sum = sum + num;
sum = 0;
num = num + 1;
do {
sum = sum + num;
num = num + 1;
} while (n <= 10)
num <= 10
true
false
slide 11
do-while statement
❖ Example
#include <stdio.h>
int main()
{
Hello, world! 0
int i = 0; //initialization
Hello, world! 1
…
do {
Hello, world! 97
printf("Hello, world! %d\n", i); //repeat
Hello, world! 98
i++; //repeat
Hello, world! 99
} while (i < 100); //condition
return 0;
}
slide 12
For statement
❖ Usually used when a loop will be executed a set number of times
❖ The for looping structure is a specialized form of the while loop
▪ The general form of the for statement is:
slide 13
For statement
❖ The for loop executes as follows:
▪ (1) The initial statement executes
▪ (2) The loop condition is evaluated
▪ (3) Execute the for loop statement
▪ (4) Execute the update statement
initial statement
loop
statement update statement
condition true
false
slide 14
For statement
❖ Example of for looping
slide 15
Break statement
❖ Break statement
▪ Go out of the loop block and execute next statements
▪ When a break statement is encountered inside a loop, the
loop is immediately terminated and the program control
resumes at the next statement following the loop
▪ If you are using nested loops, the break statement will stop
the execution of the innermost loop and start executing the
next line of code after the block
▪ Example
while(1){
scanf(“%d”, &j);
if (j == 0)
break;
}
slide 16
Continue statement
❖ Continue in loop
▪ The continue statement in C programming works
somewhat like the break statement.
• Instead of forcing termination, continue statement forces the
loop to continue or execute the next iteration
▪ Example
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {// loop from 1 to 10
if (i == 6) // If i is equals to 6,
Output:
continue; // continue to next iteration
1 2 3 4 5 7 8 9 10
else
printf("%d ", i); // otherwise print the value of i
}
return 0;
}
slide 17
Continue statement
❖ How continue statement works?
while statement do while statement
while (testExpression) { do {
// codes // codes
if (testExpression) { if (testExpression) {
continue; continue;
} }
// codes // codes
} }
while (testExpression);
for statement
for (init; testExpression; update) {
// codes
if (testExpression) {
continue;
}
// codes
}
slide 18
Nested Loop
❖ The placing of one loop inside the body of another loop
is called nesting
❖ When you “nest” two loops, the outer loop takes control
of the number of complete repetitions of the inner loop
❖ While all types of loops may be nested, the most
commonly nested loops are for loops
slide 19
Nested Loop
❖ Nested Loop allows to use one loop inside another loop
❖ When working with nested loops, the outer loop changes
only after the inner loop is completely finished
slide 20
Nested Loop
❖ Example
int main ()
{
int i, j;
return 0;
}
slide 21
Infinite Loop
❖ An infinite loop occurs when a condition always
evaluates to true and continues to execute endlessly
▪ Example
while (1){
i = 0;
i++;
printf(“%d”, i);
}
slide 22