1.4 - Conditions and Loops
1.4 - Conditions and Loops
Would you like to specify conditions that control how your program runs (or
whether it runs at all)?
This program instructs the computer to display "Happy New Year" only if
today is January 1st.
Notice that this program contains the If, Then, and EndIf keywords.
Conditions in Small Basic Programs
In programming, you can do that same thing in more than one way. As a
programmer, you can choose the best way.
Both programs give the same result, but you can use fewer If, Then, and EndIf
keywords if you use the Else keyword.
Conditions in Small Basic Programs
OUTPUT
When you write a program, you can specify as many conditions as you want by
using the ElseIf keyword. You can also specify one or more operations for the
computer to perform, depending on which condition is true when your program
is run.
In general, you use a For..EndFor loop to run code a specific number of times.
To manage this type of loop, you create a variable that tracks how many times
the loop has run.
Let’s use this concept to print the multiplication table for the number 5.
OUTPUT
Loops in Small Basic Programs
If you don’t know the loop count before you write a program, you
can create a While loop instead of a For loop.