前言
C语言是一种结构化程序设计语言; C语言的三大结构:顺序结构、分支(选择)结构、循环结构。 艺术来源于生活!!!C语言中的三大结构亦是如此!
一、顺序结构?
C语言中的顺序结构,很简单,就是代码一条一条的按顺序执行,它不会乱执行,而是从上往下依次执行; 这就和我们的人生一样,只会永远的前进,不会后退! 前途是光明的,道路是曲折的!!!! 
二、分支(选择)结构?
我们人生中在一定的条件下,会选择不同的道路; C语言中的分支(选择)结构也是如此,其中它的典型代表就是if语句和switch 语句,接下来,我们就来详细讲讲他们!;
1、if
if语句的基本格式是这样的; if(表达式) { 执行体; } 上代码看看:
int age = 10;
if (age<18)
{
printf("未成年\n");
}
只要表达式里面的结果为真,那么{ }里面的语句就会被执行; 运行截图:  如果我们把age改为20,条件就会不成立,不成立的话,括号里面的值就为假,为假就不会执行; 对于if语句来说:执行的步骤是:1、先判断表达式的值;2、若表达式为真则执行{}里面的执行体;若为假就不会执行; 那什么又为假,什么又为真呢?,在C语言中,我们把0叫做假,非0叫做真; 当然与if配对的还有else else的话就不需要写条件了,与if()括号里面的条件相反的就是else的条件了,不需要写出来,写出来也会报错,当然在情况已经讨论完的情况下可以不写else,也就是说可以没有else,不能没有if; 示意图:  
当然,一般情况下我们if语句的循环体都是需要加花括号括起来的,这样有利于养成良好代码风格;但是你不家的话,if语句就会和离他最近的一条语句匹配,如果它们之间有空格,也可以匹配成功; 示意图:  值得注意的是,千万别再if语句的后面加 ;号,因为这也会被认为是一条语句,只不过是空语句罢了;   当然if语句并不是仅支持if和else两个分支,它支持多个分支,这多个分支就需要else if()连接起来;比如这样:   那么我们再来看看else的匹配问题: else的匹配问题遵循就近原则;else它会与离他最近的if或else if语句匹配; 示意图:
int main()
{
int a = 0;
int b = 3;
int c = 2;
if (a == 1)
if (b == 3)
printf("you can see me!\n");
else if (c == 2)
printf("hahaha\n");
else
printf("you not can see me!\n");
return 0;
}
这个else并没有和if匹配,根据就近原则,else和第二个else if相匹配; 因此当if语句不满足时,是不会进去的,既然都不会进去了,那自然也就不会执行任何一条语句,那么最后程序就会什么都不执行:  改变一下条件,就能看到语句执行了: 
switch
既然有了if语句为什么还会存在switch语句呢,主要是因为在某些方面,switch语句比if语句更加方便; switch语句用的也很广泛: 比如说我们输入一个数字,然后判断是星期几; 就可以用switc语句:  好像效果确实如此; 我们再来输个4试试: 
哎哟,情况并不是我们想的那样:它进确实是从case 4:哪里进的,但是出好像并没有从本次跳出去,而是从最后跳出去的,为了解决这个问题,我们就需要用break来把这个问题解决了;  这次我们如果输入9呢? 
我们可以看到什么也没有,虽然达到了预期效果,但是站在顾客的角度来说,是不太友好的;我们应该给顾客一个反馈: 加个default就可以了,default表示其它情况;  default的位置可以随便放; 当然,我们发先,在每条语句后面似乎都加了一个break,那么可不可以不加呢?这个根据具体场景具体分析,向我这道题,我们是根据数字,判断是具体到星期几,因此每条语句都要加break;但是我们如果是判断是否为工作日,就不用: 
同时我们应注意到 case 后面只能跟常量、#define定义的宏、枚举常量,不能跟变量,哪怕是const修饰的常变量也不行!!!! 
三、循环结构?
C语言的循环主要分为 while循环、for循环、do while循环;
1、while循环
//while 语法结构 while(表达式) 循环语句;  例如我们想在屏幕上输出1~10的数字,我们就可以利用while循环
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0; }
 那么这时候如果我遇到了5就不想往后打了,该怎么办呢? 这时候break作用又来了,在循环语句中,break可以终止掉一个循环 while中的break是用于永久终止循环的 这时我们的代码就可以这样写:
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 5)
break;
printf("%d ", i);
i = i + 1;
}
return 0;
}
 那如果这时候我又改需求了,我就是像跳过5,但是后面的数又想继续打印,这时候另一个关键字continue的作用就来了; continue:跳过continue后面的所有语句,直接来到判断语句(while循环),  但是千万要注意continue的用法,不要把更新条件放在continue后面,不然容易造成死循环;
2、for循环
//for(表达式1; 表达式2; 表达式3) 循环语句; 表达式1 表达式1为初始化部分,用于初始化循环变量的。 表达式2 表达式2为条件判断部分,用于判断循环时候终止。 表达式3 表达式3为调整部分,用于循环条件的调整。  我们现在利用for循环打印以下1~10;  我们再来看看break在for循环中的作用;  我们再来看看continue在for循环中的作用;  我们通过上述for循环流程图可以发现,cotinue并没有跳到判断部分,而是跳到了调整部分,这要注意与while区别: 一些for循环的变形:
#include <stdio.h>
int main()
{
for(;;)
{
printf("hehe\n");
}
题。
int i = 0;
int j = 0;
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
printf("hehe\n");
}
}
int i = 0;
int j = 0;
for(; i<10; i++)
{
for(; j<10; j++)
{
printf("hehe\n");
}
}
int x, y;
for (x = 0, y = 0; x<2 && y<5; ++x, y++)
{
printf("hehe\n");
}
return 0; }
3、do while循环
do 循环语句; while(表达式);  我们现在利用for循环打印以下1~10;  我们再来看看break在for循环中的作用;  直接终止掉整个循环(是永久性的)
我们再来看看continue在for循环中的作用;  continue在do while中的用法与在while中一样,都是跳到判断条件; 我们也应注意控制号调整条件的位置,避免造成死循环;
|