Small Basic - Avoiding the Else


Avoiding the Else

Let’s say you want to create a program that monitors the state of a system by watching its temperature. If the temperature’s below 100°, the program tells your user that the system’s running normally. If the temperature’s 100° or more, then it displays a warning message. The code below shows you one way to write this program, along with the output from two sample runs.

 

' Helps monitor the temperature of a system

 

TextWindow.Write( "Enter the temperature of the system: " )

temp = TextWindow.ReadNumber()

 

If ( temp < 100 ) Then

   msg = "The system is running normally."

Else

    msg = "Warning! The system is overheating."

EndIf

 

TextWindow.WriteLine( msg )

 

Output:

Enter the temperature of the system: 90

The system is running normally.

 

Enter the temperature of the system: 110

Warning! The system is overheating.

 

Look how the program sets the msg variable to different strings, based on the current temperature (in Lines 7 and 9), and then it displays that message (Line 11). See this next example for a different way to write this program.

 

' Monitors the temperature of a system

 

TextWindow.Write( "Enter the temperature of the system: " )

temp = TextWindow.ReadNumber()

 

msg = "The system is running normally."

' Assume normal

 

If ( temp >= 100 ) Then          ' and if it isnt normal

msg = "Warning! The system is overheating."

                                 ' change your assumption

EndIf

 

TextWindow.WriteLine( msg )

 

The program above assumes that the system’s running normally and gives the msg variable the message, "The message is running normally." Your program then checks to see if the assumption’s true (temp >= 100). If the system’s running normally (the temperature’s below 100 degrees), then the program continues to display the message, "The message is running normally." If the temperature is 100 degrees or more, then your program updates the msg variable with the warning message ("Warning! The system is overheating") and continues to write the line at the end to warn your user. You were able to eliminate the Else block completely!

This style (of using an If statement to change a variable’s default value) is very common among programmers, so you should get used to it. You’ll see both this style and the If/Else style used in the community.

 

Why are we explaining these alternatives? So that you can understand the differences and will be able to know what the code is doing when you see other peoples' code!

  

Have a Small and Basic day!

   - Ninja Ed and Majed Marji

Comments (6)

  1. Daniele Cazzaro says:

    Hi,

    I have seen many times this style of programming.

    There is one thing to say, when you use the first solution (if/else) you execute 5 step of code always (write, read, if condition, assignment and writeline), instead when you use the second solution (if only) in the best case you execute 5 step (same as above), but in the worst 6 step (write, read, first assignment, if condition, second assignment and writeline).

    Daniele

  2. Daniele, understood. Sometimes this method only removes one line of code.

    Do you use Small Basic?

    Keep in mind that this style isn't being recommended. We're merely teaching the alternatives so that new programmers will understand the differences and will be able to know what the code is doing when they see other peoples' code.

    Thanks!

  3. UPDATE:

    I updated the formatting of the code and updated the references to the lines. I also added the paragraph at the bottom to explain the intentions of this blog post a bit more.

    Thanks!

  4. Yes, I wanted to point out this additional aspect of your code that I thought still important.

    I do not know Small basic but I love this language and its ability to introduce programming.

    When my son (3 yr) has learned to speak well, the second step will be teach him how to program, and Small basic is here for. 😉

    Daniele

  5. Daniele, sorry I missed your comment!

    If you teach your 3 year old Small Basic, let me know! =^)

    Over on the Small Basic blog, we have 48 testimonies of students, age 8-13:

    blogs.msdn.com/.../small-basic-elementary-student-testimonials.aspx

    So just leave a comment over there with your quote for your kid, and kid's age.

    I'm teaching an 8 year old daughter Small Basic. I've taught Kodu to a 4-year old daughter as well. So you might want to start with Kodu.

    Thanks!

  6. 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