Hello this is that talks about using switch as a conditional. A switch sentence makes a variable to be tested to be equal against some values. Every value is named a case and the value is checked for every case.

 

The structure of a switch is…

 

Switch (the expression)

   

Case and constant expression: statement

 

[default : statement ]

 

When you make a switch statement make sure you are aware of:

–       It is easier for the switch expression to work if it is an integer or an enumerated type.

–       The number of cases is unlimited, each one has to be separated with the value they are compared to with a : .

–       The constant and the expression compared have to be the same type.

–       When a variable is equal to a case, the statements net to that case will execute until a break statement appears.

–       A break the switch ends and the control jumps to the following line of the switch.

 

 

I found this diagram on http://www.tutorialspoint.com/cplusplus/images/cpp_switch_statement.jpg that may explain better this process.

The behavior of a switch statement goes like this:

 

If s converted value matches the expression – Control is transferred to a statement following the label.

 

If none of the constants match the ones on the case – Control is transferred to a default label.

 

If none of the constants match the constants in the case labels – Control is transferred to the statement after the switch statement.

 

I created this simple program as an example for the usage of a switch conditional. It evaluates the number introduced and says if you are an adult or not.

 

WATCH MY VIDEO https://youtu.be/-_8H_XSRJR8

 

CC BY 4.0 #Mastery17 – Use of “switch” as a conditional by Ricardo Newton is licensed under a Creative Commons Attribution 4.0 International License.