Question:
Can someone please explain to me loops?
Geralt of Rivia
2009-02-12 14:16:54 UTC
I want to learn a couple of programming languages, starting with the TI-84 BASIC language, but I've come across WHILE loops and loops in general.

I am stuck with loops and have no idea what they mean and how they are used. I know what they do, but I don't know how to incorporate them into a program.

This is the program that the tutorial told me to do:

http://www.arasian.com/vortex/83pbas/16.htm

Whats going on with the G? I know that it explains it on the bottom, but I've read it and reread it and I still don't understand it =/. So, can someone please explain to me the loops and how they are used so I can move on with my studies?

Thanks.
Five answers:
2009-02-12 14:37:10 UTC
The first thing I have to wonder is why are you trying to learn this language? This is a pretty archaic programming language, and I can't think of anyplece where it's used, other than as a curiosity.



Nevertheless, the G value is the control of the loop. At the top, G is set to zero. It has to be set to a value to make this work. A is set to 15 for the example. You can set A to anything you like.



Now, the program waits until you type in a number.If the number you type is smaller or larger than A (15), you get the appropriate message.



The value of G stays at zero, because we want the loop to continue until the user gets the number right (which will happen as long as G==0). Once they hit it, G is set to 1, and they get the success message. Now that G == 1, the program ends because the condition has been met.



In most languages, this would look something like this pseudocode. This is a much simpler way of seeing it:



A = 15;

G = 0;

D = ''; (D is null)



while (G is zero) {

print "Enter a number:";

read input into D;

if (D is more than A) {

print "Your number is too high"

} else if (D is less than A) {

print "Your number is too low"

} else if (D equals A) {

print "Your number is correct"

set G to 1;

}



}



Loops are just a way of repeating something until a condition is met or a range of values has been tested.



"while" statements run until a certain condition (G equals 1) is met.



"If" statements look at the value of something, and act based on that value. An if statement can have a number of different actions to take, all based on different conditions. You can also do this with a "switch" statement.



"for" statements act on a range of values. Say you wanted to know the square of all number from 1 through 20. A "for" statement would perform the calculation and display it through the whole range, then stop at the last number.
tuaamin13
2009-02-12 22:28:13 UTC
Loops are for repeating a series of operations multiple times.



FOR loops are used a definite number of times (ie, you want to check every number in an array). You know that the array is 10 or 20 or 300 places.



WHILE loops run for an undetermined amount of time. Say you want to add 1 to a negative number the user puts in until it's 0. (Stupid example, I know). You don't know how many times you'll do that, so you'll use a while loop.



DO loops (may not be on TI-84, but it's in some other languages) are while loops that are executed at least once. With a while loop, if the condition is satisfied before you enter the loop you won't go through the operations.



Thus if you have while(x<2) and right before that you have x=3, your while loop won't execute. DO loop will execute once, then check the while condition.



It's possible to write virtually any FOR loop with a WHILE loop, or a WHILE loop with a FOR loop, just it's not always the most efficient way to go about things.
Joe C
2009-02-12 22:25:01 UTC
Loops are a way to repeat lines of code.



In the code example they are asking you to guess a number until you get it right.



Lets look at counting to 10.

There are several constructs to create a loop



The GoTo:

1: x=0

2: x = x + 1

3: print x

4: If x = 10 goto 6

5: goto 2

6: exit



For / Next (used to iterate a variable):



For x = 1 to 10

print x

next x



Conditional loops While / Until :



x=0

DO

x = x + 1

print x

UNTIL x = 10



x=0

WHILE x < 10

x=x + 1

print x

WEND



The difference between UNTIL and WHILE is when the condition is evaluated. UNTIL does it at the end of the loop so the code within the loop is always executed once. WHILE evaluates at the begining so the loop code might never be executed if the WHILE condition is false from the start.



Hope this helps
Jaden
2009-02-12 22:29:29 UTC
Loops do lots of things. For instance, I used to program TI Calculators when I should have been paying attention in my high school math classes, but every program I ever made incorporated some kind of loop.



Most of my programs were little games, but a mathematical use for them is to calculate prime numbers. You can create a loop that will try and multiply every number by another number to see if it is the number (to verify it is prime)



Here's an example. I don't know for sure if it works or not, it's been a long time.



Input "Prime? ",N

(N/2)->A

0->P

While A!=0

(N/2)->B

While B!=0

If (A*B)=N Then

1->P

End

B-1->A

End

A-1>B

End

If P=1

Disp "Your number is Prime"

End
2009-02-12 22:20:55 UTC
repeating a part of the program over and over again until you decide to exit...



it's normally done with if/then/else statements and a counter...



For example, write out the line I AM GREAT twenty times then exit program...

1. set your counter to 0

2. write out the line I AM GREAT

3. advance your counter by 1

4. check to see if you're counter is >= 20

5. if it's not, repeat steps 1 - 4 (the loop), if it is, exit program



... or ... WHILE counter < 20... etc...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...