Sample


[br]5 [br]When you want to test a variable against a lot of values, <strong>it's a waste of time to write an if/else if/else statement for each possibility</strong>. <strong>The switch statement does it for you.</strong> You insert the variable you want to test between its brackets and then you write a <strong>case</strong> statement for each value you want to test. [crayon-5daf777c51dea196785784/] The <code>break</code> keyword is <strong>foundamental</strong>, because it tells the program to exit the switch statement. If you forget it, your program will start executing every case after the right one. The <code>default</code> statement is basically the <strong>else statement of switch</strong>. [br]https://itszephyro.com/blog/learn-c/c-switch/[br]#include <stdio.h> int main() { int var; printf("Enter a number: "); scanf("%d", &var); switch(var) { case 0: printf("A"); break; case 1: printf("B"); break; case 2: printf("C"); break; case 3: printf("D"); break; default: printf("None"); } } [br]

[br]5
[br]When you want to test a variable against a lot of values, it’s a waste of time to write an if/else if/else statement for each possibility.

The switch statement does it for you.

You insert the variable you want to test between its brackets and then you write a case statement for each value you want to test.

The break keyword is foundamental, because it tells the program to exit the switch statement.

If you forget it, your program will start executing every case after the right one.

The default statement is basically the else statement of switch.
[br]https://itszephyro.com/blog/learn-c/c-switch/[br]#include

int main() {
int var;
printf(“Enter a number: “);
scanf(“%d”, &var);
switch(var) {
case 0:
printf(“A”);
break;
case 1:
printf(“B”);
break;
case 2:
printf(“C”);
break;
case 3:
printf(“D”);
break;
default:
printf(“None”);
}
}
[br]

To learn more, visit: