switch: looking at one variable
Learn C++ in step-by-step lessons.
switch: looking at one variable
The switch construct offers an alternative to nested if statements. Switch can be used when the test involves a single variable with a small number of possible values.
The syntax is:
switch(variable) {
case value: statement; statement; statement; break;
case value: statement; statement; break;
case value: statement; break;
default: statement;
}
When a match is found, every statement is executed until there is a break.
Goals:
- Be able to use and evaluate the switch/ case control structure
Objectives:
- Be able to explain the syntax of the switch case control structure
- Know what break does
- Be able to determine when switch /case is appropriate
Please study the material at each of the links below.
- Calculate tip based on service: Y or N
- Print season from month
Glossary for switch lessonFull Glossary