Board Logo
« LOOPING (FOR/NEXT; WHILE/WEND; DO UNTIL/LOOP) »

Welcome Guest. Please Login or Register.
Apr 19th, 2014, 01:55am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

This board is not meant for general discussion, it is meant for posting articles to help others.
For general discussions use the appropriate board, which best describes your problem area.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: LOOPING (FOR/NEXT; WHILE/WEND; DO UNTIL/LOOP)  (Read 23041 times)
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4386
xx LOOPING (FOR/NEXT; WHILE/WEND; DO UNTIL/LOOP)
« Thread started on: Mar 13th, 2005, 5:47pm »

Here we go loop de loop,
Here we go loop de lie...

Johnny Thunder (real name Gil Hamilton) released that song in 1962 on the Diamond Label. By 1963 it had climbed to #4 on the pop/rock charts, and was the biggest hit ever released by Johnny Thunder, although he did continue performing until the 1980's.

The Basic Programming Language was invented in 1964 by John Kemeny and Thomas Kurtz, as a simple programming language for engineers who did not need to understand computers or advanced programming. Because the home PC did not enter the scene until the late 1970's, I doubt Johnny Thunder's song had anything to do with looping, or the basic computer language, as we know it.

Basic programming, however, makes extensive uses of loops and looping; and, if we examine the lyrics of that 1962 pop song, we might think it was written by a computer with a nervous affliction perilously close to an infinite loop.

We have several forms of loops availalbe in Just Basic. A loop is the programmers way of telling the computer to "do something," and keep doing it until "something changes." The "Dumb Bunny Loop," also called an "infinite loop," is an example of a programming mistake where nothing changes and the loop continues execution until we perform the traditional "three-fingered-salute" to end the program.

The "Dumb Bunny Loop" has usually been encountered by all of us at one time or another. There is an old saying, "A person who never makes a mistake doesn't do very much." Here is an example of a "Dumb Bunny Loop": CAUTION - PROFESSIONAL CODER ON A CLOSED COURSE. DON'T TRY THIS AT HOME!
Code:
WHILE x=0 : WEND : END
 

As you can see, the value of x never changes during the loop, which continues until you interrupt the loop. I recommend the "three-fingered-salute" or pulling the plug before resorting to the "big hammer" technique.

The proper forms of looping are FOR/NEXT, WHILE/WEND, DO WHILE, and DO UNTIL. Some common examples are:
Code:
FOR cntr = 1 TO 100
	total = total + 10
NEXT cntr
 

The FOR/NEXT loop above, will execute 100 times before execution drops to the next block of code. With each pass through the loop, 10 will be added to the variable 'total.' This type of loop might be used to calculate the result of depositing $10 from each week's paycheck into a savings account for 100 weeks. Inside the loop, the programmer can include additional code, such as calculating interest for the week, by multiplying total * (int/52) and adding to the total. Unfortunately, it is never used by members of Congress who manage Social Security.
Code:
WHILE total < 1000
	total = total + 10
	total = total + (total * (.05/52))
	week = week + 1
PRINT total, week
WEND
 

This WHILE/WEND loop also deposits $10 per week to the savings account, at the same time it computes an annual interest of 5% divided by 52 weeks and adds that to the total savings. When the total amount saved is equal or greater than $1,000, execution continues to the next block of code.

Wow! This interest stuff is pretty cool! Now Johnny has learned it will only take him 96 weeks to save over $1,000! Does everybody know about this?
Code:
DO
	total = total + 10
	total = total + (total * (.05/52))
LOOP UNTIL total > 1000
 

This DO/LOOP UNTIL executes until total > $1,000. Johnny has evidently decided the new Worbles Home PC, at $1,000, will still be on sale by the time he has saved enough to buy one. Once his savings are greater than the amount needed for purchase, he will quit saving. (Silly Worble! Doesn't he know computers are like cars, horses, or women! There is never enough money; more is always needed for 'accessories!')
Code:
DO
	total = total + 10
	total = total + (total * (.05/52))
	week = week + 1
LOOP WHILE week < 100
 

In this DO/LOOP WHILE, Johnny has evidently decided he will save his money for only 100 weeks, at which time he is hoping the Worbles PC will be on sale and he will have enough for the purchase.

Looping is often used to fill arrays, load images, or any task which is repetive in nature. Sometimes the choice of loop depends upon what we wish to do with it. In the FOR/NEXT and WHILE/WEND loops, the comparison is made at the top of the loop, before the action inside the loop is performed. This is the best choice for reading data statements or loading images because the comparison is made before the action is performed. If all the data statements have been read, or all images have been loaded, execution will drop to the next block of code before we will encounter an "Input past end..." or "File not found..." error.

On the other hand, if we are paying a credit card bill, we want to calculate the interest first, add it to the present balance, and then subtract the current payment. (Well, maybe WE don't want to, but those credit card companies are pretty sticky about such things.) After the payment is made, we check to see if the balance is equal to, or less than 0, in which case we exit the loop. DO LOOPS can also be written with the comparison at the top, example:
Code:
DO UNTIL total > 1000
	total = total + 10
	week = week + 1
	PRINT week, total
LOOP
 

Any of these loops can be interrupted before ending by inserting an EXIT FOR, EXIT WHILE, or EXIT DO, inside the loop to determine if another condition evaluates to true. Several comparisons can be made during execution of the loop.

For example, you're going to flip a coin 1,000 times with a chum. If the coin comes up heads, you win a quarter. If the coin comes up tails, you lose a quarter. You and your chum each start with $5.00 and the game is over if either of you go broke, or if you have flipped the coin 1,000 times.
Code:
'John & Tom's Coin Toss
'heads = 1  A win for John
'tails = 2  A win for Tom
johnsBank = 500
tomsBank = 500

WHILE flips < 1000  'Set maximum number of decisions
    coinToss = INT((RND(1)*2)+1)
    IF coinToss = 1 THEN
        johnsBank = johnsBank + 25
        tomsBank = tomsBank - 25
    ELSE
        johnsBank = johnsBank - 25
        tomsBank = tomsBank + 25
    END IF
    PRINT "John has "; johnsBank; " and Tom has "; tomsBank
    IF johnsBank = 0 OR tomsBank = 0 THEN EXIT WHILE
    flips = flips + 1  'Do one more flip if needed
WEND
END
 


As long as there have been fewer than 1,000 flips, execution of the WHILE/WEND loop continues. There are only two possible outcomes of the random coin toss. If the random number is 1, John's bank is increased by 25 cents and Tom's bank is decreased, ELSE the random number is 2, Tom's bank is increased and John's bank is decreased. If either player has zero, the game is over. The test to see if either player has gone broke is after the PRINT statement, or the game could end while one of the player's has 25 cents displayed.

Unfortunately, Johnny won this game six consecutive times and decided it was easier to flip a coin than to flip burgers at Worble King. Johnny quit his day job and moved to Las Vegas where he became a professional coin-flipper. For a few days he was doing well and his savings account grew to nearly $100 before he ran into a streak of bad luck and lost it all. He now burns pizzas at "Pizzas 'R Us" and is still trying to figure out how his system let him down.

Any 'system' can win while the number of decisions is small. There is no such thing as "The Law of Averages," because that bill died in committee and never came up for a vote. If you let 'random numbers' make decisions for you, 'the probability of large numbers' will always prevail.

« Last Edit: Jun 13th, 2008, 2:20pm by Welopez » User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
Janet Terra
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Female
Posts: 3061
xx Re: LOOPING (FOR/NEXT; WHILE/WEND; DO UNTIL/LOOP)
« Reply #1 on: Mar 13th, 2005, 6:14pm »

I have never laughed more while actually learning in my life! Who would have thought accounting could be this much fun. What a great tutorial! This JB Community sure is lucky to have you around. cheesy
Janet
User IP Logged

Janet Visit me at BASIC Samplings
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls