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.

 

Wednesday, May 24, 2006

Looping using the 'For' loop.

Seeing as I have just given an example of how to use the 'do while' loop, I may as well quickly give an example of how to use the 'for' loop. I will use the same example of needing to read in 10 numbers and then print out the sum.

Here is the program using the 'for' loop.


' ==================================
' Demonstration Of The 'For' Loop
' ==================================
' Description:
' This program demonstrates the use of
' the 'for' 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: 24th May 2006

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

' The number of items to read in
NumItemsToRead = 10

' The current total
Total = 0

' Loop until we have read in the required
' number of items.
for i = 1 to NumItemsToRead
if i = 1 then
input "Please enter a number: "; InputNumber
else
input "Please enter another number: "; InputNumber
end if

' Update the current total
Total = Total + InputNumber
next

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

end


Points to note about the 'for' loop are...

1) The use of the 'for' and 'next' keywords.

2) The variable that will be used as the counter.

3) The start value of the counter and the end value of the counter.

As you can see, the 'for' loop is really simple to use and ideal for cases where you just want to iterate over a piece of code a certain number of times. It is pretty common practice to use simple variables names with for loops (like i in my example above). Normally, you are encouraged to use very descriptive names for your variables, but in the case of the 'for' loop, we know the variable is just going to be some kind of counter so we can make an exception here and keep it simple. I used a variable called i because this seems to have developed into the most commonly used variable name for 'for' loops. By sticking with convention, other programmers will find it easier to read and maintain my code (should they need to). Note: the letter 'i' can at least be seen to be an abbreviation of the word 'index'. :-).

There you go. You have now officially been introduced to the three main types of loops provided by Liberty Basic.

Have fun with your programming.

Eddie

2 Comments:

  • Hi Eddie,

    Thanks for posting your experiences! I should point out that the looping constructs you've illustrated here are standard BASIC, just so that non-BASIC users won't think this is some Liberty BASIC specific stuff. :-)

    -Carl Gundel, author of Liberty BASIC

    By Blogger Carl Gundel, at 8:15 PM  

  • Hi Carl,

    Thank you for your interest in this blog. I'm sure my readers will be especially interested in any comments you make here (what with you being the creator of Liberty Basic and all). I'm sure your input here will a) help me with my own learning and b) help make sure I post correct information for others on this blog.

    Thank you for your comments and please visit this blog often. I will always welcome your comments.

    Oh, by the way, congratulations on developing such a wonderful, easy to use, programming language.

    Eddie

    By Blogger Edward Meyer, at 8:37 PM  

Post a Comment

<< Home