Udostępnij za pośrednictwem


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

  • Anonymous
    March 31, 2015
    The comment has been removed

  • Anonymous
    April 04, 2015
    The comment has been removed

  • Anonymous
    April 04, 2015
    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!

  • Anonymous
    April 08, 2015
    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

  • Anonymous
    July 23, 2015
    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!

  • Anonymous
    January 31, 2016
    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