C语言基础知识-程序流程结构

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.概述

C语言支持最基本的三种程序运行结构:顺序结构,选择结构,循环结构。
  顺序结构:程序按顺序执行,不发生跳转。
  选择结构:依据是否满足条件,有选择的执行相应功能。
  循环结构:依据条件是否满足,循环多次执行某段代码。

 

二.选择结构

1>.if语句

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#include <stdio.h>

int main(void)
{
    int a = 100;
    int b = 20;
    if (a > b)
    {
        printf("a > b\n");
    }

    return 0;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo if_demo.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo 
a > b
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

2>.if ... else语句

 C语言基础知识-程序流程结构

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo2.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#include <stdio.h>

int main(void)
{
    int a = 100;
    int b = 200;
    if (a > b)
    {
        printf("a > b\n");
    }
    else
    {
        printf("a < b\n");
    }

    return 0;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo2 if_demo2.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo2 
a < b
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

3>.if ... else if ...else语句

C语言基础知识-程序流程结构

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo3.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#include <stdio.h>

int main(void)
{
    int a = 300;
    int b = 300;
    if (a > b)
    {
        printf("a > b\n");
    }
    else if(a == b)
    {
        printf("a == b\n");
    }
    else
    {
        printf("a < b\n");
    }

    return 0;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo3 if_demo3.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo3 
a == b
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

4>.三目运算符【其实其内部判断条件和if相似,语法结构简单明了

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo4.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#include <stdio.h>

int main(void)
{
    int a = 100;
    int b = 300;
    int max;

    if (a > b)
    {
        max = a;
    }
    else
    {
        max = b;
    }
    printf("s1 = %d\n",max);

    a = 10;
    b = 20;
    max = (a > b ? a:b);      //上面一大堆代码,我们仅仅用三目运算符一行简写。三目运算符格式为"表达式?选项1[表达式]:选项2",即如果表达式为真,选择选项1的结果,如果为假则选择新选项2。
    printf("s2 = %d\n",max);

    return 0;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo4 if_demo4.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo4 
s1 = 300
s2 = 20
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

5>.switch语句【注意:if 条件语句执行效率差,switch条件语句执行效率相对较高,但是if可以判断一个区间,而switch则只能用来判断一个值

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat switch_demo.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#include <stdio.h>

int main(void)
{
    char c;
    c = getchar();    //注意该方法只会接收第一个字符哟~比如你输入的是100,它只会接收第一个字符“1”

    switch(c)    //参数只能是整型变量
    {
    case '1':
        printf("OK\n");
        break;    //switch遇到break就中断了
    case '2':
        printf("not OK\n");
        break;    
    default:    //如果上面的条件都不满足,那么执行default
        printf("are u OK?\n");
    }

    return 0;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o switch_demo switch_demo.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 
1
OK
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 
2
not OK
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 
3
are u OK?
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 
5
are u OK?
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

 

三.循环结构

1>.while语句

 C语言基础知识-程序流程结构

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat while_demo.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int a = 1;
    while(a < 10)
    {
        printf("a = %d\n",a);
        a++;
        system("sleep 0.5");
    }
    printf("程序执行完毕~\n");
    
    return EXIT_SUCCESS;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o while_demo while_demo.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./while_demo 
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
程序执行完毕~
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

2>.do ... while语句

 C语言基础知识-程序流程结构

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat narcissus.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int index = 100;
    do
    {
        int one = 0, ten=0, hundred=0;  //将一个三位数分解个位,十位,百位
        
        hundred = index / 100;        //百位

        ten = index / 10 % 10;        //十位

        one = index % 10;             //个位
        
        if (hundred * hundred * hundred + ten * ten * ten + one * one * one == index)    //各个位数的立方和等于该数本身,那么它就是一个水仙花
        {
            printf("%d是水仙花数\n",index);
        }
        
        index ++;
    }while(index < 1000);

    return EXIT_SUCCESS;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o narcissus narcissus.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./narcissus 
153是水仙花数
370是水仙花数
371是水仙花数
407是水仙花数
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 

3>.for循环

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int index = 100;
    int sum = 0;
    
    for(index = 0;index<=100;index++)    //计算0-100之间所有数字的是总和
    {
        sum += index;
    }
    
    printf("sum = %d\n",sum);
    
    return EXIT_SUCCESS;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo for_demo.c 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo 
sum = 5050
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo2.c 
/*
@author :yinzhengjie
blog:http://www.cnblogs.com/yinzhengjie
EMAIL:y1053419035@qq.com
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    for(int i = 100;i<1000;i++)
    {
        int one = 0, ten=0, hundred=0;  //将一个三位数分解个位,十位,百位
        
        hundred = i / 100;        //百位

        ten = i / 10 % 10;        //十位

        one = i % 10;             //个位
        
        if (hundred * hundred * hundred + ten * ten * ten + one * one * one == i)    //各个位数的立方和等于该数本身,那么它就是一个水仙花
        {
            printf("%d是水仙花数\n",i);
        }
    }
    return EXIT_SUCCESS;
}
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo2  for_demo2.c -std=c99         #注意,在Linux系统我们编译for循环代码时需要指定"-std=c99",否则会报错"error: ‘for’ loop initial declarations are only allowed in C99 mode"
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo2 
153是水仙花数
370是水仙花数
371是水仙花数
407是水仙花数
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# 
使用for循环打印三位数存在的水仙花数字

相关文章: