分支结构程序设计

版权声明:转载请注明 https://blog.csdn.net/qq_34720818/article/details/86632989

分支结构程序设计

前言:
结构化程序设计方法要求一个程序只能有三种基本控制结构组成,任何浮复杂的问题都可以用这三种基本结构去解决。这三种基本机构是顺序结构(上篇博客已经讲解)、选择结构(本篇博客讲解)、循环结构(下篇博客讲解)
1、总体知识点
在这里插入图片描述
2、示例代码:
1、输入一个字符判断其是空格数字还是其他字符

    char ch;
    printf("please input a char :\n");
	ch=getchar();
	if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
		printf("the char is a character\n");
	else if(ch>='0'&&ch<='9')
        printf("the char is a number\n");
	else
		 printf("the char is a tab\n");*/

2、输入一串字符统计数字空格和其他字符的个数

char ch;
	int num=0,character=0,tab=0,i;
    printf("please input a char\n");
	while((ch=getchar())!='\n')
	{
		if(ch==32)
			i=0;//空格
		else if(ch>'0'&&ch<'9')
			i=1;//数字
		else 
			i=2;//其他字符
		switch(i)
		{
		case 0:tab++; break;
		case 1:num++; break;
		case 2:character++; break;
		}
	}
	printf("there are %d number and %d tab and %d character\n",num,tab,character);

3、break和continue用法。break(整个循环终止)和continue(当前一次循环终止)的用法

 //求1-10的偶数项之和
   int sum=0;
    for(int i=0;i<20;i++)
	{
		if(i%2!=0)
			continue;
		if(i>10)
			break;
		sum+=i;
	}
	printf("the sum of even number is %d\n",sum);*/

猜你喜欢

转载自blog.csdn.net/qq_34720818/article/details/86632989