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 Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




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

@Raknarg -- that only works with a very limited number of (condition)s (as you'd have to write out all of the elseif paths yourself), and if all of the condition values are known ahead of time.

E.g.: (condition)'s value is obtained during the execution of stuff() in a non-deterministic way (such as user input).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




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

Yeah, that's why I like fake goto lines better Razz
mirhagk




PostPosted: Tue May 24, 2011 4:41 pm   Post subject: Re: RE:[tutorial] loops and faking goto line

Raknarg @ Sun May 22, 2011 2:18 pm wrote:
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?


Um How about the ultra simple:
Turing:

stuff
if (not condition) then
     more stuff
end if


I would say that's much cleaner than both options, with less lines of code.
2goto1




PostPosted: Tue May 24, 2011 4:54 pm   Post subject: Re: RE:[tutorial] loops and faking goto line

mirhagk @ Tue May 24, 2011 4:41 pm wrote:


Um How about the ultra simple:
Turing:

stuff
if (not condition) then
     more stuff
end if


I would say that's much cleaner than both options, with less lines of code.


I agree. For increased readability, as a general best practice in all languages, I like to try to name boolean variables with names that imply a positive value. It makes boolean comparisons more readable because you avoid double negatives. For example, the code below tests if someone is of legal driving age:

Turing:

var isNotLegalDrivingAge : boolean := false
if (not isNotLegalDrivingAge) then
     % The person if of legal driving age
end if


It can get messy when you start to code logic containing lots of these double negative conditions. Instead, name your boolean variables with names that imply a positive condition, such as the following:

Turing:

var isLegalDrivingAge : boolean := false
if (isLegalDrivingAge) then
     % The person if of legal driving age
end if
Hoshi




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

I like your Review on loops . It is very helpful for test review! It goes well with Cervantes Loops Tutorial too
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 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: