INTERODUCTION:-
To select from multiple branching , apart from using nested if-else statements, we can use another special type of construct . The particular construct successively tests the value of an expression or variable against a list of integers or character constants. When a match is found the statement associated with that constant that constant are executed.
FLOWCHART:-
syntax:-
switch(integer_or_charecter_variable)
{ case constant 1: statements;
case constant 2: statements;
case constant n: statements;
default: statements;
}
The following features of the switch-case – default structure should be noted:
- One can use only integer or character values for the case constants. Floating point variables are not allowed as a case constant.
- One can have only equality of cases and we cannot have any other inequality operation ( like > , <etc.)
- Each case constant is followed by a colon (:)
- There can be multiple statements following a case constant and these statements need nod not be enclosed within a pair of curly brackets { }
- The default statements is optional and can be placed anywhere within the switch block.
- After entering the switch block each case constant is matched with the switch variable. If a match is not found with a case constant , then the control skip next case constant.
- As soon as the switch value matches with a case constant, the statements following that case , and all statements associated with subsequent cases along with the default statement get exectuted.
- To execute only the statements following a particular case , a break statement is included after the statements following the case constant . Once a case is satisfied the controls simply falls through the subsequent statements till it reaches a break statement.
- A switch – case default structure is generally used to write amenu driven program, where each option of the menu corresponds to a case constant.
Great content
LikeLike
Awesome content.
LikeLike
Great content 🙂 Thank you
LikeLike