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

Username:   Password: 
 RegisterRegister   
 [tutorial] loops and faking goto line
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: Tue Mar 11, 2003 12:20 am   Post subject: [tutorial] loops and faking goto line

Cervantes wrote a more basic tutorial on loops and forloops that explain things better and is available here
Mine covers a bit more advanced consepts and should be read after understanding of the basics

Loops allow you to loop through a part of your program until a condition, so that you dont have to type up the same code over and over again.

the most basic loop:
code:

loop
put "compsci.ca"
end loop


this will keep on outputing compsci.ca on the screen until program terminates... Alt F4 in most cases Confused

Now knowing of how loops behave (as soon as end loop line is reached, control jumps back to loop line) we can add a little counter in there to tell us how many times we runned through the loop. There are two kinds

a) pre-incroment
thats when you have your counter at the BEGINNING of the loop
code:

loop
counter:= counter + 1
put counter
end loop


and b) post-incroment
thats when you have your counter at the END of the loop
code:

loop
put counter
counter:= counter +1
end loop


the difference? Not much... mostly has to do with where you put your exit statment (read down v ). But some teachers have an issue with what counters you use. Just to compare two, here're their outputs

pre-incroment: 1 2 3 4 5..
post-incroment: 0 1 2 3 4..

Basically you put your counter on the OPPOSITE side of your exit statment (more on that).

EXIT WHEN STATMENT (finally!)
you'd have to exit your loop sooner or later... thats where exit statment comes in. Its a kind of if statment that exits your loop (jumps to end loop line).
code:

loop
counter:=counter+1
put counter
exit when counter = 5
end loop
put "the end"


the output will be 1 2 3 4 5 the end

Now if you would put your exit when statment in the beginning of the loop and used a post-incroment counter, its possible to skip through the whole loop. (you can use an exit to get out of the loop of put a true condition into exit when like exit when 1=1 if you want to get out on purpose).

LOOP TRICKS
There aren't much tricks, but you can use your loops to do something they weren't ment to do... You can fake goto line that turing is missing.

If at sertain point you want to skip a part of the code, you put that code into a loop with exit on the line right before end loop. This way the loop will NEVER repeat, but now you have the power to skip right to the end with another exit statment in the middle of your code!! Very Happy

here's how:
code:

var text:string

put "this will fake a goto line"
loop
put "would you like to skip next few lines? y/n"
get text
exit when text = "y"

put "you chose not to skip this lines"
put "now you will suffer"
put "bwahahaha"
put "some more unwanted lines"

exit
end loop
put "the end"


I suppose you could do the same thing with an if statment, BUT you cant if you want to have 2 or more exit points Wink

More tricks - abusing fake goto line
Good programmer doesnt just memorize programming syntax but understands it. Great programmer explains to others how you can use that sytax in the new ways 8)

More of faking goto line. Before you could only jump farward to the end of the loop. But if you understand basic syntax you can figure out a way to jump BACK few lines Wink Here's how:

code:

var text:string

loop
put "beginning of the program"
     loop
     put "would you like to go back to the beginning? y/n"
     get text
     exit when text = "y"
     
     put "you didnt chose to go to the beginning and fix your errors"
     put "now you have to live in regret"
     put "compsci.ca is cool"

     exit
     end loop

exit when text not= "y"
end loop
put "the end"


In the inside loop, we use the same goto end trick, but now we also have an OUTSIDE loop that will bring us back to the beginning of the program. Notice how outside loop exit when is structured. It will exit the loop ONLY if you chose not to go back to the beginning.

The advantages of being able to skip through a portion of the code (or perhaps go back Wink) will prove useful when aim for efficiency of your program. Such as if you already know the result, there's no point in making computer do unnececary calculations.

Also you gain more control over your code, such as in a game - an ability to go back and change your stats if you made an mistake while chosing them, or being able to exit the intro story at vertually any point.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
atrain




PostPosted: Mon May 16, 2005 6:56 pm   Post subject: (No subject)

only problem is you end up with a ton of loops........
If you constanty need to go to something, i use procedures or it statments


if you have a main loop and a sub loop and a loop inside that it gets anoying...
theguru




PostPosted: Sun Jan 15, 2006 9:27 am   Post subject: (No subject)

atrain wrote:
only problem is you end up with a ton of loops........
If you constanty need to go to something, i use procedures or it statments


if you have a main loop and a sub loop and a loop inside that it gets anoying...


I know what you mean. I've been trying to use that for my 'Rock, Paper, Scissors' program to let the user to go back and fourth between the different screens and I have 6 loops nested with each other. it's probably really inefficient but not like i care Rolling Eyes

You should just use that 'fake goto' trick if you want to verify a users input.
code:

put "enter choice: " ..
get choice
loop
  put "you entered ", choice, " are you sure (n=no)?" ..
  getch (sure)
  exit when ord(sure) ~= <insert ordinal value of 'n' here>
end loop
Raknarg




PostPosted: Wed May 18, 2011 9:16 am   Post subject: RE:[tutorial] loops and faking goto line

I've been doing this for a while and my teacher says its bad programming practice. Would you agree with this?
unoho




PostPosted: Wed May 18, 2011 10:44 am   Post subject: RE:[tutorial] loops and faking goto line

since im feeling like trolling,
in line 2, should be "concepts" rather than "consepts"
and "sertain" should be "certain" under loop tricks

hehe
Tony




PostPosted: Wed May 18, 2011 11:07 am   Post subject: RE:[tutorial] loops and faking goto line

Since I'm feeling like reminding you that the original post was published in 2003 -- except to find high-school level writing (meaning sub-par Laughing )

Also, @Raknarg -- what exactly is "this" in "I've been doing this"? goto is generally considered to be bad due to issues with legibility (spaghetti code from jumping all over the place), and safety (jumping into arbitrary spots). The above code is closer to Java's "goto" (labelled breakpoints), which address both points explicitly, _but_ Turing's code doesn't have labels and relies on tricky exit conditions, so the clarify is possibly not ideal.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
unoho




PostPosted: Wed May 18, 2011 11:50 am   Post subject: RE:[tutorial] loops and faking goto line

ouchhh... burnn..u got me..i didn't see that coming lol
Raknarg




PostPosted: Wed May 18, 2011 3:21 pm   Post subject: RE:[tutorial] loops and faking goto line

Dear god Tony, I was refering to the fake goto line in general. I posted here because there was a topic here already and I didn't have time to make an actual topic. If your going t reprimand me, don't be an ass about it.

I know full well its necro posting, but I beleive there's an exception that if you had something worthwhile to add, its allowed. I felt it was a worthwhile question.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed May 18, 2011 3:35 pm   Post subject: RE:[tutorial] loops and faking goto line

Sorry, the "2003" part of the comment was directed towards unoho. Your question was rather vague in the context it was posted, but I hope that I've addressed the technical parts.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Wed May 18, 2011 3:41 pm   Post subject: RE:[tutorial] loops and faking goto line

Ok thanks.
mirhagk




PostPosted: Wed May 18, 2011 5:09 pm   Post subject: RE:[tutorial] loops and faking goto line

Not if it helps much, but bad programming practice is generally whatever you define it to be. Often there's a constant struggle between speed of writing vs speed of execution. Generally you need to find something that works for you, or rather just listen to what your boss/teacher tells you to do.
Raknarg




PostPosted: Wed May 18, 2011 7:21 pm   Post subject: RE:[tutorial] loops and faking goto line

Oh yeah. I personally don't see the error really, buyt me teacher told me today that he really dislikes it when I do it, so I'll have to find some other means for the time being :/
mirhagk




PostPosted: Wed May 18, 2011 10:39 pm   Post subject: RE:[tutorial] loops and faking goto line

The error is mostly in the fact that it looks messy, and there are ways to do it that look a lot nicer. GOTO's are never really needed, they are just useful, and they were removed from modern languages for a reason. (mostly cuz what happens to local variables? what happens with try/catch blocks? There were too many unanswered questions)
user23




PostPosted: Sun May 22, 2011 1:15 pm   Post subject: Re: [tutorial] loops and faking goto line

Ahh there must be some better / more efficient way to get it done?
Raknarg




PostPosted: Sun May 22, 2011 2:18 pm   Post subject: RE:[tutorial] loops and faking goto line

Well, there's different ways to do it. The way my teacher is telling me to do it is just by using an if statement.

Instead of:
Turing:

loop
     stuff
     exit when (condition)
     more stuff
     exit
end loop


You could use:
Turing:

if (condition) then
     stuff
else
     stuff
     more stuff
end if


Get it?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: