c语言学习笔记(4)选择结构程序

C语言学习笔记(4)选择结构程序

本次学习C语言结构程序中重要的结构程序之一——选择结构,是构成C语言程序的重要语句结构,在学习时,我也遇到了相应的问题,将在文中提出,并将学习的心得感悟写在博客内,欢迎讨论。

if语句

if语句的基本形式

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

及对表达式中的条件进行判断,为真则运行语句1,为假则运行语句2

想提醒同为初学者的各位一定要注意书写格式


运算符

&&——且

||——或

!——非

且条件运算符的优先级高于赋值运算符但低于关系运算符和算数运算符


其中if语句还可以嵌套使用
嵌套使用1

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

嵌套使用2

if(表达式1)
	if(表达式1.1)
		{
		语句1.1
		}
	else
		{
		语句1.2
		}
else	
	if(表达式2.1)
		{
		语句2.1
		}
	esle
		{
		语句2.2
		}	
	

提醒:不要漏掉分号

嵌套使用3

if(表达式1)
	语句1
if(表达式2)
	语句2
if(表达式3)
	语句3	

switch语句

switch(表达式)
	{

	case 常量表达式1:语句1;break;

	case常量表达式2:语句2;break;

	……

	case常量表达式n:语句n;break;

	default:语句n+1;
	}

需要注意的是

switch中的表达式需要为整型表达式,字符表达式或枚举型表达式的一种

case 和常量i间需要一个空格间隔

break的主要作用是执行此条语句后就跳出switch语句(可不写)

default是当表达式的值与所有的case后的常量表达式都不符的时候,则实行default后的语句

以下是我在尝试编程是遇到的困难,欢迎参与讨论
在程序输入中AB将会被识别成为A
AB应为字符串并由相应的函数定义和读取



#include <stdio.h>

void main()                                  
{
	int type1,type2;

    	char*Fbloodtype;
    	char*Mbloodtype;

      	printf("Please father and mother's bloodtypes(A or B or O or AB):\n");   //输入父母血型

	scanf("%s%s", &Fbloodtype , &Mbloodtype);

	{
      	if(Fbloodtype== "A")
      	

            	type1= 1;

      	else

   	if(Fbloodtype== "B")

            	type1= 2;

      	else

    	if(Fbloodtype== "AB")

            	type1= 3;

      	else

      	if(Fbloodtype== "O")

           	type1= 4;

      	else	
	}

       	printf("Invaild data1!\n"); 
       
	{
      	if(Mbloodtype== "A")

            	type2= 1;

      	else

      	if(Mbloodtype== "B")

           	type2= 2;

      	else

      	if(Mbloodtype== "AB")

            	type2= 3;

      	else

      	if(Mbloodtype=="O")

            	type2= 4;

      	else
      	}

          printf("Invaild data2!\n");      //对父母的血型进行赋值

      	if((type1>=1 && type1 <= 4) && (type2 >= 1 && type2<= 4))

      {

            

            switch(type1* 10 + type2)                     //将父母血型对应数字放置于二位数的十位和个位

 

            {

            	case11 : printf("Child's blood type is A or O\n");break;

            	case12 : printf("Child's blood type is A or B or O or AB\n");break;

            	case13 : printf("Child's blood type is A or B or AB\n");break;

            	case14 : printf("Child's blood type is A or O\n");break;

            	case21 : printf("Child's blood type is A or B or O or AB\n");break;

            	case22 : printf("Child's blood type is B or O\n");break;

           	case23 : printf("Child's blood type is A or B or AB\n");break;     

            	case24 : printf("Child's blood type is B or O\n");break;

            	case31 : printf("Child's blood type is A or B or AB\n");break;

            	case32 : printf("Child's blood type is A or B or AB\n");break;     

            	case33 : printf("Child's blood type is A or B or AB\n");break;

            	case34 : printf("Child's blood type is A or B\n");break;

            	case41 : printf("Child's blood type is A or O\n");break;

            	case42 : printf("Child's blood type is B or O\n");break;

            	case43 : printf("Child's blood type is A or B\n");break;

            	case44 : printf("Child's blood type is O\n");break;

            }

      }

      else printf("Invailddata3!\n");

}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗头狗不狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值