Professional Documents
Culture Documents
while (expression)
statement
e.g.:
i = 1;sum = 0;
while (i <= 100) {
sum += i; /* sum=sum + i */
i++;
}
Chapter 3 Control Flow
3.4 Loops — While and For
sum = 0; i = 1;sum = 0;
for (i=1;i <= 100; i++) { while (i <= 100) {
sum += i; sum += i;
} i++;
}
Chapter 3 Control Flow
3.4 Loops — While and For
Example 1: input an integer n, and calculate n items’ sum :
1 1 1
1
3 5 7 How to calculate n!
#include <stdio.h> (factorial) ? And how to
int main (void) output a table of 0!, 1!..n! ?
{
int denominator, flag, i, n;
double item, sum;
printf("Enter n:");
scanf("%d",&n);
flag=1, denominator=1, sum=0;
for (i=1; i<=n; i++){
item=flag*1.0/denominator;
sum = sum+item;
flag = -flag; denominator +=2;
}
printf("sum= %f\n", sum);
return 0;
}
Chapter 3 Control Flow
3.4 Loops — While and For
double fac(int m)
#include <stdio.h> {
double fac(int i); int i;
int main (void) double factorial=1;
{ for (i=1; i<=m; i++)
int i, n; factorial=factorial*i;
double factorial; return factorial;
printf("Enter n:"); }
scanf("%d",&n);
for (i=0; i<=n; i++){
factorial=fac(i);
printf("%d!= %.0f\n", i,factorial);
}
return 0;
}
Chapter 3 Control Flow
3.4 Loops — While and For
#include <stdio.h>
int main(void)
{
int integer;
printf("Enter a integer:");
scanf("%d", &integer);
while(integer > 0){
printf("%d", integer%10);
integer /= 10;
}
printf("\n");
return 0;
}
Chapter 3 Control Flow
3.4 Loops — While and For
2/14
Chapter 3 Control Flow
21
(i=1) 34, 8, 64, 51, 32, 21
8, 34, 64, 51, 32, 21 (m→1)
(i=2) 8, 34, 64, 51, 32, 21
8, 34, 64, 51, 32, 21 (m→0)
(i=3) 8, 34, 64, 51, 32, 21
8, 34, 51, 64, 32, 21 (m→1)
(i=4) 8, 34, 51, 64, 32, 21
8, 32, 34, 51, 64, 21 (m→3)
(i=5) 8, 32, 34, 51, 64, 21
8, 21, 32, 34, 51, 64 (m→4)
〖 Example 〗 Sort
: 81 94 11 96 12 35 17 95 28 58 41 75 15
5-sort 35 17 11 28 12 41 75 15 96 58 81 94 95
3-sort 28 12 11 35 15 41 58 17 94 75 81 96 95
1-sort 11 12 15 17 28 35 41 58 75 81 94 95 96
Chapter 3 Control Flow
3.4 Loops — While and For
return 0;
}
Chapter 3 Control Flow
3.4 Loops — While and For
Example 7: version2
#include <stdio.h>
int main(void)
{
int men, women, child;
for(men=0; men<=15; men++)
for(women=0; women<=22; women++){
child=45-men-women;
if(men*3+women*2+child*0.5==45)
printf("men=%d, women=%d, child=%d\
n",
men, women, child);
}
return 0;
}
Chapter 3 Control Flow
3.5 Loops — Do-While
Example:
sum=0;
i = 1;
do {
sum += i;
i++;
} while (i <= 100);
Chapter 3 Control Flow
3.5 Loops — Do-While
}
Chapter 3 Control Flow
3.5 Loops — Do-While
#include <stdio.h>
int digits(int);
int main(void)
{
int number;
printf("Enter a number:");
scanf("%d", &number);
int digits(int integer)
printf("It contains
{ %d digits.\n", digits(number));
return 0; int count=0;
} if(integer<0) integer=-integer;
do{
integer/=10;
count++;
} while(integer != 0);
return count;
}
Chapter 3 Control Flow
3.6 Break and Continue
continue statement :
what is the
It causes the next iteration of the enclosing for,
difference between
while, or do loop to begin.
break and continue?
e.g.:
for ( i = 0; i < n; i ++) { for (n = 100; n <= 200; n++) {
if( a[i] < 0 ) /* skip negative
if (n%3 == elements
0) */
continue; continue;
… /* do positive elements printf("%d
*/ ", n);
}
}
In the while and do, the test part is executed
immediately; in the for, control passes to the increment
step. The continue statement applies only to loops, not to
switch.
Chapter 3 Control Flow
3.7 Goto and Labels
P.60 3-2