The C Programming Language Chapter 5 Control Flow
#黑日工作室 #
非常感谢 天津城市建设学院周锦姝老师
The C Programming Language Chapter 5 Control Flow
Chapter 5 Control Flow
Overview of C Statements
Selected Construction
if
switch
Loops
goto and if
while and do while
for
break and continue
The C Programming Language Chapter 5 Control Flow
5.1 Overview of C Statements
C statements,using a semicolon,;” as a
statement
terminator ;
Kinds of C statements:
Expression statements:an expression flowed by,;”
Control statements (9),if( )~else~ switch
for( )~
while( )~
do~while( )
continue
break
goto
return
branches
loops
others
As total=total+limit ;
a=3 ;
func( ) ;
printf(“Hello,world!\n”) ;
Compound statements:using braces {…} to group
declarations and statements,so that they are
syntactically equivalent to a single statement.
o There is no,;” after the right brace.
As {
int x=1,y=2,z ;
z=x+y ;
printf(“%d”,z) ;
}
The C Programming Language Chapter 5 Control Flow
5.2 Selected Construction (if and switch)
if statement,has three syntax
if (expression) statement
flowchart
expr
statement
!0
=0
as,if (x>y) printf(“%d”,x);
expr
statement1 statement2
!0 =0? if (expression)statement1
else
statement2
flowchart as,if (x>y) max=x ;else max=y ;
The C Programming Language Chapter 5 Control Flow
if ( expr1 ) statement1
else if (expr2 ) statement2
else if (expr3 ) statement3
…...
[ else statement n ]
expr1
statemnt1
非 0
=0
expr2
expr3
statemntnstatemnt3statemnt2
非 0
非 0
=0
=0
flowchart:
as,if (salary>1000) index=0.4;
else if (salary>800) index=0.3;
else if (salary>600) index=0.2;
else if (salary>400) index=0.1;
else index=0;
The C Programming Language Chapter 5 Control Flow
as,if (a==b&&x==y) printf(“a=b,x=y”);
if (3) printf(“OK”);
if (?a?) printf(“%d”,?a?);
Notice:
If (expression)
if (x)? if (x!=0)
if(!x)? if(x==0)
as,
#include <stdio.h>
main()
{ int x,y;
scanf(“%d,%d”,&x,&y);
if(x>y)
x=y; y=x;
else
x++; y++;
printf(“%d,%d\n”,x,y);
}
Compile Error!
the value may be any type
statement may be compound statement
The C Programming Language Chapter 5 Control Flow
/*ch5_1.c*/
#include <stdio.h>
main()
{ int x,y;
printf("Enter an integer:");
scanf("%d",&x);
y=x;
if(y<0)
y= -y;
printf("\ninteger:%d--->absolute value:%d\n",x,y);
}
例 求一个数的绝对值
Enter an integer,-12?
integer:-12--->absolute value,12
The C Programming Language Chapter 5 Control Flow
/*ch5_2.c*/
#include <stdio.h>
main()
{ int a,b;
printf("Enter integer a:");
scanf("%d",&a);
printf(“Enter integer b:");
scanf("%d",&b);
if (a= =b)
printf(“a= =b\n");
else
printf(“a!=b\n");
}
例 输入两个数并判断两数相等否
Enter integer a:12?
Enter integer b:12?
a= =b
Enter integer a:12?
Enter integer b:9?
a!=b
The C Programming Language Chapter 5 Control Flow
/*ch5_3.c*/
#include <stdio.h>
main()
{ char c;
printf("Enter a character:");
c=getchar();
if(c<0x20) printf("The character is a control character\n");
else if(c>='0'&&c<='9') printf("The character is a digit\n");
else if(c>='A'&&c<='Z') printf("The character is a capital letter\n");
else if(c>='a'&&c<='z') printf("The character is a lower letter\n");
else printf("The character is other character\n");
}
例 判断输入字符种类运行,Enter a character,?
The character is a control character
运行,t t,8?
t i digi
运行,Enter a character,D?
The character is a capital letter
运行,Enter a character,h
he c aracter is a lower let er
Enter a ch a ter,&?
The c ara te is other haracter
The C Programming Language Chapter 5 Control Flow
if statement can be nested:
if (expr1)
if (expr2) statement1
else statement2
else
if (expr3) statement3
else statement4
nested if
nested if
if (expr1)
if (expr2)
statement1
else
statement2
nested if
if (expr1)
if (expr2)
statement1
else
statement2
nested if
if (expr1)
statement1
else
if(expr2)
statement2
else
statement3
nested if
The C Programming Language Chapter 5 Control Flow
例 输入两数并判断其大小关系
/*ch5_4.c*/
#include <stdio.h>
main()
{ int x,y;
printf("Enter integer x,y:");
scanf("%d,%d",&x,&y);
if(x!=y)
if(x>y) printf("X>Y\n");
else printf("X<Y\n");
else
printf("X==Y\n");
}
Enter integer x,y:12,23?
X<Y
Enter integer x,y:12,6?
X>Y
Enter integer x,y:12,12?
X==Y
The C Programming Language Chapter 5 Control Flow
if-else association:
associates the else with the closest previous
else-less if
if(……)
if(……)
if(……)
else…...else…...
else…...
as,if (a= =b)
if (b= =c)
printf(“a= =b= =c”);
else
printf(“a!=b”);
Uses braces { } to force the proper association
main()
{ int x=100,a=10,b=20;
int v1=5,v2=0;
if (a<b)
if (b!=15)
if (!v1) x=1;
els
if(v2) x=10;
x=-1;
printf(“x=%d”,x);
} x=-1
The C Programming Language Chapter 5 Control Flow
flowchart
Switch statement? switch ( expr )
{ case E1,statement 1; break ;
case E2,statement 2; break;
…….
case En,statement n; break;
[default,statement ;break;]
}
Notice:
E1,E2,… En must be constant expressions,and must
be different;
Cases serve just as labels,the break statements
causes an immediate exit from the switch ;
Switch can be nested ;
Statements after case having no { } is all right;
Several cases can use the same statements.
as,……
case?A?:
case?B?:
case?C?,
printf(“score>60\n”);
break;
……..
switch
expression
statement1 statement2 statement n statement…...
E 1 E 2 En default
case
The C Programming Language Chapter 5 Control Flow
as,switch(score)
{ case 5,printf(“Very good!”);
case 4,printf(“Good!”);
case 3,printf(“Pass!”);
case 2,printf(“Fail!”);
default,printf(“data error!”);
}
when score is 5:
Very good! Good! Pass! Fail! data error!
The C Programming Language Chapter 5 Control Flow
main()
{ int x=1,y=0,a=0,b=0;
switch(x)
{ case 1:
switch(y)
{ case 0,a++; break;
case 1,b++; break;
}
case 2,a++;b++; break;
case 3,a++;b++;
}
printf(“\na=%d,b=%d”,a,b);
}
a=2,b=1
The C Programming Language Chapter 5 Control Flow
5.3 Loops
The goto Statement
The while Statement
The do ~ while Statement
The for Statement
The C Programming Language Chapter 5 Control Flow
Goto statement causes an unconditional jump
to a labeled statement somewhere in the
current function;
A label has the same form as a variable name;
the goto should be used ralely.
goto label;
….…..
label,statements;
goto
The C Programming Language Chapter 5 Control Flow
if and goto can make loops
/*ch5_1.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
loop,if(i<=100)
{ sum+=i;
i++;
goto loop;
}
printf("%d",sum);
}
sum=0+1=1
sum=1+2=3
sum=3+3=6
sum=6+4=10
……
sum=4950+100=5050
terminationincrement
condition
loop body
initialization
The C Programming Language Chapter 5 Control Flow
while
while (expression)
satement;
flowchart:
expr
statement
F(0)
T(!0)
while
The expression is evaluated,then
the statement is executed;
Specifications:
Exit from a loop
The value of the expression is false
( 0) ;
Encounter break,return,goto
in the body.
Infinite loop,while(1)
statement;
The C Programming Language Chapter 5 Control Flow
while
/*ch5_2.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
while(i<=100)
{ sum=sum+i;
i++;
}
printf("%d",sum);
}
initialization
terminationincrement
condition
loop body
The C Programming Language Chapter 5 Control Flow
例 显示 1~10的平方
/*ch5_21.c*/
#include <stdio.h>
main()
{ int i=1;
while (i<=10)
{ printf("%d*%d=%d\n",i,i,i*i);
i++;
}
}
result:
1*1=1
2*2=4
3*3=9
4*4=16
5*5=25
6*6=36
7*7=49
8*8=64
9*9=81
10*10=100
The C Programming Language Chapter 5 Control Flow
do
satement;
while (expression)
flowchart
do
statement
expr
F(0)
T(!0) while
The statement is executed,then the
expression is evaluated;
Specifications:
The body is always executed at least once
do~while => while
expr
statement
F(0)
T(!0)
statement
While
do~while
The C Programming Language Chapter 5 Control Flow
/*ch5_3.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
do
{ sum+=i;
i++;
}while(i<=100);
printf("%d",sum);
}
do~while
The C Programming Language Chapter 5 Control Flow
compare while with do~while
/*ch5_4.c*/
#include <stdio.h>
main()
{ int i,sum=0;
scanf(“i=%d",&i);
do
{ sum+=i;
i++;
}while(i<=10);
printf(“sum=%d",sum);
}
/*ch5_41.c*/
#include <stdio.h>
main()
{ int i,sum=0;
scanf(“i=%d",&i);
while(i<=10)
{ sum+=i;
i++;
}
printf(“sum=%d",sum);
}
i=1
sum=55
i=1
sum=55
1
Su =11
1
=0
The C Programming Language Chapter 5 Control Flow
for
for ([expr1] ;[ expr2] ;[ expr3])
statement;
flowchart:
expr2
statement
F(0)
T(!0)
for
expr1
expr3
The C Programming Language Chapter 5 Control Flow
for,for( initialization; loop condition ; increment )
{
statements;
}
specifications:
expr1,expr2,expr3 may be any types;
Any of the three parts can be omitted,although the
semicolons ; must remain.
Infinite loop,for( ; ; )
for=>while expr1;while (expr2)
{
statements;
expr3;
}
例 用 for循环求
#include <stdio.h>
main()
{ int i,sum=0;
for(i=1; i<=100 ;i++ )
sum+=i;
printf("%d",sum);
}
The C Programming Language Chapter 5 Control Flow
as,#include<stdio.h>
main( )
{ int i=0;
for(i=0;i<10;i++)
putchar(?a?+i);
}
result,abcdefghij
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;i++)
putchar(?a?+i);
}
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;)
putchar(?a?+(i++));
}
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;putchar(?a?+i),i++)
}
The C Programming Language Chapter 5 Control Flow
main()
{ int i,j,k;
for(i=0,j=100;i<=j;i++,j--)
{ k=i+j;
printf("%d+%d=%d\n",i,j,k);
}
}
#include<stdio.h>
main()
{ char c;
for(;(c=getchar())!='\n';)
printf("%c ",c);
}
The C Programming Language Chapter 5 Control Flow
Loops can be nested
(1) while()
{ ……
while()
{ ……
}
…...
}
(2) do
{ ……
do
{ ……
}while( );
…...
}while( );
(3) for( ; ; )
{ ……
for( ; ; )
{ ……
}
…...
}
(4) while()
{ ……
do
{ ……
}while( );
…….
}
(5) for( ; ; )
{ ……
while()
{ ……
}
…...
}
(6) do
{ ……
for( ; ; )
{ ……
}
…...
}while( );
The C Programming Language Chapter 5 Control Flow
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
9 18 27 36 45 54 63 72 81
…………
…..
例 循环嵌套,输出九九表
i
j
/*ch5_5.c*/
#include <stdio.h>
main()
{ int i,j;
for(i=1;i<10;i++)
printf("%4d",i);
printf("\n---------------------------------------\n");
for(i=1;i<10;i++)
for(j=1;j<10;j++)
printf( (j==9)?"%4d\n":"%4d",i*j);
}
The C Programming Language Chapter 5 Control Flow
i<10
printf
F(0)
T(!0)
i=1
j++
j=1
j<10
T(!0)
F(0)
i++
for(i=1;i<10;i++)
for(j=1;j<10;j++)
printf((j==9)?"%4d\n":"%4d",i*j);
outer loop
inner loop
The C Programming Language Chapter 5 Control Flow
5.4 Break and Continue
Break
The break statement provides an
immediate exit from loops (while,
do~while and for ) or switch,
expr
……
break;
……
F(0)
T(!0)
whiledo
……
break;
…...
expr
F(0)
T(!0) while
expr2
……
break;
…...
F(0)
T(!0)
for
expr1
expr3
switch
expr
statement1
break;
statement2
break;
statementn
break;
statement
break;
…...
const 1 const 2 const n default
case
The C Programming Language Chapter 5 Control Flow
例,输出圆面积,面积大于 100时停止
#define PI 3.14159
main()
{
int r;
float area;
for(r=1;r<=10;r++)
{ area=PI*r*r;
if(area>100)
break;
printf("r=%d,area=%.2f\n",r,area);
}
}
break
The C Programming Language Chapter 5 Control Flow
例,小写字母转换成大写字母,直至输入非字母字符
#include <stdio.h>
main()
{
int i,j;
char c;
while(1)
{ c=getchar();
if(c>='a' && c<='z')
putchar(c-'a'+'A');
else
break;
}
}
break
The C Programming Language Chapter 5 Control Flow
Continue
Skips the statements flow the continue,and causes the
next iteration of the loop(for,while,or do~while) to begin;
Applies only to loops,not to switch.
expr
……
continue;
……
F(0)
T(!0)
while
T(!0)
do
……
continue;
…...
expr
F(0)
while
expr2
……
continue;
…...
F(0)
T(!0)
for
expr1
expr3
The C Programming Language Chapter 5 Control Flow
例 求输入的十个整数中正数的个数及其平均值
/*ch5_12.c*/
#include <stdio.h>
main()
{ int i,num=0,a;
float sum=0;
for(i=0;i<10;i++)
{ scanf("%d",&a);
if(a<=0) continue;
num++;
sum+=a;
}
printf("%d plus integer's sum,%6.0f\n",num,sum);
printf("Mean value:%6.2f\n",sum/num);
}
#黑日工作室 #
非常感谢 天津城市建设学院周锦姝老师
The C Programming Language Chapter 5 Control Flow
Chapter 5 Control Flow
Overview of C Statements
Selected Construction
if
switch
Loops
goto and if
while and do while
for
break and continue
The C Programming Language Chapter 5 Control Flow
5.1 Overview of C Statements
C statements,using a semicolon,;” as a
statement
terminator ;
Kinds of C statements:
Expression statements:an expression flowed by,;”
Control statements (9),if( )~else~ switch
for( )~
while( )~
do~while( )
continue
break
goto
return
branches
loops
others
As total=total+limit ;
a=3 ;
func( ) ;
printf(“Hello,world!\n”) ;
Compound statements:using braces {…} to group
declarations and statements,so that they are
syntactically equivalent to a single statement.
o There is no,;” after the right brace.
As {
int x=1,y=2,z ;
z=x+y ;
printf(“%d”,z) ;
}
The C Programming Language Chapter 5 Control Flow
5.2 Selected Construction (if and switch)
if statement,has three syntax
if (expression) statement
flowchart
expr
statement
!0
=0
as,if (x>y) printf(“%d”,x);
expr
statement1 statement2
!0 =0? if (expression)statement1
else
statement2
flowchart as,if (x>y) max=x ;else max=y ;
The C Programming Language Chapter 5 Control Flow
if ( expr1 ) statement1
else if (expr2 ) statement2
else if (expr3 ) statement3
…...
[ else statement n ]
expr1
statemnt1
非 0
=0
expr2
expr3
statemntnstatemnt3statemnt2
非 0
非 0
=0
=0
flowchart:
as,if (salary>1000) index=0.4;
else if (salary>800) index=0.3;
else if (salary>600) index=0.2;
else if (salary>400) index=0.1;
else index=0;
The C Programming Language Chapter 5 Control Flow
as,if (a==b&&x==y) printf(“a=b,x=y”);
if (3) printf(“OK”);
if (?a?) printf(“%d”,?a?);
Notice:
If (expression)
if (x)? if (x!=0)
if(!x)? if(x==0)
as,
#include <stdio.h>
main()
{ int x,y;
scanf(“%d,%d”,&x,&y);
if(x>y)
x=y; y=x;
else
x++; y++;
printf(“%d,%d\n”,x,y);
}
Compile Error!
the value may be any type
statement may be compound statement
The C Programming Language Chapter 5 Control Flow
/*ch5_1.c*/
#include <stdio.h>
main()
{ int x,y;
printf("Enter an integer:");
scanf("%d",&x);
y=x;
if(y<0)
y= -y;
printf("\ninteger:%d--->absolute value:%d\n",x,y);
}
例 求一个数的绝对值
Enter an integer,-12?
integer:-12--->absolute value,12
The C Programming Language Chapter 5 Control Flow
/*ch5_2.c*/
#include <stdio.h>
main()
{ int a,b;
printf("Enter integer a:");
scanf("%d",&a);
printf(“Enter integer b:");
scanf("%d",&b);
if (a= =b)
printf(“a= =b\n");
else
printf(“a!=b\n");
}
例 输入两个数并判断两数相等否
Enter integer a:12?
Enter integer b:12?
a= =b
Enter integer a:12?
Enter integer b:9?
a!=b
The C Programming Language Chapter 5 Control Flow
/*ch5_3.c*/
#include <stdio.h>
main()
{ char c;
printf("Enter a character:");
c=getchar();
if(c<0x20) printf("The character is a control character\n");
else if(c>='0'&&c<='9') printf("The character is a digit\n");
else if(c>='A'&&c<='Z') printf("The character is a capital letter\n");
else if(c>='a'&&c<='z') printf("The character is a lower letter\n");
else printf("The character is other character\n");
}
例 判断输入字符种类运行,Enter a character,?
The character is a control character
运行,t t,8?
t i digi
运行,Enter a character,D?
The character is a capital letter
运行,Enter a character,h
he c aracter is a lower let er
Enter a ch a ter,&?
The c ara te is other haracter
The C Programming Language Chapter 5 Control Flow
if statement can be nested:
if (expr1)
if (expr2) statement1
else statement2
else
if (expr3) statement3
else statement4
nested if
nested if
if (expr1)
if (expr2)
statement1
else
statement2
nested if
if (expr1)
if (expr2)
statement1
else
statement2
nested if
if (expr1)
statement1
else
if(expr2)
statement2
else
statement3
nested if
The C Programming Language Chapter 5 Control Flow
例 输入两数并判断其大小关系
/*ch5_4.c*/
#include <stdio.h>
main()
{ int x,y;
printf("Enter integer x,y:");
scanf("%d,%d",&x,&y);
if(x!=y)
if(x>y) printf("X>Y\n");
else printf("X<Y\n");
else
printf("X==Y\n");
}
Enter integer x,y:12,23?
X<Y
Enter integer x,y:12,6?
X>Y
Enter integer x,y:12,12?
X==Y
The C Programming Language Chapter 5 Control Flow
if-else association:
associates the else with the closest previous
else-less if
if(……)
if(……)
if(……)
else…...else…...
else…...
as,if (a= =b)
if (b= =c)
printf(“a= =b= =c”);
else
printf(“a!=b”);
Uses braces { } to force the proper association
main()
{ int x=100,a=10,b=20;
int v1=5,v2=0;
if (a<b)
if (b!=15)
if (!v1) x=1;
els
if(v2) x=10;
x=-1;
printf(“x=%d”,x);
} x=-1
The C Programming Language Chapter 5 Control Flow
flowchart
Switch statement? switch ( expr )
{ case E1,statement 1; break ;
case E2,statement 2; break;
…….
case En,statement n; break;
[default,statement ;break;]
}
Notice:
E1,E2,… En must be constant expressions,and must
be different;
Cases serve just as labels,the break statements
causes an immediate exit from the switch ;
Switch can be nested ;
Statements after case having no { } is all right;
Several cases can use the same statements.
as,……
case?A?:
case?B?:
case?C?,
printf(“score>60\n”);
break;
……..
switch
expression
statement1 statement2 statement n statement…...
E 1 E 2 En default
case
The C Programming Language Chapter 5 Control Flow
as,switch(score)
{ case 5,printf(“Very good!”);
case 4,printf(“Good!”);
case 3,printf(“Pass!”);
case 2,printf(“Fail!”);
default,printf(“data error!”);
}
when score is 5:
Very good! Good! Pass! Fail! data error!
The C Programming Language Chapter 5 Control Flow
main()
{ int x=1,y=0,a=0,b=0;
switch(x)
{ case 1:
switch(y)
{ case 0,a++; break;
case 1,b++; break;
}
case 2,a++;b++; break;
case 3,a++;b++;
}
printf(“\na=%d,b=%d”,a,b);
}
a=2,b=1
The C Programming Language Chapter 5 Control Flow
5.3 Loops
The goto Statement
The while Statement
The do ~ while Statement
The for Statement
The C Programming Language Chapter 5 Control Flow
Goto statement causes an unconditional jump
to a labeled statement somewhere in the
current function;
A label has the same form as a variable name;
the goto should be used ralely.
goto label;
….…..
label,statements;
goto
The C Programming Language Chapter 5 Control Flow
if and goto can make loops
/*ch5_1.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
loop,if(i<=100)
{ sum+=i;
i++;
goto loop;
}
printf("%d",sum);
}
sum=0+1=1
sum=1+2=3
sum=3+3=6
sum=6+4=10
……
sum=4950+100=5050
terminationincrement
condition
loop body
initialization
The C Programming Language Chapter 5 Control Flow
while
while (expression)
satement;
flowchart:
expr
statement
F(0)
T(!0)
while
The expression is evaluated,then
the statement is executed;
Specifications:
Exit from a loop
The value of the expression is false
( 0) ;
Encounter break,return,goto
in the body.
Infinite loop,while(1)
statement;
The C Programming Language Chapter 5 Control Flow
while
/*ch5_2.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
while(i<=100)
{ sum=sum+i;
i++;
}
printf("%d",sum);
}
initialization
terminationincrement
condition
loop body
The C Programming Language Chapter 5 Control Flow
例 显示 1~10的平方
/*ch5_21.c*/
#include <stdio.h>
main()
{ int i=1;
while (i<=10)
{ printf("%d*%d=%d\n",i,i,i*i);
i++;
}
}
result:
1*1=1
2*2=4
3*3=9
4*4=16
5*5=25
6*6=36
7*7=49
8*8=64
9*9=81
10*10=100
The C Programming Language Chapter 5 Control Flow
do
satement;
while (expression)
flowchart
do
statement
expr
F(0)
T(!0) while
The statement is executed,then the
expression is evaluated;
Specifications:
The body is always executed at least once
do~while => while
expr
statement
F(0)
T(!0)
statement
While
do~while
The C Programming Language Chapter 5 Control Flow
/*ch5_3.c*/
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
do
{ sum+=i;
i++;
}while(i<=100);
printf("%d",sum);
}
do~while
The C Programming Language Chapter 5 Control Flow
compare while with do~while
/*ch5_4.c*/
#include <stdio.h>
main()
{ int i,sum=0;
scanf(“i=%d",&i);
do
{ sum+=i;
i++;
}while(i<=10);
printf(“sum=%d",sum);
}
/*ch5_41.c*/
#include <stdio.h>
main()
{ int i,sum=0;
scanf(“i=%d",&i);
while(i<=10)
{ sum+=i;
i++;
}
printf(“sum=%d",sum);
}
i=1
sum=55
i=1
sum=55
1
Su =11
1
=0
The C Programming Language Chapter 5 Control Flow
for
for ([expr1] ;[ expr2] ;[ expr3])
statement;
flowchart:
expr2
statement
F(0)
T(!0)
for
expr1
expr3
The C Programming Language Chapter 5 Control Flow
for,for( initialization; loop condition ; increment )
{
statements;
}
specifications:
expr1,expr2,expr3 may be any types;
Any of the three parts can be omitted,although the
semicolons ; must remain.
Infinite loop,for( ; ; )
for=>while expr1;while (expr2)
{
statements;
expr3;
}
例 用 for循环求
#include <stdio.h>
main()
{ int i,sum=0;
for(i=1; i<=100 ;i++ )
sum+=i;
printf("%d",sum);
}
The C Programming Language Chapter 5 Control Flow
as,#include<stdio.h>
main( )
{ int i=0;
for(i=0;i<10;i++)
putchar(?a?+i);
}
result,abcdefghij
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;i++)
putchar(?a?+i);
}
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;)
putchar(?a?+(i++));
}
as,#include<stdio.h>
main( )
{ int i=0;
for(;i<10;putchar(?a?+i),i++)
}
The C Programming Language Chapter 5 Control Flow
main()
{ int i,j,k;
for(i=0,j=100;i<=j;i++,j--)
{ k=i+j;
printf("%d+%d=%d\n",i,j,k);
}
}
#include<stdio.h>
main()
{ char c;
for(;(c=getchar())!='\n';)
printf("%c ",c);
}
The C Programming Language Chapter 5 Control Flow
Loops can be nested
(1) while()
{ ……
while()
{ ……
}
…...
}
(2) do
{ ……
do
{ ……
}while( );
…...
}while( );
(3) for( ; ; )
{ ……
for( ; ; )
{ ……
}
…...
}
(4) while()
{ ……
do
{ ……
}while( );
…….
}
(5) for( ; ; )
{ ……
while()
{ ……
}
…...
}
(6) do
{ ……
for( ; ; )
{ ……
}
…...
}while( );
The C Programming Language Chapter 5 Control Flow
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
9 18 27 36 45 54 63 72 81
…………
…..
例 循环嵌套,输出九九表
i
j
/*ch5_5.c*/
#include <stdio.h>
main()
{ int i,j;
for(i=1;i<10;i++)
printf("%4d",i);
printf("\n---------------------------------------\n");
for(i=1;i<10;i++)
for(j=1;j<10;j++)
printf( (j==9)?"%4d\n":"%4d",i*j);
}
The C Programming Language Chapter 5 Control Flow
i<10
printf
F(0)
T(!0)
i=1
j++
j=1
j<10
T(!0)
F(0)
i++
for(i=1;i<10;i++)
for(j=1;j<10;j++)
printf((j==9)?"%4d\n":"%4d",i*j);
outer loop
inner loop
The C Programming Language Chapter 5 Control Flow
5.4 Break and Continue
Break
The break statement provides an
immediate exit from loops (while,
do~while and for ) or switch,
expr
……
break;
……
F(0)
T(!0)
whiledo
……
break;
…...
expr
F(0)
T(!0) while
expr2
……
break;
…...
F(0)
T(!0)
for
expr1
expr3
switch
expr
statement1
break;
statement2
break;
statementn
break;
statement
break;
…...
const 1 const 2 const n default
case
The C Programming Language Chapter 5 Control Flow
例,输出圆面积,面积大于 100时停止
#define PI 3.14159
main()
{
int r;
float area;
for(r=1;r<=10;r++)
{ area=PI*r*r;
if(area>100)
break;
printf("r=%d,area=%.2f\n",r,area);
}
}
break
The C Programming Language Chapter 5 Control Flow
例,小写字母转换成大写字母,直至输入非字母字符
#include <stdio.h>
main()
{
int i,j;
char c;
while(1)
{ c=getchar();
if(c>='a' && c<='z')
putchar(c-'a'+'A');
else
break;
}
}
break
The C Programming Language Chapter 5 Control Flow
Continue
Skips the statements flow the continue,and causes the
next iteration of the loop(for,while,or do~while) to begin;
Applies only to loops,not to switch.
expr
……
continue;
……
F(0)
T(!0)
while
T(!0)
do
……
continue;
…...
expr
F(0)
while
expr2
……
continue;
…...
F(0)
T(!0)
for
expr1
expr3
The C Programming Language Chapter 5 Control Flow
例 求输入的十个整数中正数的个数及其平均值
/*ch5_12.c*/
#include <stdio.h>
main()
{ int i,num=0,a;
float sum=0;
for(i=0;i<10;i++)
{ scanf("%d",&a);
if(a<=0) continue;
num++;
sum+=a;
}
printf("%d plus integer's sum,%6.0f\n",num,sum);
printf("Mean value:%6.2f\n",sum/num);
}