2. while
The while Repetition Structure
while
while (condition) action;
• Example in C:
int product=2;
while (product<=1000)
product=2*product;
product<=1000 product=2*product
Y
N
while
3. Counter-Controlled Repetition
#include <stdio.h>
void main() {
int counter, grade, total, average;
total=0;
counter=0;
while (counter < 10) {
printf("Enter grade:");
scanf("%d", &grade);
total+=grade;
counter++;
}
average=total/10;
printf("Class average is %dn", average);
}
(initialization)
(execute loop)
(termination)
4. Counter-Controlled Repetition
• Result
Enter grade:60
Enter grade:70
Enter grade:65
Enter grade:76
Enter grade:73
Enter grade:56
Enter grade:78
Enter grade:64
Enter grade:89
Enter grade:76
Class average is 70
5. Nested Control Structures
#include <stdio.h>
void main() {
int passed=0, failed=0, counter=0, result;
while (counter<10) {
printf("Enter result (1=pass, 2=fail):");
scanf("%d", &result);
if (result==1)
passed++;
else
failed++;
counter++;
}
6. Nested Control Structures
printf("Passed %dn", passed);
printf("Failed %dn", failed);
if (passed>8)
printf("Excellent Classn");
}
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):2
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Passed 9
Failed 1
Excellent Class
7. do-while The do-while Repetition Structure
do while
do
action;
while (condition);
condition
action
Y
N
8. do-while The do-while Repetition Structure
• 1+2+…+100
#include <stdio.h>
void main() {
int s, i;
s=0;
i=1;
do {
s = s + i;
i++;
} while (i<=100);
printf("1+2+...+100=%dn", s);
}
9. The for Repetition Structure
for
for (expression1; expression2; expression3)
action;
expression2 action
Y
N
expression3
expression1
10. The for Repetition Structure
for
#include <stdio.h>
void main() {
int counter;
for (counter = 1; counter <= 10; counter++)
printf("%d ", counter);
}
1 2 3 4 5 6 7 8 9 10
11. The break Statement
break
#include <stdio.h>
void main() {
int x;
for (x = 1; x <= 10; x++) {
if (x == 5)
break;
printf("%d ", x);
}
printf("nBroke out of loop at x == %dn", x);
}
1 2 3 4
Broke out of loop at x == 5
12. break
The break Statement
Example: break in switch and for structure
for (i=1;i<=3;i++)
{ switch(i)
{ case 1: printf(“*n”);
break;
case 2: printf(“**n”);
break;
case 3: printf(“***n”);
break;
}
} *
**
***
for (i=1;i<=3;i++)
{ if (i==1) {printf(“*n”);
break;}
if (i==2) {printf(“**n”);
break;}
if (i==3) {printf(“***n”);
break;}
}
*
13. The continue Statement
continue
#include <stdio.h>
void main() {
int x;
x = 1;
while (x <= 10) {
if (x == 5)
continue;
printf("%d ", x);
x++;
}
}
1 2 3 4
#include <stdio.h>
void main() {
int x;
for (x = 1; x <= 10; x++) {
if (x == 5)
continue;
printf("%d ", x);
}
}
1 2 3 4 6 7 8 9 10
14. The goto Statement
goto
#include <stdio.h>
void main() {
int s=0, i=1;
Loop:
s = s + i;
i++;
if (i<=100) goto Loop;
printf("1+2+...+100=%dn", s);
}
1+2+…+100=5050
Label