Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Loop within a loop?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TipsyCzar




PostPosted: Thu Sep 15, 2016 12:21 pm   Post subject: Loop within a loop?

What is it you are trying to achieve?
<I'm trying to add a loop within a loop>


What is the problem you are having?
<Anything I try brings me to Syntax Error>


Describe what you have tried to solve this problem
<Tried adding names to the loops, like : loop 1, end loop 1>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<The code would look something like this (the code isn't used for anything, only wrote it as an example) :>

Turing:


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
<Turing 4.1>
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Sep 15, 2016 2:06 pm   Post subject: 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:

Turing:

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




PostPosted: Fri Sep 16, 2016 7:38 am   Post subject: 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)

Turing:


%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




PostPosted: Fri Sep 16, 2016 12:44 pm   Post subject: 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


Also OK:
code:

if
    loop
    end loop
end if


Wrong:
code:

loop
    if
    end loop
end if


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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: