Learn Decision Making using Switch Statement in C++

C++ (7) - Decision Making 740X296

After discussing the decision making using if statement with its various forms, we are now on a date with another useful decision making tool: the switch statement. The switch statement is useful when we have a long list of possible options (values) to be tested against only one variable.

To imagine the problem, let’s go back to the menu example. The program displays a menu of options, and asks the user to enter his choice. The choice is typically an integer number (or sometimes a letter). Once entered and validated, the user’s choice is compared against the list of available options to determine what actions to be taken (i.e. what code to be executed). In the simple menu example discussed in the previous article, the matter was really simple; the menu offered the user only three options (1, 2, and 99)


What if the menu was longer?! What if it contains more options that just displaying system date and time?! Consider the case wherein the menu should also offer displaying host name, IP address(es), and also offers changing them all?! There are eight options, besides to the exit option = nice different choices available. Now, the hard part is: how to code this?!

Obviously, we need a series of if .. else if  statements to decide (based on the user’s input) what the program will do. This will be something like the following:

The checks are made for different values against the same variable. Each possible offered option required a separate check, resulting in nice checks. Now, what if the menu was even longer?! The switch statement offers a more efficient way to implement this requirement.


Where: x1, x2, … , xn are the possible values to check the variable x against.
break is a C++ statement that exits the switch block.
default  is a C++ keyword that acts as the last resort if neither of the case values had a match. Does this look familiar?! Yes, exactly!! The else statement job in the if .. else if .. else structure. They both do the same job.

Consider the basic calculator example. In this new version, we are going to prompt the user to enter the operation he wants to perform using the format: X operator Y

Where X and Y are the operands, and the operator is either +, -, *, or /
The program should make the required calculation and print the result to the user.

Consider the following code:

Let’s see how this program will behave:


And with subtraction:


And one more time with multiplication:


Great!! Now, several things need some explanation:

  • char op;

This statement declares a variable op of type char (character).

  • The statement: cin >> x >> op >> y;

prompts the user to type three inputs of two different types (float and char).

  • The final statement that displays the result to the user:

was written in two lines. cin and cout statements can span multiple lines as desired.

In think the rest of the code is simple enough to be easily understood.

Conditional Operator
Consider the following code:

The Conditional Operator presents a very compact alternative to the above code:

I think the line is self-explanatory, so I prefer to illustrate it by an example.

In this example, we are going to re-write the Odd-Even program using the conditional operator.

Consider the following code.

Let’s execute it.



* * * * * *

  • The switch statement can replace the if..else if structure in case different numeric values or characters are being tested against the same variable.
  • The conditional operator could be a compact alternative to a simple if statement with a condition and single-statement body, and else part having also a single-statement body.

That was the topic of Decision Making. In the next article, we are going to talk about Loops. An interesting topic that you shouldn’t miss. Waiting for you there!!!


Please enter your comment!
Please enter your name here