switch语句

switch(expression) {
  case x:
    // codeblock
    break;
  case y:
    // codeblock
    break;
  default:
    // codeblock
}

程序执行到break语句后,会跳出switch,直接执行后面的语句。
下面是一道关于日期对应课程的switch语句,switch会根据day的值,相应执行对应语句。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
include <iostream>
using namespace std;
int main(){
    int day ;
    cout<<"input day:"; cin>>day;
  switch(day)
        {
           case 1: cout<<"Chinese"; break;
           case 2: cout<<"English"; break;
           case 3: cout<<"Music";break;
           case 4:cout<<"Mathematics";break;
           case 5:cout<<"PE";break;
           default:cout<<"wrong number";
        }
return 0;
}

如下程序:
2月份有28天,2月1 日是周一,现输入一个数x,请判断2月x日是周几。
样例输入1:2   样例输出:today is Tuesday
样例输入2:30  样例输出:February have only 28 days  

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
include <iostream>
using namespace std;
int main()
{
     int date;
     cout<<"please input day:"; cin>>date;
     if(date>28||date<1)
     {
     cout<<"February have only 28 days please input again";
     }
    else
    {
         switch(date%7)
         {
               case 1:cout<<"February "<<date<<" is Monday\n";break;
               case 2:cout<<"February "<<date<<" is Tuesday\n";break;
               case 3:cout<<"February "<<date<<" is Wednesday\n";break;
               case 4:cout<<"February "<<date<<" is Thursday\n";break;
               case 5:cout<<"February "<<date<<" is Friday\n";break;
               case 6:cout<<"February "<<date<<" is Saturday\n";break;
               case 0:cout<<"February "<<date<<" is Sunday\n";break;
          } 
     } 
    return 0;
}

switch也可以用 字符判断。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
include
using namespace std;
int main()
{
     char date;
     cout<<"please input char:"; 
     cin>>date;
      switch(date)
     { 
             case 'm':cout<<" Monday\n";break; 
             case 't':cout<<"February "<<date<<" is Tuesday\n";break; 
             case 'w':cout<<"February "<<date<<" is Wednesday\n";break; 
             case 'h':cout<<"February "<<date<<" is Thursday\n";break; 
             case 'f':cout<<"February "<<date<<" is Friday\n";break; 
             case 's':cout<<"February "<<date<<" is Saturday\n";break; 
             case 'u':cout<<"February "<<date<<" is Sunday\n";break; 
             default: cout<<"nothing"; } return 0;
}

计算两个数进行四则运算,如a+b

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){
    int a,b,ans;
    char c;
    cin>>a>>c>>b;
    
    switch (c){
    case '+': ans=a+b; break;
    case '-':  ans=a-b; break;  
    case '*':  ans=a*b; break;  
    case '/':  if(b!=0) ans=a/b; break;  
    default:break;
    }
    cout<<ans;
    return 0;
}

Using range in switch case in C/C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
      int date;
      cout<<"please input int:"; cin>>date;
      switch(date){
      case 1  5:cout<<"first";break;
      case 7  10:cout<<" second";break;
      case 11  22:cout<<"third";break;
      default: cout<<"nothing";
}
return 0;
}
Scroll to Top