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)

Input & Output In C++ (Basic)

Cout<<”Print sentence”;     This statement is use to print out any kinds of sentence or string type values. Cout<<345;     This statement is use to print out any kinds of numeric numbers, without change. Cout<<variable;        This statement is use to print the value of variable. The value of the which variable we want to print, It is need to write that variable on variable place.