switch case语句与if elseif语句类似,都是从多个选择条件里选取一个来执行。
(一)先来看一个if elseif程序
#include
int main()
{
int number;
printf("Please input an integer between 1~7: ");
scanf("%d", &number);
printf("Today is ");
if(1 == number)
{
printf("Monday!");
}
else if(2 == number)
{
printf("Tuesday!");
}
else if(3 == number)
{
printf("Wednesday!");
}
else if(4 == number)
{
printf("Thursday!");
}
else if(5 == number)
{
printf("Friday!");
}
else if(6 == number)
{
printf("Saturday!");
}
else if(7 == number)
{
printf("Sunday!");
}
else
{
printf("a wrong day! Please input an integer between 1~7!");
}
return 0;
}
运行结果:
Please input an integer between 1~7: 7
Today is Sunday!
(二)再用switch case语句实现相同的功能
#include
int main()
{
int number;
printf("Please input an integer between 1~7: ");
scanf("%d", &number);
printf("Today is ");
switch(number)
{
case 1:
{
printf("Monday!");
break;
}
case 2:
{
printf("Tuesday!");
break;
}
case 3:
{
printf("Wednesday!");
break;
}
case 4:
{
printf("Thursday!");
break;
}
case 5:
{
printf("Friday!");
break;
}
case 6:
{
printf("Saturday!");
break;
}
case 7:
{
printf("Sunday!");
break;
}
default:
{
printf("a wrong day! Please input an integer between 1~7!");
break;
}
}
return 0;
}
运行结果1:
Please input an integer between 1~7: 7
Today is Sunday!
运行结果2:
Please input an integer between 1~7: 10
Today is a wrong day! Please input an integer between 1~7!
注意:
switch case的结构为
switch(表达式或变量或常量)
{
case 条件1:
{
执行语句;
break;
}
case 条件2:
{
执行语句;
break;
}
……
default:
{
执行语句;
break;
}
}
这里“break;”表示中断,若忘了写,程序会继续执行下面的条件。
(三)试试下面这个程序
#include
int main()
{
int number;
printf("Please input an integer between 1~7: ");
scanf("%d", &number);
printf("Today is ");
switch(number)
{
case 1:
{
printf("Monday!");
}
case 2:
{
printf("Tuesday!");
}
case 3:
{
printf("Wednesday!");
}
case 4:
{
printf("Thursday!");
}
case 5:
{
printf("Friday!");
}
case 6:
{
printf("Saturday!");
}
case 7:
{
printf("Sunday!");
}
default:
{
printf("a wrong day! Please input an integer between 1~7!");
}
}
return 0;
}
运行结果:
Please input an integer between 1~7: 3
Today is Wednesday!Thursday!Friday!Saturday!Sunday!a wrong day! Please input an integer between 1~7!
(四)用while实现多次输入
#include
int main()
{
int number;
printf("Please input an integer between 1~7: ");
scanf("%d", &number);
// 循环终止条件为number小于或等于0
while(number > 0)
{
printf("Today is ");
switch(number)
{
case 1:
{
printf("Monday!");
break;
}
case 2:
{
printf("Tuesday!");
break;
}
case 3:
{
printf("Wednesday!");
break;
}
case 4:
{
printf("Thursday!");
break;
}
case 5:
{
printf("Friday!");
break;
}
case 6:
{
printf("Saturday!");
break;
}
case 7:
{
printf("Sunday!");
break;
}
default:
{
printf("a wrong day! ");
break;
}
}//switch
printf("\nPlease input an integer between 1~7: ");
scanf("%d", &number);
}//while
printf("Loop end!\n");
return 0;
}
运行结果:
Please input an integer between 1~7: 7
Today is Sunday!
Please input an integer between 1~7: 5
Today is Friday!
Please input an integer between 1~7: 1
Today is Monday!
Please input an integer between 1~7: 10
Today is a wrong day!
Please input an integer between 1~7: 0
Loop end!
(五)若输入1~7,打印结果并结束程序;否则重新输入
#include
void print_day(int num)
{
switch(num)
{
case 1:
{
printf("Monday!");
break;
}
case 2:
{
printf("Tuesday!");
break;
}
case 3:
{
printf("Wednesday!");
break;
}
case 4:
{
printf("Thursday!");
break;
}
case 5:
{
printf("Friday!");
break;
}
case 6:
{
printf("Saturday!");
break;
}
case 7:
{
printf("Sunday!");
break;
}
default:
{
break;
}
}//switch
}
int main()
{
int number;
printf("Please input an integer between 1~7: ");
scanf("%d", &number);
// 默认循环条件一直为真,遇到break循环才会终止
while(1)
{
if(number >= 1 && number <= 7)
{
printf("Today is ");
print_day(number);
printf("\nProgram end!\n");
break;
}
printf("It is a wrong day!\n");
printf("\nPlease input an integer between 1~7: ");
scanf("%d", &number);
}//while
return 0;
}
运行结果:
Please input an integer between 1~7: 10
It is a wrong day!
Please input an integer between 1~7: 20
It is a wrong day!
Please input an integer between 1~7: 5
Today is a Friday!
Program end!
想了解小朋友学编程可加QQ 307591841 或微信 307591841
关注微信公众号请扫二维码