switch case control istruction:- where nesting is not easy in this situation switch case is used.it use 4 keywords switch ,case ,break and default.
syntax:-
switch (expression)
{
case constant_value:code; break;
case constant_value:code; break;
case constant_value:code; break;
default:code;
}
notes:-here expression not in true or false.it is a number
this expression compare with all constant_value if it is match the corresponding code is running, if expression does not match with any case, the default case is run.
rule:-constant_value will be different.
- constant cannot be real.
- if case 2 is match then from case 2 to default all case are executed but not 1, if we want one one case executed then break keyword is used , which end the switch case.
- default can be used any where.
Example:-
Comments
Post a Comment