Qbasicnews.com

Full Version: Looping problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
alrighty then, when i try to loop my program to run twice it ends up running a million bazillion time however my do loop i make to say loop until time =2 when the start time = 0. what should i do?
the form for what i have is as follows:

Code:
cls
randomize timer

time =0
card1=rnd *3
do
if card1 <1 then
               print " clubs"
              elseif card1 <2 and card1 > 1 then
               print "spades"
               time = time+1
               loop until time =2
               end if
thats the jist of what i have however my do loop is getting messed up.[/code]
try this:

Code:
loop until time >= 2
See if these programs make any sense. They do the same thing:

*peace*

Meg.

Program #1
[syntax="QBASIC"]
RANDOMIZE TIMER 'seed the RND() function
CLS 'clear the screen
FOR CounterLoop% = 1 TO 2 'start loop which will process 2x
Suit% = INT(RND * 4) + 1 'pick a random suit from 4 choices
SELECT CASE Suit% '\
CASE 1 ' \
PRINT "Clubs" ' \
CASE 2 ' \
PRINT "Diamonds" ' \ depending on the suit,
CASE 3 ' / print an appropriate message.
PRINT "Hearts" ' /
CASE 4 ' /
PRINT "Spades" ' /
END SELECT '/
NEXT CounterLoop% '
SYSTEM 'end the program
[/syntax]

Program #2
[syntax="QBASIC"]
RANDOMIZE TIMER 'seed the RND() function
CLS 'clear the screen
CounterLoop% = 0 'set loop counter to zero
DO 'start loop
Suit% = INT(RND * 4) + 1 'pick a random suit from 4 choices
SELECT CASE Suit% '\
CASE 1 ' \
PRINT "Clubs" ' \
CASE 2 ' \
PRINT "Diamonds" ' \ depending on the suit,
CASE 3 ' / print an appropriate message
PRINT "Hearts" ' /
CASE 4 ' /
PRINT "Spades" ' /
END SELECT '/
CounterLoop% = CounterLoop% + 1 'increment loop counter
LOOP UNTIL CounterLoop% = 2 'exit loop when counter is 2
SYSTEM 'end the program
[/syntax]
This condition is impossible to satisfy if 'card1' is an integer:

Quote:[syntax="QBASIC"]
elseif card1 <2 and card1 > 1 then
[/syntax]

Seeing as you're only incrementing the 'time' variable once this condition is satisfied, it follows that 'time' will never be incremented and hence an infinite loop will result.

Use Meg's code - it's neater and well-structured.

-shiftLynx
thank you :wink:
Quote:the form for what i have is as follows:

Code:
cls
randomize timer

time =0
card1=rnd *3
do
if card1 <1 then
               print " clubs"
              elseif card1 <2 and card1 > 1 then
               print "spades"
               time = time+1
               loop until time =2
               end if
thats the jist of what i have however my do loop is getting messed up.[/code]

There are various problems with your code:

1. The location of your DO means that the same value of CARD1 is used, over and over aqain. If it does not meet the IF criteria, then you will loop endlessly.

2. If you arrange your code in the recommended way, as shown below, you will immediately see that you have your LOOP UNTIL TIME = 2 in the WRONG place!

3. I have changed your code so that it works consistantly, thus:
Code:
cls
randomize timer
time =0

do
  card1 = int(rnd*2)+1
  if card1 = 1 then
    print " clubs"
  elseif card1 =2 then
    print "spades"
  end if
  
  time = time+1

loop until time =2



[/code]