4.control Structure Loops
4.control Structure Loops
CSE 1101
C loops
CONTROL STRUCTURE
Repetition Statements
2
REPETITION STATEMENTS
You can make decision with your program.
Another problem arise when you want repeating
doing one operation.
For example:
create a program to print 1 to 10.
CONTROL STRUCTURE
A pretest repetition statement computes a value for its associated
predicate before entering the loop body and will execute the loop body as
long as the predicate is true.
Once the predicate computes to false, repetition stops and the program
control passes to the next statement that follows the repetition
statement.
Flowchart
true
predicate loop body
false
4
CONTROL STRUCTURE
A posttest repetition statement first executes the loop body and then
computes the predicate. If the predicate is true, the loop body is executed
again; otherwise, the repetition terminates.
Flowchart
true
false
5
CONTROL STRUCTURE
while statement;
for statement; a pretest repetition statement
do-while statement; a posttest repetition statement
Example
while (++counter <= 10) {
printf (“%d\n”, counter);
/*end while*/
6
THE ESSENTIALS OF REPETITION
Loop
Group of instructions computer executes
repeatedly while some condition remains true
Counter-controlled repetition
Definite repetition: know how many times loop
will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data" 7
THE FOR REPETITION STATEMENT
8
THE FOR REPETITION STATEMENT
Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
Example:
for( int counter = 1; counter <= 10; counter++ )
printf( "%d\n", counter );
Prints the integers from one to ten
No
semicolon
(;) after
last
expression
9
THE FOR REPETITION STATEMENT
For loops can usually be rewritten as while loops:
initialization;
while ( loopContinuationTest ) {
statement;
increment;
}
Initialization and increment
Can be comma-separated lists
Example:
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );
10
THE FOR STATEMENT : NOTES AND
OBSERVATIONS
Arithmetic expressions
Initialization, loop-continuation, and increment
can contain arithmetic expressions. If x equals 2
and y equals 10
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
Notes about the for statement:
"Increment" may be negative (decrement)
If the loop continuation condition is initially false
The body of the for statement is not performed
Control proceeds with the next statement after the for
statement
Control variable
Often printed or used inside for body, but not necessary 11
THE FOR STATEMENT : NOTES AND
OBSERVATIONS
12
1 /* Fig. 4.5: fig04_05.c
2 Summation with for */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int sum = 0; /* initialize sum */
9 int number; /* number to be added to sum */
10
11 for ( number = 2; number <= 100; number += 2 ) {
sum += number; /* add number to sum */
12
Note that number has a different
13 } /* end for */
14 value each time this statement is
15 printf( "Sum is %d\n", sum ); /* output sum */ executed
16
17 return 0; /* indicate program ended successfully */
18
19 } /* end function main */
Sum is 2550
13
THE WHILE REPETITION STATEMENT
Repetition structure
Programmer specifies an action to be repeated while
some condition remains true
Psuedocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
while loop repeated until condition becomes
false
14
THE WHILE REPETITION STATEMENT
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;
16
THE DO…WHILE REPETITION
STATEMENT
Example (letting counter = 1):
do {
printf( "%d ", counter );
} while (++counter <= 10);
Prints the integers from 1 to 10
17
THE DO…WHILE REPETITION
STATEMENT
Flowchart of the do…while repetition
statement
18
1 /* Fig. 4.9: fig04_09.c
2 Using the do/while repetition statement */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int counter = 1; /* initialize counter */
9
10 do {
11 printf( "%d ", counter ); /* display counter */
} while ( ++counter <= 10 ); /* end do...while */
12
increments counter then checks if it
13
14 return 0; /* indicate program ended successfully */ is less than or equal to 10
15
16 } /* end function main */
1 2 3 4 5 6 7 8 9 10
19
C’S SINGLE-ENTRY/SINGLE-EXIT SEQUENCE,
SELECTION AND REPETITION STATEMENTS.
20
THE BREAK AND CONTINUE STATEMENTS
break
Causes immediate exit from a while, for,
do…while or switch statement
Program execution continues with the first statement
after the structure
Common uses of the break statement
Escape early from a loop
Skip the remainder of a switch statement
21
THE BREAK AND CONTINUE STATEMENTS
continue
Skips the remaining statements in the body of a
while, for or do…while statement
Proceeds with the next iteration of the loop
while and do…while
Loop-continuation test is evaluated immediately after the
continue statement is executed
for
Increment expression is executed, then the loop-
continuation test is evaluated
22
REFERENCES
23
Q&A
Any Question?
Can you write an infinity loop using
for/while/do-while statements?
24