The switch statment allows us to select or make a decision from multiple given choices based on a given fixed value. The switch statement transfer the control to a chosen statement within its body.
Write a C program to print given value from 1 to 5 in words, if given value is other than 1 to 5 then print other other than 1 to 5.
#include<stdio.h> int main() { int number;scanf(“%d”,&number); switch(number) case 1: printf(“Number is one\n”); case 2: printf(“Number is two\n”); case 3: printf(“Number is three\n”); case 4: printf(“Number is four\n”); case 5: printf(“Number is five\n”); default: printf(“Number is other than 1 to 5\n”); } } Explanatin :- Suppose you give 5 as input, the output will be :- Suppose you give 10 as input, the output will be :- |
Think what will happen, if break statement will not use in above C program, see below.
#include<stdio.h> int main() { int number;scanf(“%d”,&number); switch(number)
case 1:
} } Explanation :- Suppose you give 5 as input. The compiler jumps to case 5 based on given input as well as compiler execute all of lines after case 5 because here we are not using any break statement the output will be :- Suppose you give 10 as input, the output will be :- |