
-----------------------------------
TipsyCzar
Thu Sep 15, 2016 12:21 pm

Loop within a loop?
-----------------------------------
What is it you are trying to achieve?




loop 1

put  "Enter a number"

loop 2

get number

if number = varname then
put "You gessed correctly"
exit loop 2
elsif number not= varname then
end loop 1
end if



Please specify what version of Turing you are using


-----------------------------------
DemonWasp
Thu Sep 15, 2016 2:06 pm

RE:Loop within a loop?
-----------------------------------
You need one end loop for every loop command, and one exit for every loop that you want to be able to exit. For example:


loop
    put "Enter a number"
    loop
        get number
        if number = varname then
            put "You guessed correctly"
            exit % this will exit the inner loop, but not the outer one
        else
            put "You guessed incorrectly"
        end if
    end loop

    % you should probably have a way to exit this loop too, or it will loop forever
end loop


-----------------------------------
TipsyCzar
Fri Sep 16, 2016 7:38 am

Re: RE:Loop within a loop?
-----------------------------------
Here's the last part of a program I'm working on (note that the loops start are earlier on in the code)



%Play again
put "Your balance is currently $" .. 
    put balance ..
        put " . Do you want to play again with the same numbers? yes/no"
            get answer
            if answer = "yes" then
            end loop
        elsif answer = "no" then
    exit
end if
%all possibilities have ended. Restart the loop
end loop



The first end loop go the error (Syntax Error at "loop". Expected "end if"). The elsif got the error ("elsif" without matching "if"). The end if got the error (Syntax Error at "if". Expected "end loop"). The rest of the code has no errors.[/i]

-----------------------------------
DemonWasp
Fri Sep 16, 2016 12:44 pm

RE:Loop within a loop?
-----------------------------------
You need to match the start of a block structure to its end (like a loop ... end loop or an if ... end if structure). You cannot mix-and-match.

OK:
[code]
loop
    if
    end if
end loop
[/code]

Also OK:
[code]
if
    loop
    end loop
end if
[/code]

Wrong:
[code]
loop
    if
    end loop
end if
[/code]

See how they aren't matched? That's what's wrong.

In the future, please paste all the relevant parts of your code. It's harder to understand what your problem is if I can't see the whole thing.
