Eposode 150: Quick! Switch!

Comic Transcript

Panel 1.
Ludd: So, what is the other type of conditional statement?
Kodu: The “switch” statement allows you to check a variable against a large number of values…

Panel 2.
Kodu: …and execute different programming code depending on the value.
Ludd: What happens if none of the values are a match?

Panel 3.
Kodu: What do you mean what happens? Why would you program it not to match?
Baltie: I think Ludd…

Panel 4.
Kodu: You are interrupting again.
Laby: Let him speak, Kodu. Maybe you could also learn something. Go ahead, Baltie.

Panel 5.
Baltie: Umm… switch statements usually have a default section which runs a particular set of code if none of the other cases are met.

Panel 6.
Kodu: Oh yeah, that makes sense… thanks, Baltie.
Baltie: No problem!

In human speak please!

“Switch” statements make writing conditional statements easier by removing the need to write a whole bunch of “if” statements to check the values of a variable. Let’s take a look at an example. As before, this example does not represent any particular programming language.

Start Program

Int firstNum Int secondNum Int result String operation

Bool answerExists = true

Main {

Print “Enter first number:”
Input firstNum
Print “Enter second number:”
Input secondNum
Print “Enter ‘+’ to add or ‘-‘ to subtract:”
Input operation

result = Calculator(num1, num2, operation)

if (answerExists) {
Print “The result of your calculation is: “ + result
}
Else {
Print “Invalid math operator was entered.”
}

}

Int Calculator(Int num1, Int num2, String operator) {

Int answer = 0

Switch (operator) {

Case “+” :
answer = num1 + num2
break
Case “-” :
answer = num1 – num2
break
Default :
answerExists = false
break
}

Return answer

}

End Program

Let’s take a look at the new code presented here.

Bool answerExists = true

“Bool” is short for Boolean, a data type that has one of two values, either true or false. This variable can act as a simple on/off switch; do one thing if it’s true, do something else if it isn’t. Here the variable is initialized, or set to have a default value of true. This can be changed by the program later, as you will soon see.

Moving on with the example, it is easier to continue with the definition of the “Calculator” function.

Int Calculator(Int num1, Int num2, String operator) {

No matter how many parameters a function requires, it can only return one value. (There are data types, which can have many values embedded in them. An integer array is an example.) This function needs three parameters, two of data type “Int” and one of data type “String,” and it returns one value of data type “Int.”

     Int answer = 0

The declaration of a variable inside a function limits the variable’s scope or reach within that function. It means that although “answer” is initialized to have a default value of “0” it cannot be used in the “Main” function. It will still be able to pass its value to “Main” via the built-in “Return” function as we will see later.

     Switch (operator)

The “Switch” statement block is the second most commonly used conditional programming method. It is given a variable to observe, “operator” in this instance and will perform certain tasks based on its value.

          Case “+” :
answer = num1 + num2
break
Case “-“ :
answer = num1 – num2
break

This block of code executes tasks based on the value of the “Switch” variable. If the value of “operator” is “+” it will add the values of “num1” and “num2” passed to the “Calculator” function from the “Main” function. If it is “-“ then it will subtract “num2” from “num1.” The “break” simply means that if this set of code is executed then the program has found a match and should not bother checking any of the other cases.

          Default :
answerExists = false
break

If the value of “operator” does not match one of the cases of the “Switch” statement, it will default to running these programing statements. Notice how this set of code changes the value of “answerExists” from true (defined at the beginning of the program) to false. This will determine which of the statements in the “If” section is executed in “Main.”

     Return answer

As seen in a previous example, the line returns the value of “answer” to the “Main” function. Even if the “Switch” executes the default value,, and does not assign a new value to “answer,” it will still return the value 0 (zero) to “Main.” The reason for this is that we had set “answer” equal to 0 when we declared it, and we had to do that to satisfy the “Calculator” function’s expectation to return an integer. If it doesn’t, then the program might behave unexpectedly, or even crash.

     result = Calculator(num1, num2, operation)

if (answerExists) {
Print “The result of your calculation is: “ + result
}
Else {
Print “Invalid math operator was entered.”
}

The return value of “Calculator” function is assigned to “result.” Then, if the value of “answerExists” was unchanged in “Calculator,” the “If” statement will execute the result of the calculation. Otherwise it will print that an invalid math operator was entered.