Skip to main content

Switch, Case, Default Statements In C++



Switch case is uses as alternative of if….else. When we need to use long term of if…else then we use switch case for reducing program execution time.


syntax:
Switch(variable){
Case value 1:
Statement;
Break;
Case value 2:
Statement;
Break;


.
.
.
Default:
Statement;
Break;
}


In the switch variable we put the value which needs to match with case value. The value of variable are compare with all case value , if any of them are match with switch variable then program execute that case statement. If there are no matches found or false value passed  in the switch variable then it execute default statement.

Here break keyword are use for end that particular process, that means break keyword stop the switch statement process. The break key word can execute only then, when any of the case can executed.


If this program get integer value within 1 to 3 then its execute the case values.


 If we pass any kinds of value without case value, then the program execute the default statement


Here flowchart are provided for understand more easily .


Flow chart 

Popular posts from this blog

do...while loop in C++

The do while loop is very similar to while loop . In the while loop first check final condition then execute the statement. But in the do while loop first execute the statement then check the final condition. Syntax: Initialization; Do{ increment/decrement;   } while(Final condition)

For Loop In C++

A loop can repeat the statement of a particular job continuously for a specific number of times. Loop Description Syntax For The condition can tested before statement execution.   For(Initialization, Final condition, increment /decrements ) { statement; } while First it check the condition then do the job. Initialization; While(Final condition) { Statement; increment /decrements; } Do..while First do job then test the condition. Initialization; Do{ Statement; increment /decrements; } While(Final condition)