07程序语句

程序语句


5.1判断语句

5.1.1if(表达式) {语句}

#include <stdio.h>

int main(){

    printf("请输入2个数字:");

    int a,b;

    scanf("%d",&a);

    scanf("%d",&b);

    if(a>b){

       printf("输入的第一个数字较大.\n");

    }

    printf("%d %d",a,b);

    return 0;

}

5.1.2 if(表达式) {语句1} else {语句2}

5.1.3 if(表达式1) {语句1}

else if(表达式2){语句2}

else if(表达式n){语句n}

else {语句n+1}

#include <stdio.h>

int main(){

    printf("请输入2个数字:");

    int a,b;

    scanf("%d",&a);

    scanf("%d",&b);

    if(a>b){

       printf("输入的第一个数字较大.\n");

    }else{

       printf("输入的第二个数字较大.\n");

    }

    printf("%d %d",a,b);

    return 0;

}

5.1.4if语句嵌套

5.1.5switch多分支选择语句

switch(表达式){

    case 常量1:语句1;

case 常量2:语句2;

:   :   :

case 常量n:语句n;

default:语句n+1;

}

5.2循环语句

5.2.1while(表达式) {执行语句}

5.2.2do {执行语句} while(表达式)

5.2.3for(循环变量赋初值;循环条件;循环变量增值) {执行语句}

5.3跳转语句

5.3.1break

遇到break语句后,程序将立刻退出循环。

5.3.2continue

跳过本次循环

5.3.3goto

无条件转移语句

 

#include <stdio.h>

int main(){

    int aa = 0;

    while(aa < 10){

       if(aa==5){

           goto enough;

       }

       printf("执行循环\n");

       aa++;

    }

    enough:

       printf("执行GOTO\n");

    return 0; 

}

 

#include <stdio.h>

int main(){

    int aa = 0;

    enough:

       printf("执行GOTO\n");

    while(aa < 10){

       if(aa==5){

           goto enough;

       }

       printf("执行循环\n");

       aa++;

    }

    return 0; 

}

 

5.3.4return

从函数返回语句

猜你喜欢

转载自www.cnblogs.com/Aha-Best/p/10911322.html