switch Statement
Learn about the switch statement in C++.
We'll cover the following
In this lesson, we’ll look at the switch
statement which is another conditional statement.
The switch
statement (syntax)
The switch
statement is the extended form of if-else
statement, but with a slight change: in if-else
, we use a condition which, if false
, executes the block of statements inside the else
part. In the switch
statement, however, we match an expression against several values for equality. These values are called cases. When the expression’s value matches with a case (integer or a character value), only then do the case’s statement(s) get executed.
The
switch
statment works with characters and integers only.
When no case is matched, the default statement (if there is one) is executed. It is not compulsory to have a default statement, but it is a good programming practice to have one.
The syntax for the switch statement is:
switch(expression){case constant1:statementbreak;case constant2:statementbreak;case constant3:statementbreak;...default:statement}
The
default
statement can be anywhere in theswitch
statement. If no case is matched, thedefault
statement will still be executed.
How switch
works?
Look at the animation below to understand how the switch
statement works.
In the illustration above, suppose some expression evaluates to the constant case c3
. The control then directly goes to the matched case c3
. Then the statement of c3
executes until a break
statement is found.
The break
statement stops the processing of the particular case inside the switch
statement and passes the control to the statement after the switch
statement.
Note that the
break
statement can be used inside a loop as well.
- Whenever the
break
statement is executed inside a loop, the loop is immediately terminated (no instruction after thebreak
statement within that loop block will be executed), and the control jumps to the next instruction following the loop. Notice if there is a nested loop and thebreak
is inside the second nested loop, then only the inner (second) loop will be terminated.
Let’s look at a code example now that uses the switch
statement to calculate grades based on the marks entered by the user.
We want to determine what grade the candidate would get. A candidate is considered to have passed if they score more than 50% on the test. They get a “C” if they score 50%, and “D” or “F” if they score less than 50%.
//example of switch statement #include<iostream> using namespace std; int main() { int marks; cout << "The marks: "; cin >> marks; switch(marks / 10) { case 10: case 9: { cout << "A+" << endl; break; } case 8: { cout << "A" << endl; break; } case 7: { cout << "B+" << endl; break; } case 6: { cout << "B" << endl; break; } case 5: { cout << "C" << endl; break; } case 4: { cout << "D" << endl; break; } case 3: cout << "The "; case 2: cout << "grade "; case 1: cout << "is: "; case 0: { cout << "F" << endl; break; } } return 0; }
Run the code above with different numbers including
95
,100
,109
, and7
as inputs.
We can execute multiple cases as well, as can be seen in lines 13–14 and lines 50–56.
When a case label matches with the evaluated expression and there are multiple case labels without a break statement in between, those case labels are also executed until a break
statement is found. For example, if we enter 30 marks as input in the code editor above, we’ll get the output The grade is: F
. This shows that once case 3
matches, the case labels 2
, 1
, and 0
are also executed until the break
statement is found.
Solve the quiz below to identify the issues with our code above.
Enter 75
marks in the code above and see which grade gets printed.
A
B+
If you’ve answered the quiz questions above, you know exactly what the issue is with our code. We need to modify our code so that the input entered is valid and within range (0 to 100).
So our if
condition would be if(marks > 0 && marks <= 100)
and our else
condition would be cout << "Invalid input" << endl;
.
Use the if-else
statement to solve this issue in the code editor below. Then try running the code again with inputs 105
and -105
respectively.
//example of if else statement(variant 2) #include<iostream> using namespace std; int main() { int marks; cout << "The marks: "; cin >> marks; // Add "if" condition here { switch(marks / 10) { case 10: case 9: { cout << "A+" << endl; break; } case 8: { cout << "A" << endl; break; } case 7: { cout << "B+" << endl; break; } case 6: { cout << "B" << endl; break; } case 5: { cout << "C" << endl; break; } case 4: { cout << "D" << endl; break; } case 3: cout << "The "; case 2: cout << "grade "; case 1: cout << "is: "; case 0: { cout << "F" << endl; break; } } } // Add "else" here return 0; }
Click the “Show Solution” button below to view the solution.