7-选择结构

一、算法

就是去解决问题所需要的步骤。

 

交换:

 1 #include <stdio.h>  //包含头文件
 2 
 3 int main() //主函数
 4 {
 5     int iNumber1, iNumber2;  //声明3个变量
 6     int temp;  
 7 
 8     iNumber1 = 8; //赋值
 9     iNumber2 = 10;
10  
11     temp = iNumber1; //1 把iNumber1赋值给temp
12     iNumber1 = iNumber2; //2 把iNumber2赋值给iNumber1
13     iNumber2 = temp; //3 把temp赋值给iNumber2
14 
15     printf("%d %d\n",iNumber1,iNumber2); //输出我们需要的结果
16     return 0;
17 }

 

算法分为3种结构:1、顺序结构  2、选择结构  3、循环结构

1顺序结构。

 

 

2、选择结构,(分支结构)

 

 

二、If 语句

 1if  2if....else  3else if

 

1、if   基本用法:   if(表达式){语句}

只要表达式为真,那么就执行语句。

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     if (1 > 5)
 6     {
 7         printf("5是大于1的\n");
 8     }
 9 
10     return 0;
11 }

 

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

选择结构,如果只有一个if 选择是不是少了?

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     int i;
 6 
 7     printf("你喜欢男的还是女的?\n");
 8     printf("输入1表示男\n");
 9     scanf("%d", &i);
10 
11     if (i==1)
12     {
13         printf("你选择了男性\n");
14     }
15     else
16     {
17         printf("你你选择了女性\n");
18     }
19 
20     return 0;
21 }

 

3、else if       if(表达式){语句}else if(表达式){语句}else{语句}

 

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     int i;
 6 
 7     printf("你喜欢男的还是女的?\n");
 8     printf("输入1表示男,输入0表示女。\n");
 9     scanf("%d", &i);
10 
11     if (i==1)
12     {
13         printf("你选择了男性\n");
14     }
15     else if (i==0)
16     {
17         printf("你你选择了女性\n");
18     }
19     else if (i==2)
20     {
21         printf("sorry,去泰国的飞机票我这没有。");
22     }
23     else if (i = 3)
24     {
25 
26     }
27     else if (i == 4)
28     {
29 
30     }
31     else //其他的  11 25 69 8888
32     {
33 
34     }
35 
36     return 0;
37 }

 

注意:1、在写if语句的时候最好尽量一定记得要加上{}  包括后面for(){}

  2、在写if括号里面的表达式时 尽量把常量写在前面

  3、if() 后面一定记得千万千万不要加;(分号)。

4、三目运算符  a>b?a:b;   ?:

!(单目运算符)

  = =+ - * (双目运算符)

?:   if else

a>b  是真还是假  如果是真返回a值,否则返回假值

//从键盘输入两个整数,求出这3个整数的最大数。

 

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     int a, b,c,max2,max3;  //temp 临时变量 用来中一个中间变量
 6 
 7     printf("请输入3个整数,用空格分开");
 8     scanf("%d %d %d",&a,&b,&c);
 9 
10     max2 = a > b ? a : b; //这个时候 已经找到a和b的最大值
11 
12     max3 = max2 > c ? max2 : c; //置换成一个
13 
14     //max3 = a > b ? a : b > c ? a > b ? a : b : c;
15 
16     printf("最大值为%d\n", max3);     
17 
18     a > b ? a : b;
19 
20     //a>b  是真还是假  如果是真返回a值,否则返回假值
21 
22     return 0;
23 }

 

三、switch 语句

//交通灯  红灯停 绿灯行 黄灯等待

 

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     int i;
 6     printf("请输入你选择的灯");
 7 
 8     scanf("%d", &i);
 9      
10     if (i == 0)
11     {
12         printf("绿灯行!");
13     }
14     else if (i == 1)
15     {
16         printf("红灯停!");
17     }
18     else if (i == 2)
19     {
20         printf("黄灯缓行!");
21     }
22     else
23     {
24         printf("输入错误\n");
25     }
26 
27     switch (i)
28     {
29         case 0:
30         printf("绿灯行!\n");
31         break;
32 
33         case 1:
34         printf("红灯停!\n");
35         break;
36 
37         case 2:
38         printf("黄灯缓行!\n");
39         break;
40 
41         default:
42         printf("输入错误!\n");
43         break;
44     }
45 
46     return 0;
47 }

 

//计算两个树之间的差

 1 #include  <stdio.h>
 2 
 3 int main()
 4 {
 5     int i, j, sum;
 6     scanf("%d %d",&i,&j);
 7      
 8     switch (i>j)  //if(表达式)  switch(表达式)
 9     {
10 
11         case 0:
12         sum = j - i;
13         break;
14 
15         case 1:
16         sum = i - j;
17         break;
18 
19         default:
20         break;
21     }
22 
23     printf("%d和%d之间相差%d\n", i, j, sum);
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/tiantiancode/p/11126552.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值