Day4-控制语句

1. 分支语句

1.1 顺序语句,分支语句,循环语句
分支语句
    if()~else~
    switch
循环语句
    for()
    while()~
    do~while()
    goto
辅助控制语句
    continue
    break
    return
1.2 if~else语句的使用
if语句概述
    if(表达式)
        语句块1
    else
        语句块2
常见形式
    简化形式
        if(表达式)语句块  例如: if(x>y) printf("%d",x);
    阶梯形式
        if(表达式1) 语句块1
            else if(表达式2) 语句块2
                else if(表达式3) 语句块3
                    ...
                    else 语句块n
    嵌套形式
        if()
            if() 语句块1
            else    语句块2
        else
            if() 语句块3
            else 语句块4
将百分制成绩转换成等级制成绩,有A,B,C,D四个等级,包含输入检测
​
(demo1): 嵌套形式实现代码
(demo2): 阶梯形式实现代码
1.3 switch语句
多分支选择结构 switch
switch语句的基本形式
    switch(表达式){
        case 常量表达式1:语句块1;break;
        case 常量表达四2:语句块2;break;
        ...
        case 常量表达式n:语句块n;break;
        default: 语句块n+1
    }
switch语句的使用:
    每个常量表达式的值必须各不相同,否则会出现矛盾
    当常量表达式的值与case后面的常量表达式值相等时,执行此case后边的语句
    若没有break结束语句,则将case匹配成功后边的代码全部执行直到switch结束或遇到break语句
    switch中的表达式可以是整型、字符型表达式或枚举
    case常量:只起语句标号的作用
    default:当与所有的case均不匹配时,执行此代码

demo3

2. 循环语句

2.1 for语句
一般形式:
    for(表达式1;表达式2;表达式3){
        ...
    }
执行过程:
    先执行表达式1;
    再执行表达式2,若为真执行循环体,然后再执行表达式3,为假则跳出循环体执行后续代码
注意:
    表达式1可省略,但循环之前应给循环变量赋值
    表达式2可省略,将陷入死循环
    表达式3可省略,但在循环体中增加使循环变量值改变的语句
    表达式省略,但分号‘;’不可省略
​
使用for循环语句实现1到100的累加:demo8
案例:使用for循环打印九九乘法表:demo9
#include <stdio.h>
int main(int argc,char *argv[])
{
    int i,j;
    for(i = 1;i<10;i++){
        for(j = 1;j <= i;j++){
            printf("%d*%d=%d  ",j,i,i*j);
        }
        printf("\n");
    }
    return 0;
}
​
/*
打印结果
linux@ubuntu:~/C/day04$ gcc demo9.c -Wall
linux@ubuntu:~/C/day04$ ./a.out 
//上两行为Ubuntu下编译源代码命令和执行代码命令,以下才为代码执行结果
1*1=1  
1*2=2  2*2=4  
1*3=3  2*3=6  3*3=9  
1*4=4  2*4=8  3*4=12  4*4=16  
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  
*/

2.2 while语句
while语句构成循环:
    基本形式
        while(表达式){
            ....
        }
特点:
    先进行条件判断,再进行循环
​
使用goto跳转语句实现1到100的累加:demo5
2.3 do-while语句
do-while语句:
    基本形式
        do{
            ...
        }while(表达式);
特点:
    先进行循环在进行条件判断,即无论如何都会先执行一次循环体
    
使用goto跳转语句实现1到100的累加:demo6

2.4 goto语句
跳转语句:goto
    当函数有很多个出口,使用goto把这些出口集中到一处是很方便的
优点:
    无条件跳转易于理解
    可以减少嵌套
    可以避免那种忘记更新某一个出口点的问题
    算是帮助编译器做了代码优化
goto语句结构:
    goto 节点标志(开始跳转点)
    ...
    节点标志(跳转到的点,可以在goto的前边也可以在goto的后边)
​
使用goto跳转语句实现1到100的累加:demo4
2.5 程序举例
//打印所有的“水仙花”数。即一个三位数,其各位数字立方和等于该数本身:demo7

#include <stdio.h>
#include <math.h>
int main(int argc,char *argv[])
{
    int i = 100, a, b, c;
    //while
    printf("----------------while-----------\n");
    while(i < 999){ 
        a = i%10;
        b = (i/10)%10;
        c = i/100;
​
        if(a*a*a + b*b*b + c*c*c == i)
            printf("%d+%d+%d = %d\n",c*c*c,b*b*b,a*a*a,i);
        i++;
    }
    
    //do...while
    printf("----------do..while----------\n");
    i = 100;
    do{     
        a = i%10;
        b = (i/10)%10;
        c = i/100;
​
        if(pow(a,3)+pow(b,3)+pow(c,3) == i)
            printf("%d\n",i);
        i++;
    }while(i <= 999);
​
    //for
    printf("------------for------------\n");
    for(i = 100; i <= 999; i++){
        a = i%10;
        b = (i/10)%10;
        c = i/100;
​
        if(pow(a,3)+pow(b,3)+pow(c,3) == i)
            printf("%d\n",i);
    }
    //goto
    printf("----------goto----------\n");
    i = 100;
loop:
        a = i%10;
        b = (i/10)%10;
        c = i/100;
​
        if(pow(a,3)+pow(b,3)+pow(c,3) == i)
            printf("%d\n",i);
        i++;
        if(i<999)
            goto loop;
​
    return 0;
}
​
​
/*
打印结果:
linux@ubuntu:~/C/day04$ gcc demo7.c -Wall -lm
linux@ubuntu:~/C/day04$ ./a.out 
//上两行为Ubuntu下编译源代码命令和执行代码命令,以下才为代码执行结果
----------------while-----------
1+125+27 = 153
27+343+0 = 370
27+343+1 = 371
64+0+343 = 407
----------do..while----------
153
370
371
407
------------for------------
153
370
371
407
----------goto----------
153
370
371
407
*/
/*打印特殊字符(demo10)

F 
_FE 
__FED 
___FEDC 
____FEDCB 
_____FEDCBA
*/
#include <stdio.h>
int main(int argc,char *argv[])
{
    char ch = 'F';
    int i,j;
    for(i = 0;i<6;i++){
        for(j = 0;j<i;j++){
            printf("_");
        }
        ch = 'F';
        for(j = 0;j<=i;j++,ch--){
            printf("%c",ch);
        }
        puts("");
    }
    return 0;
}
​
/*
打印结果:
linux@ubuntu:~/C/day04$ gcc demo10.c -Wall
linux@ubuntu:~/C/day04$ ./a.out 
//上两行为Ubuntu下编译源代码命令和执行代码命令,以下才为代码执行结果
F
_FE
__FED
___FEDC
____FEDCB
_____FEDCBA
​
*/

3. 辅助控制语句

3.1 break语句
用于从循环体内跳出循环体,即提前结束循环
    break只能用在循环语句和switch语句  
/*程序练习:demo11
打印100以内的素数(素数:在大于1的自然数中,除了1和此整数自身外,没法被其他自然数整数的数)
*/
#include <stdio.h>
#include <math.h>
​
int main(int argc,char *argv[])
{
    int n = 100;
    int i,j,m;
    
    printf("2 3 ");
    for(i = 2;i < n ;i++){
        m = (int)sqrt(i);
        for(j = 2;j <= m;j++){
            if(i % j == 0) break;
            if(j == m) printf("%d ",i);
        }
    }
    printf("\n");
    return 0;
}
​
/*
打印结果:
linux@ubuntu:~/C/day04$ gcc demo11.c -Wall -lm
linux@ubuntu:~/C/day04$ ./a.out 
//上两行为Ubuntu下编译源代码命令和执行代码命令,以下才为代码执行结果
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
​
*/
3.2 continue语句
结束本次循环,接着判定下一次是否执行循环
注意continue于break的区别
    continue只结束本次循环,而break终止本层循环(即当且仅当结束一个循环体)
/*
程序练习:
打印10以内,不能被3整除的数
*/
#include <stdio.h>
int main(int argc,char *argv[])
{
    int i = 1;
    for(;i<10;i++){
        if(i % 3 == 0) continue;
        printf("%d ",i);
    }
    puts("");
    return 0;
}
/*
打印结果
linux@ubuntu:~/C/day04$ gcc demo12.c -Wall
linux@ubuntu:~/C/day04$ ./a.out 
//上两行为Ubuntu下编译源代码命令和执行代码命令,以下才为代码执行结果
1 2 4 5 7 8 
*/

3.3 return语句
return语句的一般形式:return(<表达式>)
主要用于终止包含它的函数的执行
若终止的为主函数,则主程序结束
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值