Raised This Month: $51 Target: $400
 12% 

for loop question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-20-2012 , 13:07   for loop question
Reply With Quote #1

My APCS teacher (this is Java, but basics) says that returning in the middle of a for is a bad idea, never never do it. He also dislikes break.

For instance, this checks if a value is in an array:

PHP Code:
public boolean found(String key) {
    for(
String value possibleFoos) {
        if(
value.equals(key)) {
            return 
true// he has a problem with this for some reason
        
}
    }
    return 
false;

Is this in fact a bad idea? Or is he just a moron?
__________________
Dr. McKay is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 02-20-2012 , 13:17   Re: for loop question
Reply With Quote #2

break is only used in loops and not anywhere else.
Returning inside a loop is perfectly safe.
__________________
xPaw is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-20-2012 , 15:14   Re: for loop question
Reply With Quote #3

Quote:
Originally Posted by xPaw View Post
break is only used in loops and not anywhere else.
Returning inside a loop is perfectly safe.
Thought so. He is an idiot.

For the record, he was saying that for this loop:
PHP Code:
for(int i 0maxi++) 
Instead of returning within the loop, you should set i to max so it exits the loop. WHICH IS THE SAME EXACT THING AS USING break;

*facepalm*
__________________
Dr. McKay is offline
matsi
Thinkosaur
Join Date: Sep 2006
Old 02-20-2012 , 17:17   Re: for loop question
Reply With Quote #4

Seems like you should be the teacher then?
matsi is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-20-2012 , 18:42   Re: for loop question
Reply With Quote #5

Most people have some reason for saying such things. Ask him why. Sometimes it's more of a preference and they just don't want to deal with grading code that is not what he's used to.
__________________
fysiks is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-20-2012 , 18:58   Re: for loop question
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
Most people have some reason for saying such things. Ask him why. Sometimes it's more of a preference and they just don't want to deal with grading code that is not what he's used to.
He doesn't even grade code. He just makes sure it runs.
__________________
Dr. McKay is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-20-2012 , 19:06   Re: for loop question
Reply With Quote #7

Quote:
Originally Posted by Dr. McKay View Post
He doesn't even grade code. He just makes sure it runs.
I guess he is just stubborn in his ways then. It happens. Everybody has their opinions that they tell their pupils that it's "the correct way." It makes life easier for the teacher .

But, you would have to ask to see what he says to get a good idea of why he says what he does.
__________________
fysiks is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 02-21-2012 , 07:22   Re: for loop question
Reply With Quote #8

Quote:
Originally Posted by Dr. McKay View Post
Thought so. He is an idiot.

For the record, he was saying that for this loop:
PHP Code:
for(int i 0maxi++) 
Instead of returning within the loop, you should set i to max so it exits the loop. WHICH IS THE SAME EXACT THING AS USING break;

*facepalm*
And more intensive since
a) it has to wait for the loop to complete (so no early exits) and then jump back up to the condition (not an issue in do..while, would be solved by optimization if not for: )
b) the compiler can't know you're exiting the look so it's fucking impossible for it to optimize (not as much an issue in Java where hardly any bytecode optimizations are done, but hey, it's all about leaning theory in the end!).

I don't think Java (at least not Oracle's compiler) does loop unrolling, but in a lot of compiled languages the compiler will replace the loop with a series of repeated blocks if it has a constant number of iterations (and if configured to prefer speed over size), this tells you just how much expensive jumps are to process. Manipulating the exit condition so it exits naturally just fucks all this up.
__________________

Last edited by asherkin; 02-21-2012 at 07:23.
asherkin is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-21-2012 , 17:33   Re: for loop question
Reply With Quote #9

Quote:
Originally Posted by asherkin View Post
I don't think Java (at least not Oracle's compiler) does loop unrolling, but in a lot of compiled languages the compiler will replace the loop with a series of repeated blocks if it has a constant number of iterations (and if configured to prefer speed over size), this tells you just how much expensive jumps are to process. Manipulating the exit condition so it exits naturally just fucks all this up.
My understanding is that the server JVM's JIT compiler (bytecode to native code) can do loop unrolling, but the source code to bytecode compiler doesn't.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
XINLEI
me too
Join Date: Jun 2011
Location: Colombian Coffee storage
Old 02-21-2012 , 19:02   Re: for loop question
Reply With Quote #10

I had the same question not so long about this. She explain me that "for" loops are useful when you know the start and the end of what are you going to do, although she dislikes returns in a for loop. Because she says it loses the main purpose of making for loops, and that's one of the reasons of why they are 3 ways to make loops.

Last edited by XINLEI; 02-21-2012 at 19:03.
XINLEI is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:15.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode