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

for loop question


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

Quote:
Originally Posted by XINLEI View Post
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.
But say you're writing a function that checks if a value is present in an array, and it returns true or false. You know how many times you want to loop, the size of the array. However, if the value is found, there's no point in continuing, so you can return.
__________________
Dr. McKay is offline
XINLEI
me too
Join Date: Jun 2011
Location: Colombian Coffee storage
Old 02-21-2012 , 21:39   Re: for loop question
Reply With Quote #12

Quote:
Originally Posted by Dr. McKay View Post
But say you're writing a function that checks if a value is present in an array, and it returns true or false. You know how many times you want to loop, the size of the array. However, if the value is found, there's no point in continuing, so you can return.
Yes, i'm ok with that, and sometimes i do this. This is just to clarify concepts.

Quote:
For the record, he was saying that for this loop:

for(
int i = 0; i < max; i++)


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;
I've seen people with these habit. I think it's too paranoic making sure the loop completes itself like this, but there's no problem at all with it (perhaps in their decade it does matters when coding in BASIC or COBOL or another old language. Not sure).
XINLEI is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-22-2012 , 08:50   Re: for loop question
Reply With Quote #13

Quote:
Originally Posted by Dr. McKay View Post
Or is he just a moron?
That would be yes.
Quote:
Originally Posted by Dr. McKay View Post
Instead of returning within the loop, you should set i to max so it exits the loop.
Never do this.
Quote:
Originally Posted by XINLEI View Post
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.
You are in the point of the issue, do not listen to her though, that is just typical false reasoning.
Quote:
Originally Posted by XINLEI View Post
I think it's too paranoic making sure the loop completes itself like this, but there's no problem at all with it (perhaps in their decade it does matters when coding in BASIC or COBOL or another old language. Not sure).
But there is a problem, a huge.

There are absolutely no other purposes for choosing for or while other than readability ( if even that ). If there is even slightest difference in performance in ones implementation after loop type change ( which is one's compiler creates different assembly code with different loop types ) then the one should probably get a better compiler. There is no difference between for and while in any level but in someone's imagination.

Even the readability is arguable and the whole for-while division is unnecessary. What we could simply have is:
PHP Code:
loopexpression ) { }
loopstatementexpression ) { }
loopstatementexpressionstatement ) { } 
Even better replace the word "loop" with some easy symbol at the same time every other unnecessary long word in general programming languages.

What comes to the other issues: Java is so slow no one should care how loops are implemented there anyway. Breaks should be avoided like all jumps as linear code is much more easier to read, yet in the specific issue break is obviously the best option. I would also like to point out how you should avoid using continues which is kind of general in here. If one should return inside a loop then one should.

Last edited by SnoW; 02-22-2012 at 08:52.
SnoW is offline
Send a message via MSN to SnoW
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 02-22-2012 , 09:17   Re: for loop question
Reply With Quote #14

Quote:
Originally Posted by Powerlord View Post
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.
Nice find.

Quote:
Originally Posted by XINLEI View Post
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.
"for" is just syntactic sugar around while loops.

Code:
for (int i = 0; i < 10; i++) {
    /* loop body */
}
is the same as

Code:
while (i < 10) {
    static int i = 0;
    /* loop body */
    i++;
}
Quote:
Originally Posted by SnoW View Post
Java is so slow no one should care how loops are implemented there anyway.
The question was about for loops, not for loops in Java.
Dr. McKay even specified that he was asking about theory behind it not specific to Java.

Quote:
Originally Posted by SnoW View Post
Breaks should be avoided like all jumps as linear code is much more easier to read, yet in the specific issue break is obviously the best option. I would also like to point out how you should avoid using continues which is kind of general in here. If one should return inside a loop then one should.
Using a looping construct inherently makes it non-linear code, so that's a bit silly a statement.
__________________

Last edited by asherkin; 02-22-2012 at 09:45. Reason: Corrected scope issue.
asherkin is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-22-2012 , 11:18   Re: for loop question
Reply With Quote #15

Quote:
Originally Posted by asherkin View Post
The question was about for loops, not for loops in Java.
Dr. McKay even specified that he was asking about theory behind it not specific to Java.
I know the thread focused on loops in general ( is that not obvious? ) and that is exactly why I pointed out your commenting on Java loops unnecessary. Duh.
Quote:
Originally Posted by asherkin View Post
Using a looping construct inherently makes it non-linear code, so that's a bit silly a statement.
That is not a silly statement at all if you just little think of it. But I am just fine explaining it to you, no worries. What I said was:
Quote:
Originally Posted by SnoW View Post
Breaks should be avoided like all jumps as linear code is much more easier to read, yet in the specific issue break is obviously the best option.
Nowhere not even near in this statement I refer any way to loops in general or point out that loops should not be used because of their ability of breaking linearity. Loops are needed in many cases and when needed they should be used. My statement does not any way deny that. Then again my statement states that breaks should be avoided, because they break linearity inside a loop ( I think this is the part you did not get, so I will highlight you to notice how this has nothing to do with the loop breaking the outer linearity ). Then I state that when there is no other option for break, it should be used.

If something is still unclear I will be happy to help.

Last edited by SnoW; 02-22-2012 at 11:20.
SnoW is offline
Send a message via MSN to SnoW
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-22-2012 , 18:49   Re: for loop question
Reply With Quote #16

Quote:
Originally Posted by SnoW View Post
What I said was:

Nowhere not even near in this statement I refer any way to loops in general or point out that loops should not be used because of their ability of breaking linearity. Loops are needed in many cases and when needed they should be used. My statement does not any way deny that. Then again my statement states that breaks should be avoided, because they break linearity inside a loop ( I think this is the part you did not get, so I will highlight you to notice how this has nothing to do with the loop breaking the outer linearity ). Then I state that when there is no other option for break, it should be used.

If something is still unclear I will be happy to help.
The way you stated it originally made it easy to infer that you meant that for loops without breaks were linear code.
__________________
fysiks is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-23-2012 , 06:29   Re: for loop question
Reply With Quote #17

Quote:
Originally Posted by fysiks View Post
The way you stated it originally made it easy to infer that you meant that for loops without breaks were linear code.
If you say so. But even if I had said exactly that loops without breaks are linear code, I do not think that it would have been very wise to assume my meaning to be that anyway. If I said that two plus two was three, I cannot think of Exo coming and sincerely pointing out that it is not.

Last edited by SnoW; 02-23-2012 at 06:29.
SnoW is offline
Send a message via MSN to SnoW
Scherzo
Senior Member
Join Date: Feb 2007
Location: Kwidzyn, Poland
Old 02-23-2012 , 10:42   Re: for loop question
Reply With Quote #18

I dont understand, what is wrong in breaking loop? Since performing repeatable code also require jumps, jump in other place as usually is not an argument there. I suppose its nothing more than philosophy of coding.
Scherzo is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-24-2012 , 16:32   Re: for loop question
Reply With Quote #19

Quote:
Originally Posted by SnoW View Post
What comes to the other issues: Java is so slow
While Java isn't my favorite language to work with, the 1990s called; they want their stereotype back. Although I'm not sure I should say stereotype since it was actually true back then...

Quote:
Originally Posted by Scherzo View Post
I dont understand, what is wrong in breaking loop? Since performing repeatable code also require jumps, jump in other place as usually is not an argument there. I suppose its nothing more than philosophy of coding.
Personally, I have no problems with breaking out of a loop. The only loop construct that you may not want to do that in is a while loop with an exit condition you can easily check... but even then it's sometimes useful.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-26-2012 , 05:55   Re: for loop question
Reply With Quote #20

Quote:
Originally Posted by Powerlord View Post
While Java isn't my favorite language to work with, the 1990s called; they want their stereotype back. Although I'm not sure I should say stereotype since it was actually true back then...
You are obviously wrong. JIT-compiled Java is and will always be significantly slower than any considerable compiled language such as C. I am not saying one should not use Java just because there are more faster implementations available. There are many good reasons to use different languages and performance is not always important. What I am though saying is that if performance is so important you need to do insignificant performance improvements, then building on slow language in the first place is not probably the best thing to do.
SnoW is offline
Send a message via MSN to SnoW
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 07:20.


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