C Tutorial/Statement/Switch statement
Содержание
- 1 break statement stops evaluating any further case statements.
- 2 Confused if statement
- 3 In the absence of a break statement, all statements that are followed by matched cases are executed.
- 4 Nest if statement in case statement
- 5 The switch statement
- 6 Two cases, one action
- 7 Use switch structure to evaluate characters
- 8 Use switch to simplify the logic
- 9 Use the switch structure to evaluate a user"s response from a menu (without break).
break statement stops evaluating any further case statements.
<source lang="cpp">#include <stdio.h> main(){
int iResponse = 0; printf("\nPlease select a category (1-4): "); scanf("%d", &iResponse); switch (iResponse) { case 1: printf("\nYou selected 1\n"); break; case 2: printf("You selected 2\n"); break; case 3: printf("You selected 3\n"); break; case 4: printf("You selected 4\n"); break; }
}</source>
Please select a category (1-4): 2 You selected 2
Confused if statement
<source lang="cpp">#include <stdio.h>
- include <stdbool.h>
int main(void) {
int age = 24; int college = 1; int subject = 2; bool interview = false;
if((age>25 && subject==1) && (college==3 || college==1)) interview = true; if(college==2 &&subject ==1) interview = true; if(college==1 && subject==2 && !(age>28)) interview = true; if(college==2 && (subject==2 || subject==3) && age>25) interview = true; if(interview) printf("\n\nGive "em an interview"); else printf("\n\nReject "em"); return 0;
}</source>
Give "em an interview
In the absence of a break statement, all statements that are followed by matched cases are executed.
<source lang="cpp">#include <stdio.h> main(){
int i= 6; switch(i%2) { case 0 : printf("the number %d is even \n",i); case 1 : printf("the number %d is odd \n",i); break; }
}</source>
the number 1 is odd the number 2 is even the number 3 is odd the number 4 is even
Nest if statement in case statement
<source lang="cpp">#include <stdio.h>
- include <ctype.h>
int main(void) {
double number1 = 3.0; double number2 = 4.0; char operation = "+"; char reply = 0;
switch(operation) { case "+": printf("= %lf\n", number1 + number2); break; case "-": printf("= %lf\n", number1 - number2); break; case "*": printf("= %lf\n", number1 * number2); break; case "/": if(number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %lf\n", number1 / number2); break; case "%": if((long)number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %ld\n", (long)number1 % (long)number2); break; default: printf("\n\n\aIllegal operation!\n"); break; } return 0;
}</source>
= 7.000000
The switch statement
To take one of a number of possible actions.
switch is preferred over multiple if...else statements.
The general form of a switch statement is
<source lang="cpp">switch(switch_expr)
{ case constant expr1 : S1; S2; break; case constant expr1 : S3; S4; break; ..... default : S5; S6; break; }</source>
Two cases, one action
<source lang="cpp">#include <stdio.h> int main(void) {
char answer = 0; printf("Enter Y or N: "); scanf(" %c", &answer); switch (answer) { case "y": case "Y": printf("\nYou responded in the affirmative."); break; case "n": case "N": printf("\nYou responded in the negative."); break; default: printf("\nYou did not respond correctly..."); break; } return 0;
}</source>
Enter Y or N: N You responded in the negative.
Use switch structure to evaluate characters
<source lang="cpp">#include <stdio.h> main(){
char iResponse = "A"; printf("input(a or A or B or b or c or C):"); scanf("%d", &iResponse); switch (iResponse) { case "a": case "A": printf ("\nYou selected the character a or A\n"); break; case "b": case "B": printf("You selected the character b or B\n"); break; case "c": case "C": printf("You selected the character c or C\n"); break; }
}</source>
Use switch to simplify the logic
<source lang="cpp">#include <stdio.h> int main(void) {
int choice = 2;
if((choice>10) || (choice <1)){ choice = 11; } switch(choice) { case 7: printf("\nCongratulations!"); break; case 2: printf("\nA"); break; case 8: printf("\nB"); break; case 11: printf("\nC"); default: printf("\nSorry, you lose.\n"); break; } return 0;
}</source>
A
<source lang="cpp">#include <stdio.h> main(){
int iResponse = 0; printf("\nPlease select a category (1-4): "); scanf("%d", &iResponse); switch (iResponse) { case 1: printf("\nYou selected 1\n"); case 2: printf("You selected 2\n"); case 3: printf("You selected 3\n"); case 4: printf("You selected 4\n"); } //end switch
} //end main function</source>
Please select a category (1-4): 1 You selected 1 You selected 2 You selected 3 You selected 4