Eposode 149: If this, then what?

Comic Transcript

Panel 1.
Baltie: Kodu, you aren’t the only one who knows computer programming, you know!
Kodu: Sure, Baltie, but Ludd is asking me, aren’t you, Ludd?

Panel 2.
Ludd: Umm… hmm… hey, how do you choose between different options in programming? Say I want a function to do one thing if a variable has a certain value and something different if it has another value.

Panel 3.
Kodu: Ah, conditional statements. Yes, this is very important in programming. There are a couple of different ways to handle this.

Panel 4.
Kodu: One way is using “if” statement…
Ludd: Kinda like what I said.

Kodu: Yes, kinda. You can use an “if” statement to check the value of a variable. The program does one thing if the variable has a certain value, or something else if it doesn’t.
Ludd: And you can use lots of “if” statements to check multiple values of the variable and make your program behave as you want?

Panel 5.
Kodu: Well, yes you can… but there is a better way.

What does it mean?

Conditional statement – Simply put, a conditional statement checks to see if a particular condition is true or false. It then executes a certain block of computer code if the condition is true, and a different block if it is false.

In human speak please!

Very often, a computer program will have to make decisions based upon user or sensor input or upon results of calculations within the program. One of the ways a program makes decisions, as discussed in the comic, is by using “if” statements. Let’s look at an example.

Start Program

String activity
Int age

Main {

Print “Please enter your age: ”
Input age
activity = AgeCheck(age)
Print activity

}

String AgeCheck(int userAge) {

If ( userAge > 15)
Return “You can drive!”
Else
Return “You have to wait.”

}

End Program

Some of the code in this example was discussed in the last episode. Here are explanations of the new stuff.

Int age

“Int” is short for integer and is the data type associated with whole numbers.

     activity = AgeCheck(age)

This line calls the function” AgeCheck” by passing it the argument “age” and assigns the result to the variable “activity.”

String AgeCheck(Int userAge)

Notice that this function starts with “String” as opposed to “Void” that we used in the last example. This means that this function returns a value of data type string. Notice also, that the parameter required by this function is of the data type “Int.” Functions can return values of data types different than their accepted parameters.

     If ( userAge > 15)
Return “You can drive!”
Else
Return “You have to wait.”

This section is an example of conditional code. The first line checks to see if the value of the argument passed to it from the “Main” function is greater than 15. If so, the “AgeCheck” function returns a value of “You can drive!” to the “Main” function. If the value is less than 15 then it returns “You have to wait.”

Using an “If” conditional block make sense if you are comparing two or more values. You can always chain a whole bunch of “if-else” statements. In the next episode we will talk about another type of conditional statement. Stay tuned.