c语言while语句里if,C语言中流程控制语句(if,switch,for,while,do-while,continue,break,return)...

条件语句

在Turbo C2.0中条件语句的一般形式为:

if(表达式)

语句1;

else

语句2;

上述结构表示: 如果表达式的值为非0(TURE)即真, 则执行语句1, 执行完语 句1从语句2后开始继续向下执行;

如果表达式的值为0(FALSE)即假, 则跳过语句1而执行语句2。

注意:

1. 条件执行语句中"else 语句2;"部分是选择项,

可以缺省, 此时条件语句变成:

if(表达式)

语句1;

表示若表达式的值为非0则执行语句1 , 否则跳过语句1继续执行。

2. 如果语句1或语句2有多于一条语句要执行时,

必须使用"{"和"}"

把这些语句包括在其中, 此时条件语句形式为:

if(表达式)

{

语句体1;

}

else

{

语句体2;

}

3. 条件语句可以嵌套, 这种情况经常碰到, 但条件嵌套语句容易出错, 其原因主要是不知道哪个if对应哪else。

例如:

if(x>20||x

if(y<=100&&y>x)

printf("Good");

else

printf("Bad");

对于上述情况, Turbo C2.0规定: else语句与最近的一个if语句匹配,

上例中的else与if(y<=100&&y>x)相匹配。为了使else与if(x>20||x

必须用花括号。如下所示:

if(x>20||x

{

if(y<=100&&y>x)

printf("Good");

}

else

printf("Bad");

4. 可用阶梯式if-else-if结构。

阶梯式结构的一般形式为:

if(表达式1)

语句1;

else if(表达式2)

语句2;

else if(表达式3)

语句3;

.

.

else

语句n;

这种结构是从上到下逐个对条件进行判断, 一旦发现条件满点足就执行与它有关的语句, 并跳过其它剩余阶梯; 若没有一个条件满足,

则执行最后一个else语句n。最后这个else常起着"缺省条件"的作用。

同样, 如果每一个条件中有多于一条语句要执行时,

必须使用"{"和"}"把这

些语句包括在其中。

switch语句

在编写程序时, 经常会碰到按不同情况分转的多路问题, 这时可用嵌套if-else-fi语句来实现,

但if-else-if语句使用不方便, 并且容易出错。对这种情况, Turbo C2.0提供了一个开关语句。

开关语句格式为:

switch(变量)

{

case 常量1:

语句1或空;

case 常量2:

语句2或空;

.

.

case 常量n;

语句n或空;

default:

语句n+1或空;

}

执行switch开关语句时, 将变量逐个与case后的常量进行比较, 若与其中一个相等, 则执行该常量下的语句,

若不与任何一个常量相等, 则执行default 后面的语句。

注意:

1. switch中变量可以是数值, 也可以是字符。

2. 可以省略一些case和default。

3. 每个case或default后的语句可以是语句体,

但不需要使用"{"和"}"括起来。

下例的switch中变量为整数型。

main()

{

int test;

for(test=0; test<=10; test++)

{

switch(test)

{

case 1:

printf("%d\n", test);

break;

case 2:

printf("%d\n", test);

break;

case 3:

printf("%d\n", test);

break;

default:

puts("Error");

break;

}

}

}

下例的switch中变量为字符型。

#include

main()

{

char c;

while(c!=27)

{

c=getch();

switch(c)

{

case 'A':

putchar(c);

break;

case 'B':

putchar(c);

break;

default:

puts("Error");

break;

}

}

}

for循环

for循环是开界的。它的一般形式为:

for(;

;

)

语句;

(1)初始化总是一个赋值语句, 它用来给循环控制变量赋初值;

(2) 条件表达式是一个关系表达式, 它决定什么时候退出循环;

(3)

增量定义循环控制变量每循环一次后按什么方式变化。这三个部分之间用";"分开。

例如:

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

语句;

上例中先给i赋初值1, 判断i是否小于等于10, 若是则执行语句, 之后值增加1。再重新判断, 直到条件为假,

即i>10时, 结束循环。

注意:

1. for循环中语句可以为语句体,

但要用"{"和"}"将参加循环的语句括起来。

2.

for循环中的"初始化"、"条件表达式"和"增量"都是选择项,

即可以缺省, 但";"不能缺省。省略了初始化,

表示不对循环控制变量赋初值。省略了条件表达式, 则不做其它处理时便成为死循环。省略了增量, 则不对循环控制变量进行操作,

这时可在语句体中加入修改循环控制变量的语句。

3. for循环可以有多层嵌套。

例16:

main()

{

int i, j, k;

printf("i

j k\n");

for (i=0;

i<2; i++)

for(j=0;

j<2; j++)

for(k=0;

k<2; k++)

printf(%d %d

%d\n", i, j, k);

}

while循环与do-while 循环

while循环的一般形式为:

while(条件)

语句;

while循环表示当条件为真时, 便执行语句。直到条件为假才结束循环。并继续执行循环程序外的后续语句.

例17:

#include

main()

{

char c;

c='\0';

while(c!='\X0D')

c=getche();

}

上例中, while循环是以检查c是否为回车符开始, 因其事先被初始化为空,所以条件为真, 进入循环等待键盘输入字符;

一旦输入回车, 则c='\X0D', 条件为假, 循环便告结束。

与for循环一样, while循环总是在循环的头部检验条件, 这就意味着循环可能什么也不执行就退出。

注意:

1. 在while循环体内也允许空语句。

例如:

while((c=getche())!='\X0D');

这个循环直到键入回车为止。

2. 可以有多层循环嵌套。

3. 语句可以是语句体,

此时必须用"{"和"}"括起来。

例18:

#include

main()

{

char c, fname[13];

FILE *fp;

printf("File name:");

scanf("%s", fname);

fp=fopen(fname, "r");

while((c=fgetc(fp)!=EOF)

putchar(c);

}

do-while 循环

do-while 循环的一般格式为:

do

语句;

while(条件);

这个循环与while循环的不同在于: 它先执行循环中的语句, 然后再判断条件是否为真, 如果为真则继续循环; 如果为假,

则终止循环。因此, do-while循环至少要执行一次循环语句。同样当有许多语句参加循环时,

要用"{"和"}"把它们括起来。

continue 语句

continue语句的作用是跳过循环本中剩余的语句而强行执行下一次循环。

continue语句只用在for、while、do-while等循环体中, 常与if条件语句一起使用,

用来加速循环。

main()

{

char c;

while(c!=0X0D)

{

c=getch();

if(c==0X1B)

continue;

printf("%c\n", c);

}

}

break语句

break语句通常用在循环语句和开关语句中。当break用于开关语句switch中时,

可使程序跳出switch而执行switch以后的语句; 如果没有break语句, 则将成为一个死循环而无法退出。

当break语句用于do-while、for、while循环语句中时, 可使程序终止循环而执行循环后面的语句,

通常break语句总是与if语句联在一起。 即满足条件时便跳出循环。

main()

{

int i=0;

char c;

while(1)

{

c='\0';

while(c!=13&&c!=27)

{

c=getch();

printf("%c\n", c);

}

if(c==27)

break;

i++;

printf("The No. is %d\n", i);

}

printf("The

end");

}

注意:

1. break语句对if-else的条件语句不起作用。

2. 在多层循环中, 一个break语句只向外跳一层。

return语句

return语句的一般格式为:

return表达式;

函数的返回值是由函数体中的return语句实现返回的。

return语句一般放在函数体的最后,用于结束函数的执行,返回调用函数。若它带有表达式(此表达式可以用一对小括号括起来),系统会将它转化为在函数头中定义的类型。因而要求表达式的类型与定义中的函数值类型一致。若一个不带表达式的return语句放在函数的最后,则可省略。

一个函数中可以有多个return语句,但只能有一个return语句起作用。

The C return Statement

The return statement terminates the execution of a

function and returns control to the calling function. Execution

resumes in the calling function at the point immediately following

the call. A return statement can also return a value to the

calling function. See Return

Type for more information.

jump-statement:

return expression opt ;

The value of expression, if present, is returned to the

calling function. If expression is omitted, the return value

of the function is undefined. The expression, if present, is

converted to the type returned by the function. If the function was

declared with return type void, a return statement

containing an expression generates a warning and the expression is

not evaluated.

If no return statement appears in a function definition,

control automatically returns to the calling function after the

last statement of the called function is executed. In this case,

the return value of the called function is undefined. If a return

value is not required, declare the function to have void

return type; otherwise, the default return type is int.

Many programmers use parentheses to enclose the

expression argument of the return statement. However,

C does not require the parentheses. (parentheses圆括号)

This example demonstrates the return statement:

void draw( int I, long L );

long sq( int s );

int main()

{

long y;

int x;

y = sq( x );

draw( x, y );

return();

}

long sq( int s )

{

return( s * s );

}

void draw( int I, long L )

{

return;

}

In this example, the main function calls two functions:

sq and draw. The sq function returns the

value of x * x to main, where the return value is

assigned to y. The draw function is declared as a

void function and does not return a value. An attempt to

assign the return value of draw would cause a diagnostic

message to be issued.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值