L14 Switch
L14 Switch
Dr.P.Durgadevi,Assistant Professor(S.G)
The switch Statement
Topics
Multiple Selection
switch Statement
Unconditional Control Statement
In C, an unconditional statement is a
statement that is executed without any
condition or consideration of whether a
particular condition is true or false.
Unconditional statements are executed
sequentially, one after the other, and they do
not depend on any conditional logic.
Multiple Selection
...
default: :
statement(s)
break ;
}
switch Statement Details
The last statement of each case in the switch
should almost always be a break.
The break causes program control to jump to
the closing brace of the switch structure.
Without the break, the code flows into the next
case. This is almost never what you want.
A switch statement will compile without a
default case, but always consider using one.
Good Programming Practices
Use
scanf (“%c”, &ch) ;
to read a single character into the variable ch. (Note
that the variable does not have to be called “ch”.”)
Use
printf(“%c”, ch) ;
to display the value of a character variable.
char Example
#include <stdio.h>
int main ( )
{
char ch ;