Using Liberty Basic

Want to learn to program using Liberty Basic? Already programming in Liberty Basic but want to learn more? Follow this experienced programmer (Java) as he teaches himself this wonderful programming language.

 

Thursday, May 11, 2006

Looping using a 'While' loop

Sometimes you need your code to do something repeatedly. Let's say you are writing a program to calculate the total of 10 numbers. Rather than writing 10 lines of code that read in a number and 10 lines of code that add the latest number to the current total, you could use a loop instead. Liberty Basic offers three kinds of loops... 1) The 'while' loop, 2) the 'do while' loop, and 3) the 'for' loop. In this post we will take a look at the 'while' loop. In later posts we can tackle the 'do while' loop and the 'for' loop. Don't think I am leaving these other two loops for later because they are harder than the 'while' loop. The 'do while' loop and the 'for' loop are just as easy to understand and use. I just like tackling one thing at a time. The 'while' loop is a fine place to start when you want to start introducing loops into your programs. Anyway, so here is how it goes. The example program below reads in 10 numbers and calculates the sum of all the numbers entered in.


' =====================================
' Demonstration Of The While Loop
' =====================================
' Description:
' This program demonstrates the use of
' the while loop to read in 10 numbers
' from the user. The sum of the 10
' numbers is calculated and output at
' the end of the program.
'
' Author: Eddie Meyer
' Date: 11th May 2006

' Variables
' ============

' The number of items to read in
NumToRead = 10

' We are currently reading in item #CurrentNum
CurrentNum = 1

' The current total
Sum = 0

' Loop until we have read in the required
' number of items.
while CurrentNum <= NumToRead
if CurrentNum = 1 then
input "Please enter a number: "; InputNumber
else
input "Please enter another number: "; InputNumber
end if

' Update the current total
Sum = Sum + InputNumber

' Update the counter
CurrentNum = CurrentNum + 1
wend

' Print out the total
print
print "The sum of the "; NumToRead; " numbers you entered is ";
print Sum; "."

end


Points to note are...

1) The 'while' keyword to signify the start of the loop
2) The expression after the 'while' keyword. Basically, the loop will execute as long as this expression remains true.
3) The 'wend' keyword to signify the end of the while loop.

Also (as a side note for newbies), notice how I got the program to make a distinction between the case when we are reading in the first number compared to the case when we are reading in any of the subsequent numbers. For the first number the program prints "Please enter a number: " and for the other times it prints "Please enter another number: ".

Tip: When writing your while loops, make sure that there will be a time when the loop will exit. Otherwise the loop will go on forever... or at least until the program or computer crashes. In our case we used a counter called CurrentNum that increments until it reaches a value of 11. When CurrentNum reaches a value of 11, the expression CurrentNum <= NumToRead is not true any more, and therefore the loop stops executing (doesn't perform any more iterations). Note, that if we forgot to add the line CurrentNum = CurrentNum + 1, the value of CurrentNum would have always remained as 1 and the loop would have continued indefinately (until the program crashed).

Anyway, I think that is enough for one post. I hope you enjoyed it.

Eddie

0 Comments:

Post a Comment

<< Home