Small Basic – The If-ElseIf Ladder


The If-ElseIf Ladder

Let’s say that you’d like to compute a math formula with three variables: x, y, and z. But none of the inputs can be negative. So you’ll need to check the variables before you start!

Here’s one way to check the three inputs:

If (x < 0) Then

  TextWindow.WriteLine(“x is negative”)

Else ‘ x >= 0

  If (y < 0) Then

     TextWindow.WriteLine(“y is negative”)

  Else ‘ y >=0

    If (z < 0) Then

      TextWindow.WriteLine(“z is negative”)

    Else

      ‘ Do the calculation …

    EndIf

  EndIf

EndIf

As usual, you’ll indent the If and Else parts to make the code easier to read. But when the nesting becomes too deep, the program gets too hard to read! Another way to make the code easier to read is to combine the Else and the If after it into one ElseIf keyword.

Take a look:

If (x < 0) Then

  TextWindow.WriteLine(“x is negative”)

ElseIf (y < 0) Then

  TextWindow.WriteLine(“y is negative”)

ElseIf (z < 0) Then

  TextWindow.WriteLine(“z is negative”)

Else

  ‘ Do the calculation …

EndIf

Now let’s look at some practical examples of the If/ElseIf ladder!

Positive, Negative, or Zero

As a simple example, you’ll make a program that checks whether a number entered by your user is negative, zero, or positive. The complete program’s below, along with the output from three sample runs. Try it out!

‘ Positive, Negative, or Zero

‘ Determines if a number’s negative, zero, or positive

TextWindow.Write(“Enter a number to test: “)

num = TextWindow.ReadNumber()

If (num < 0) Then

  TextWindow.WriteLine(num + ” is negative”)

ElseIf (num = 0) Then

  TextWindow.WriteLine(num + ” is zero”)

Else

  TextWindow.WriteLine(num + ” is positive”)

EndIf

Output Examples:

Enter a number to test: -1

-1 is negative

Enter a number to test: 0

0 is zero

Enter a number to test: 10

10 is positive

The program starts by asking your user for a number (“Enter a number to test”), and then it takes the number and stores it in the num variable (“num =”).

Let’s take a closer look at these three cases:

Case 1: Your user enters a negative number (like -1). In this case, the condition on Line 6 is true. So your program runs the “is negative” statement and then ends.

Case 2: Your user enters 0. The program checks the If condition. Since (0 < 0) is false, your program moves on to check the first ElseIf condition. Since (0 = 0) is true, your program runs the “is zero” statement and then ends.

Case 3: Your user enters a positive number (like 1). The program checks the If condition. Since (1 < 0) is false, the program checks the ElseIf condition. Again, since (1 = 0) is false, the program moves to the Else clause, runs the “is positive” statement, and then ends.

The If/ElseIf ladder provides a compact way for you to make multiple tests on a condition or on a set of conditions.

Concept: In an If/ElseIf ladder, a condition’s tested only when all the conditions that come before it in the ladder are false. When any condition is true, the statement(s) associated with that condition are completed, and the program moves to the first statement after the EndIf. Use the If/ElseIf ladder to avoid deep nesting your If statements and to make your code easier to read!

Now let’s do a Self-Check!

SELF-CHECK:

To appreciate the usefulness of the If/ElseIf ladder, rewrite the “Positive, Negative, or Zero” example (the last code section above) using nested If/Else statements. (Doing something the hard way helps you appreciate something!)

How did you do? Let us know if you have any questions!

Learn more about Small Basic: http://blogs.msdn.com/b/smallbasic/

Have a Small and Basic Day!

   – Ninja Ed & Majed Marji

Comments (1)

  1. Computers Today (part 1 of 6)

    blogs.msdn.com/…/computers-today.aspx

    …..

    CS SPOTLIGHT: Girls in computer programming… why it matters!!!

    blogs.msdn.com/…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

    Computational Thinking – Videos & Papers by Jeannette Wing

    blogs.msdn.com/…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx

Skip to main content