Decision Making with Switch Statement

Shafi Sahal
Geek Culture
Published in
3 min readMay 31, 2021

We talked about the ‘if and else’ statements for decision-making. In cases, where there are more conditions to be checked, it is better and more convenient to use the switch statements, for example, the operations on a calculator. In a calculator, we need to check whether the operation is addition, subtraction, multiplication, or division.

Calculator Program

Take a look at the calculator program

Output

Enter first number: 5
Enter the operator: +
Enter second number: 6
5.00 + 6.00 = 11.00

After taking the inputs, we made a switch statement.

switch(sign)

The lines to what to do according to the value of the sign variable are written between the curly braces of the switch statement.

case '+':

The ‘case’ keyword checks the value of the sign. If the value matches, it will execute the statements written after the colon. In this case, if the value becomes ‘+’, the code after the colon will be executed else it will not.

break;

What about the ‘break’ keyword? The ‘break’ keyword is used to stop the checks. When the program execution reaches the ‘break’, it jumps out of the curly braces of the switch statement to the next line after the curly braces and prints the result. As this is a calculator program, if the first condition is matched, it is not needed to check the second condition and if the second condition is matched, it is not needed to check the third condition and on. Even if the ‘break’ statement is not given, the program execution will run without any trouble if there is no default condition. If the default condition is present, the statements after it will be executed. If the ‘break’ is not used, the program has to execute the remaining checks without any actual need. It’s just redundant. It is a principle in programming to avoid redundancy as far as possible.

It’s like telling the computer, “Hey buddy, if you got a condition as true, just don’t check the others. It is just unnecessary”.

In case there is a need for checking the other conditions too, the break can be avoided.

default:

The ‘default’ will always execute the statements after it. So, if the ‘break’ is not used, the program will finally reach the ‘default’ and execute the statements after it and the statements in the above condition blocks will not be executed. If there is the ‘break’, as the program jumps out of the checks to the next line after the curly braces of the switch statement, the statements after the ‘default’ will not be executed.

Note: Since there are no more conditions to check after the default, the ‘break’ statement is not given.

And the ‘exit(0)’ code which is taken from the <stdlib.h>, just tells the computer to stop the execution of the program there. The reason to stop the program there is that we do not want to display the result if the operator is invalid.

%0.2f

Wondered what the 0.2 is doing here? As you know %f is the format specifier for floating-point numbers. The ‘0.2’ tells that the number displayed should have only two decimal places. So, if the value is 2.01234, only ‘2.01’ will be displayed.

You might have noticed the space in the character format specifier.

scanf(" %c", &sign);

If space is not there, the program will not run correctly. The reason behind this will be explained in a later article.

So, instead of writing separate ‘if and else’ statements for each of the conditions for checking the value of the ‘sign’, the switch statement is a more convenient one.

Previous => Decision Making in Programming

Next => Repeating Logic in Programming

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.