0% found this document useful (0 votes)
28 views

Chapter5-Looping and Repetition

Looping and repetition allow code to be executed multiple times. There are three types of loops in C: while loops, do-while loops, and for loops. While loops evaluate a condition and repeat the loop body if true. Do-while loops always execute the body at least once before evaluating the condition. For loops allow initialization of a counter, condition test, and counter update to control repetition. Break and continue statements can alter normal loop flow by stopping or skipping iterations.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Chapter5-Looping and Repetition

Looping and repetition allow code to be executed multiple times. There are three types of loops in C: while loops, do-while loops, and for loops. While loops evaluate a condition and repeat the loop body if true. Do-while loops always execute the body at least once before evaluating the condition. For loops allow initialization of a counter, condition test, and counter update to control repetition. Break and continue statements can alter normal loop flow by stopping or skipping iterations.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

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

❖ Repetition statements are called loops, and are used to


repeat the same code multiple times in succession
▪ The number of repetitions is based on criteria defined in
the loop structure, usually a true/false expression

❖ There are three repetition structures in C, the while loop,


the do-while loop, and the for loop

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);

▪ It continues testing the expression/condition and executing


its block until the expression/condition evaluates to false

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;

true sum = sum + num;


num <= 10
num = num + 1;

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);

▪ It continues executing the statement until the


expression/condition becomes false
▪ Note that the statement within the do loop is always
executed at least once

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:

for (initial statement; loop condition; update statement){


statement(s);
}

❖ The for loop executes as follow:


▪ The initial statement is executed
▪ The loop expression/condition is evaluated
▪ If it is TRUE, the loop statement is executed followed by the
execution of the update statement
▪ Repeat step until the loop condition evaluates to FALSE

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

#include <stdio.h> <Result>


value of a: 10
int main () { value of a: 11
value of a: 12
int a; value of a: 13
value of a: 14
/* for loop execution */ value of a: 15
for( a = 10; a < 20; a = a + 1 ){ value of a: 16
printf("value of a: %d\n", a); value of a: 17
} value of a: 18
value of a: 19
return 0;
}

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

for(num2 = 0; num2 <=9; num2++)


{
for(num1=0; num1<9; num1++)
outer { inner
printf(“%d, %d\n”, num2, num1);
}
}

slide 20
Nested Loop
❖ Example

int main ()
{
int i, j;

for (i=1; i<10; i++) {


printf ("%d-th iteration\n", i);
for (j = 1; j < 10; j++)
printf("%d X %d = %d\n", i, j, i*j);
printf ("\n", i);
}

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

You might also like