Saturday, April 20, 2024 00:52

Table of contents >> Introduction > Conditional statement Switch

Conditional statement Switch

As we will see, conditional statement Switch works very much like the If-Else If-Else statement, with only a syntax difference. It executes an instruction based on the calculated value of a condition. The format of the Switch statement is the following:

The above code could have been written as:

We have a condition, for which we check if its result is equal to value1, and execute some instructions if it’s True; if not, we check that result against the next value, value2, executing some other instructions if it matches, and so on. Finally, if none of the conditions are True, we have a default statement, which is similar to the else statement, and which executes only if none of the values matched the value of the condition.

Aside of that, you’ve noticed the keyword break. As you already know, a Switch statement performs a conditional processing, and you can define one or more possible cases against which it can check the value of the condition. For each case, we specify the instructions we want to be executed when that case is True, and a break instruction at the end of the instructions, to separate a case instruction from another. If you don’t place a break statement, the execution will continue with the instructions that follow, without checking the case with which those instructions are associated. Let’s consider the following example:

First, the program checks the character variable is equal to character ‘A’ and executes an instruction if it’s True, printing the “The character is A” message. Everything good so far. However, because no break instruction follows, the program will continue the execution and will also display the messages “The character is B” and “The character is C”. Because of this, if no code is found after the case statement, the break instruction can be omitted, since the program will simply jump to the next case statement, until it will find a break operator. However, after the default case, a break instruction is mandatory.

You can also use multiple cases for the same instruction, as following:

There are times when the number of possible cases is limited and we check them all. In such case, it is still advisable to place a default check, in which you can print an error message, for instance.

It is considered a good practice to place the default case at the end of all the other cases.

The concepts explained in this lesson are also shown visually as part of the following video:

 

EXERCISES
1. Write a program that asks for a digit (0-9), and depending on the input, shows the digit as a word in a Bulgarian or English. Use a switch statement..

Solution


Guidelines: Just use a switch statement to check for all possible digits.

2. Write a program that, depending on the user’s choice, inputs int, double or string variable. If the variable is int or double, the program increases it by 1. If the variable is a string, the program appends \”*\” at the end. Print the result at the console. Use switch statement.

Solution


Guidelines: First input a variable, which indicates what type will be the input, i.e. by entering 0 the type is int, by 1 is double and by 2 is string.

3. Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation. Examples:
– 0 → “Zero”
– 12 → “Twelve”
– 98 → “Ninety eight”
– 273 → “Two hundred seventy three”
– 400 → “Four hundred”
– 501 → “Five hundred and one”
– 711 → “Seven hundred and eleven”

Solution


Guidelines: Use nested switch statements. Pay special attention to the numbers from 0 to 19 and those that end with 0. There because printing a single digit is part of printing a 2-digit number which is part of printing 3-digit number.

Tags: ,

Leave a Reply



Follow the white rabbit