Switch Statement in C
Switch Statement in C
Switch Statement Program in C
Hello Friends,
Today we will learn Switch Statement.
Switch Statement is used to make a choice from a number of options.It requires only one argument of any datatype which is checked with the number of case options.If the value matches with case constant then the particular case statement is executed otherwise default statement is executed.Every case statement is terminated with ':' . The break statement is used to exit from current case statement.
In switch statement,the variable or expression must be a character ('a','b') or an integer(1,2,3).
switch expression should not be terminated with a semicolon(;) or any other symbol.
switch statement can also be written without default statement but break statement is mandatory with all cases.If no break statements are given all the cases would be executed.
The case keyword must contain constant value (i.e integer or character) with a colon(:).
lets take a simple example
A program to perform addition,subtraction,multiplication and division with switch statement.
output:
lets take some more examples of switch statement.
A program to use nested if else statement in the switch statement to display ASCII character.
A program to convert years into minutes,hours,days,months or seconds using switch statement.
Points to remember
* No two case statements have identical constant in the same switch.
* In switch statement nested if can be used.
* In switch statement float values or float variable can not be used.
* Only constant values are used with Switch statement.Logical and Relational Operators cannot be used .
No comments:
Post a Comment