If vs. While

Started by
22 comments, last by Tom Sloper 12 years, 10 months ago
What I do now is to generally remember language keywords.
---------------------------------------------------
This works inside Visual Studio...

When I need to know more about the keyword I type the word and double click it to highlight.

I can then hit F1 and be taken to the exact keyword help page.

In your example this would give examples of the while and if loops in your language.

So very quickly you can find the information you need.
Advertisement
Using a while loop for code that is supposed to only run once is just bad programming. Use an if statement if you're only meaning to do something once. Use a while loop if you're wanting to iterate over some collection or series of routines that change based on the previous loops execution results until you reach your desired result. That's about it.
Always strive to be better than yourself.
I knew there were nuances that were not crystal clear at first glance...
Thank you everyone very much for sharing your wisdom. :D

[quote name='Durakken' timestamp='1309034476' post='4827682']
The question then is how are you not getting in infinite loops?


The answer, of course, is easy: the loop is not infinite because after some number of iterations (possibly just 1) the condition stops being true.


The pro vs Con is as simple as stepping through the program..


Let's take the if first...

if(collision with trigger) {
ResultingAction();
}

Each line of code is 1 process "more or less" so...

This is 1 process "if(collision with trigger) {"
This is 1 process "ResultingAction();"


So in this code you have 2 processes no matter what.

Now for the while...

This line is processed... "while Collision"
Here's your 2nd process "Resulting Action"


You're thinking..oh same number of lines, same number of processes... nope. It's not the line that is counted, but rather how many times it is gone over and changes based on some formatting... So with the while loop what happens is after that 2nd process the "while collision" is processed again.

This means that the If statement has 2 processes and the while statement has 3[/quote]

Technically correct, although in practice it can be far more complex than that. The important semantic difference is that if statements only execute the associated code zero times or once, whereas loops execute any number of times from 0 to infinity, depending on how long the condition remains true. The overhead of checking the condition must occur after every iteration of the loop.


Is this good? Is it bad? Well... it doesn't really matter but it's bad... Why?
You know that number for your processor? 3.02 GHz That is means you can run 3.02 trillion (i think it's trillion, or maybe billion i forget) processes per second. So 1 extra in billions per second it will be unoticable, unless you do it a lot and there's always the chance with an improperly used loop you get caught in an infinite loop... so it ranges from noone cares to "I'm so sorry i crashed your computer" where as if you just use the if statement you don't have to worry about it
[/quote]

This is where you've ventured into outright incorrectness.

3 GHz, for instance, means three billion (not trillion), i.e. 3,000,000,000 cycles per second. Even the most simple machine instructions can last much longer than one cycle, and some can take literally thousands of cycles. Moreover, the translation from high-level code (especially game scripts) to machine instructions is far from trivial. You know all that time you sit around waiting for your compiler to do stuff? That's the process of translating your code into machine instructions. It isn't simple, and it isn't easy, and it sure as hell isn't a 1:1 mapping from code operations to machine instructions. In fact, a typical game engine scripting language may process several thousand machine instructions just to execute one line of script code - even simple lines. Throw in complexity like cache coherency issues, pipeline stalls, branch mispredictions, and so on, and suddenly that one tiny little "it shouldn't matter" line of code is chewing up millions of CPU cycles, i.e. milliseconds. Do something that lasts 10 milliseconds 100 times, and you have a 1 second delay. Your framerate just went from "hey this game is fun" to "wtf I've been cemented to the floor."

Optimizing code for performance is a very deep art. Yes, it pays not to be wasteful with things like "while(only_happens_once)" loops, but there's another tradeoff to consider here: premature optimization is evil. Wasting your time worrying about something that only happens for a microsecond every 30 seconds is totally unproductive. The important thing when it comes to optimizing code is to profile. Measure (never guess) where your code is genuinely slow, and speed that up; everything else is more or less inconsequential.
[/quote]

Ok, look, I don't want to hi-jack this thread, and I have to point out that since I answered the question MERKB was having CORRECTLY the thread no longer has a point so it really isn't a problem, I have to deal with you and your rudeness.

First quoted area. It's obvious that I know why it isn't happening. The question isn't a matter of how does an infinite loop happen. It is how has he not stumbled into them constantly if he was using whiles in place of ifs. It would happen all the time if he was doing that. You need to read what is be said/asked in the context of how it's given before answering.

Second quoted area. Again. This is obvious that I know this and is what is explained, with different language, but we said the exact same thing. The difference mine is actually easier to grasp as to what the problem is for most people. He's probably read something similar to what you said hundreds, if not thousands of times, and it didn't click, obviously, since he's asking the question.

Third quoted area. Dive into total incorrectness? See this is the only part that I said which is "wrong" partially and I appreciate that you corrected me where I was wrong, but "total incorrectness"? The two things you "corrected me on were?
I said 3.02 GHz = 3.02 Trillion OR Billion processes
You said 3.02 GHz = 3.02 Billion processes
Am I totally wrong there? hrmm nope... That's why i said it was this or that.

I said each line of programming is 1 process
you said each line of programming can be thousands of processes

Am I totally wrong here? No. The fact is I could have kept it to 1 line = 1 process to simplify and get the point across because nebulously saying it thousands of processes really doesn't mean much because our brain can't process the idea of huge numbers well and the less concrete the harder it becomes, but I will be honest that I did think that 1 line = 1 process, but had I thought it out I probably wouldn't have, but I'd still think in terms of 1 line = 1 process for the shear benefit of having a number there that would give me an indication on how efficient the code is running vs how efficient it could be.


So you're rude and incorrect. Plus I bet you're the person who gave the -1 reputation on that post. If you're not still, by calling someone incorrect that ISN'T you confuse people and I think that is an incredibly d-bag thing to do, especially as a moderator.
If your the one who the -1 that post, or to the person who did, I have to point out I'm the person that answered the question and I did so correctly. If you think that deserves negative reputation you are an <expletive> I'm sure you can make up your own name calling. Seriously, I help and you give neg rep? What the hell is wrong with you?

Third quoted area. Dive into total incorrectness? See this is the only part that I said which is "wrong" partially and I appreciate that you corrected me where I was wrong, but "total incorrectness"? The two things you "corrected me on were?
I said 3.02 GHz = 3.02 Trillion OR Billion processes
You said 3.02 GHz = 3.02 Billion processes
Am I totally wrong there? hrmm nope... That's why i said it was this or that.

Cycles. 3.02 billion cycles. This does not mean that in a given second 3.02 billion <insert operation here> can be performed.


I said each line of programming is 1 process
you said each line of programming can be thousands of processes

Am I totally wrong here? No. The fact is I could have kept it to 1 line = 1 process to simplify and get the point across because nebulously saying it thousands of processes really doesn't mean much because our brain can't process the idea of huge numbers well and the less concrete the harder it becomes, but I will be honest that I did think that 1 line = 1 process, but had I thought it out I probably wouldn't have, but I'd still think in terms of 1 line = 1 process for the shear benefit of having a number there that would give me an indication on how efficient the code is running vs how efficient it could be.[/quote]
Again, your simplification is incorrect and thus misleading. Even for a trivial arithmetic operation it could take many hundreds of clock cycles just to retrieve the necessary data from system memory.


So you're rude and incorrect. Plus I bet you're the person who gave the -1 reputation on that post. If you're not still, by calling someone incorrect that ISN'T you confuse people and I think that is an incredibly d-bag thing to do, especially as a moderator.
If your the one who the -1 that post, or to the person who did, I have to point out I'm the person that answered the question and I did so correctly. If you think that deserves negative reputation you are an <expletive> I'm sure you can make up your own name calling. Seriously, I help and you give neg rep? What the hell is wrong with you?
[/quote]
Actually, I thought he was fairly civil and I didn't read any malicious intent into his response. Fact of the matter is, incorrect information (especially in the beginner's forum) only serves to add confusion and misinformation. As a moderator, he has even more on an onus to rectify misleading or outright incorrect information in such a forum. There's no point in getting prickly and defensive about it, just take it in good faith and move on. Rating a post down isn't a personal slight against the poster, it's simply a means of saying "the information in this post was not helpful", that's all. Someone may come along and point out errors in my post and then rate me down, it's no big deal.

[quote name='Durakken' timestamp='1309066948' post='4827793']
Third quoted area. Dive into total incorrectness? See this is the only part that I said which is "wrong" partially and I appreciate that you corrected me where I was wrong, but "total incorrectness"? The two things you "corrected me on were?
I said 3.02 GHz = 3.02 Trillion OR Billion processes
You said 3.02 GHz = 3.02 Billion processes
Am I totally wrong there? hrmm nope... That's why i said it was this or that.

Cycles. 3.02 billion cycles. This does not mean that in a given second 3.02 billion <insert operation here> can be performed.


I said each line of programming is 1 process
you said each line of programming can be thousands of processes

Am I totally wrong here? No. The fact is I could have kept it to 1 line = 1 process to simplify and get the point across because nebulously saying it thousands of processes really doesn't mean much because our brain can't process the idea of huge numbers well and the less concrete the harder it becomes, but I will be honest that I did think that 1 line = 1 process, but had I thought it out I probably wouldn't have, but I'd still think in terms of 1 line = 1 process for the shear benefit of having a number there that would give me an indication on how efficient the code is running vs how efficient it could be.[/quote]
Again, your simplification is incorrect and thus misleading. Even for a trivial arithmetic operation it could take many hundreds of clock cycles just to retrieve the necessary data from system memory.


So you're rude and incorrect. Plus I bet you're the person who gave the -1 reputation on that post. If you're not still, by calling someone incorrect that ISN'T you confuse people and I think that is an incredibly d-bag thing to do, especially as a moderator.
If your the one who the -1 that post, or to the person who did, I have to point out I'm the person that answered the question and I did so correctly. If you think that deserves negative reputation you are an <expletive> I'm sure you can make up your own name calling. Seriously, I help and you give neg rep? What the hell is wrong with you?
[/quote]
Actually, I thought he was fairly civil and I didn't read any malicious intent into his response. Fact of the matter is, incorrect information (especially in the beginner's forum) only serves to add confusion and misinformation. As a moderator, he has even more on an onus to rectify misleading or outright incorrect information in such a forum. There's no point in getting prickly and defensive about it, just take it in good faith and move on. Rating a post down isn't a personal slight against the poster, it's simply a means of saying "the information in this post was not helpful", that's all. Someone may come along and point out errors in my post and then rate me down, it's no big deal.
[/quote]



I have no problem with being corrected WHERE I am wrong. That is not what he did. He said something was totally wrong which wasn't, and implied the rest was wrong as well which wasn't. And again, the simplification is better for thinking and practical purposes as explained.

Marking someone's rep down when they help is wrong. It makes the whole point of that system worthless and it gives people the wrong impression.
Did I answer the question? yes
Did I have intention of doing so thus being helpful? yes
Was I snide, snarky, downing of any one else? no
Then what was the reason for being repped down? As far as I can figure is that someone didn't like the fact that i used more concrete and useful way of thinking about being efficient and I said something is this or that and it was this or that.

And again how is it not helpful? Had I not posted that post THE QUESTION WOULD NOT HAVE BEEN ANSWERED.
And even if I was completely wrong no one else answered the question.. not even, and most importantly, the one who said I was wrong.
Seriously, even if you had said "I'm down repping you cuz have wrong info here" at least make that info be the info that actually matters to the subject at hand.
That's actually a freakin logical fallacy v.v
It never had occurred to me that one could make such an argument from the IF and WHILE statements.

OP: Your question, that at first looks silly for the experienced programmer made me remember that time when I wasn't experienced myself thus the big battle was to stop completing the voids with imagination and start filling them with actual knowledge. From my experience, that journey is totally worth it. Keep up the good battle. :)
[size="2"]I like the Walrus best.
Dude, Durakken, you need to chill out biggrin.gif

Ok, look, I don't want to hi-jack this thread, and I have to point out that since I answered the question MERKB was having CORRECTLY the thread no longer has a point so it really isn't a problem, I have to deal with you and your rudeness.


The simple fact is your answer contained some information which was wrong, and potentially very damaging to people who do not understand what's really going on under the hood. I know you're trying to help by simplifying things for the OP, and I respect that; but that's actually not helpful. Oversimplification is not always a good thing to do. In this case, you provided an explanation that was completely off base in terms of masking the complexity of how actual software optimization works.

This is the beginner's forum. If you read the rules, you'll note that we are quite serious about the purveyance of incorrect and misleading information here, precisely because even well-meaning posts like yours can cause confusion and lead to bad habits that ultimately are damaging to the very beginners we're trying to help.

Lastly, I wasn't at all rude, and I'm rather surprised that you managed to read "rudeness" into my expansion on your reply. This is the internet, man; grow a thicker skin. Not everyone is out to sabotage your reputation and eat your children. Also, word to the wise: don't turn things into personal crusades just because you feel slighted when someone corrects you. I'm not looking for a fight here, and it doesn't look good for you to drag things in that direction. It's also, once again, thoroughly unhelpful to the actual people we're supposed to be here assisting.


First quoted area. It's obvious that I know why it isn't happening. The question isn't a matter of how does an infinite loop happen. It is how has he not stumbled into them constantly if he was using whiles in place of ifs. It would happen all the time if he was doing that. You need to read what is be said/asked in the context of how it's given before answering.[/quote]

I've been reading this thread since the OP was the only poster in it, and have read every individual response. Don't accuse me of not knowing the context. Just because you couldn't see past my agreement and explanation of your question, doesn't mean I was out to get you. The point wasn't to tell you why he wasn't getting infinite loops, it was to tell him because I felt that it might bear some clarification.


Second quoted area. Again. This is obvious that I know this and is what is explained, with different language, but we said the exact same thing. The difference mine is actually easier to grasp as to what the problem is for most people. He's probably read something similar to what you said hundreds, if not thousands of times, and it didn't click, obviously, since he's asking the question. [/quote]

Look, plenty of people already gave technically correct answers to the question. I was just rephrasing your post to try and give a different perspective, so that it would increase the odds of one of our explanations making sense for the OP. I fail to comprehend how you could even take this personally.

Third quoted area. Dive into total incorrectness? See this is the only part that I said which is "wrong" partially and I appreciate that you corrected me where I was wrong, but "total incorrectness"? The two things you "corrected me on were?
I said 3.02 GHz = 3.02 Trillion OR Billion processes
You said 3.02 GHz = 3.02 Billion processes
Am I totally wrong there? hrmm nope... That's why i said it was this or that.[/quote]

I corrected the trillion/billion part, yes, but the part that was important came next.


I said each line of programming is 1 process
you said each line of programming can be thousands of processes

Am I totally wrong here? No. The fact is I could have kept it to 1 line = 1 process to simplify and get the point across because nebulously saying it thousands of processes really doesn't mean much because our brain can't process the idea of huge numbers well and the less concrete the harder it becomes, but I will be honest that I did think that 1 line = 1 process, but had I thought it out I probably wouldn't have, but I'd still think in terms of 1 line = 1 process for the shear benefit of having a number there that would give me an indication on how efficient the code is running vs how efficient it could be.[/quote]

Yes, you're totally wrong.

Seriously. As I mentioned earlier, the point here isn't that you made a simplification; the point is that your simplification is misleading and potentially damaging to people trying to understand how to optimize their software.

GDNet, in case you hadn't ever noticed, is ranked extremely well in search engines like Google. This means that people will probably be stumbling across this thread in several years' time, long after neither of us is paying attention or available to dispel misconceptions. It is a core responsibility of posting on a long-lived internet forum to ensure that your information is factually accurate. You have something of a duty here to ensure that you're being helpful, lest you (god forbid) get down-rated for peddling inaccuracies. The rating system exists to act as a control mechanism precisely so that people think twice before they post. It's not a personal remark about your manhood, for goodness sake, it's an indicator that your post was not helpful.


Yeah, I downrated your post. And I'd do it again.

And you know what? I hope you would do the same if you found something as blatantly misguiding as your post in someone else's history - even mine. That's what the system is for.



So you're rude and incorrect. Plus I bet you're the person who gave the -1 reputation on that post. If you're not still, by calling someone incorrect that ISN'T you confuse people and I think that is an incredibly d-bag thing to do, especially as a moderator.
If your the one who the -1 that post, or to the person who did, I have to point out I'm the person that answered the question and I did so correctly. If you think that deserves negative reputation you are an <expletive> I'm sure you can make up your own name calling. Seriously, I help and you give neg rep? What the hell is wrong with you?
[/quote]

You're hardly the only person in here with a correct answer. Seriously, stop acting like you're the victim of some grand persecution here. You posted something that wasn't true, and you got called out for it. Man up and deal.

Also, insulting other members of the community is very much frowned upon, particularly moderators. I suggest you take some time to cool down before posting unpleasantries in the future. Or, if you prefer, I can ask another mod to review the situation, who may very well issue you a warning for violating the For Beginners rules; up to you.



I have no problem with being corrected WHERE I am wrong. That is not what he did. He said something was totally wrong which wasn't, and implied the rest was wrong as well which wasn't. And again, the simplification is better for thinking and practical purposes as explained.


You're welcome to your opinion that your simplification is "better", but you are in no way entitled to assert that it is objectively true for everyone everywhere. At least two people disagree that your simplification was helpful, so maybe you need to reconsider your certainty about the whole thing, hmm?

Also, I specifically went out of my way to flag the portions of your post I felt were correct and OK, so don't try and paint me with this brush like I'm going around systematically undermining your reputation or something.


Marking someone's rep down when they help is wrong. It makes the whole point of that system worthless and it gives people the wrong impression.
Did I answer the question? yes
Did I have intention of doing so thus being helpful? yes
Was I snide, snarky, downing of any one else? no
Then what was the reason for being repped down? As far as I can figure is that someone didn't like the fact that i used more concrete and useful way of thinking about being efficient and I said something is this or that and it was this or that.[/quote]

Did you answer the question? Sure. Did you intend to be helpful? No question. Were you rude or a jerk about it? Not at all. Were you right? Hell no.


That is why I down-rated your post. Like I said above, the post ratings are not a judgment of the value of your eternal soul; they're an indicator that the post contains potentially incorrect, misleading, confusing, or damaging information.

Did your post meet those criteria? Yes.


And again how is it not helpful? Had I not posted that post THE QUESTION WOULD NOT HAVE BEEN ANSWERED.
And even if I was completely wrong no one else answered the question.. not even, and most importantly, the one who said I was wrong.
Seriously, even if you had said "I'm down repping you cuz have wrong info here" at least make that info be the info that actually matters to the subject at hand.
That's actually a freakin logical fallacy v.v
[/quote]

There are, at my current count, five people who answered the OP's question. You're not some kind of lone hero who's the only face of justice and helpfulness in the wild, untamed forum lands.

And I defy you to back your claim that I didn't answer the question either.


Lastly, you need to go look up what a "logical fallacy" is, because you're wrong about that, too wink.gif

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


stuff


No you no one answered what the pros and cons were and no one was going to because looking at other posts through out the forum the next stage of inquiry was going to be "let's see some code" because you weren't actually understanding what was being asked and believed that the person was having problem with a specific code and asked the question in a funny way. And you obviously can't read context because well i pointed that out that the first thing i asked that you responded to you didn't get it.


see here's the problem, You just admitted that I AM RIGHT, and then you try to say I'm totally wrong. Totally wrong means that I am wrong about every point about in the entire thing i said. Am I? NO. And the way you said it implies, like I said before, that the rest of what I said was wrong.

The point that you are saying is not the question or answer of the post and it is insignificant information really. I mean what you did is akin to someone asking someone what color the sky is and them answering that sky has no color and that it is simply a process of reflection and how our eyes take in various wavelengths of light that make it the color blue, red, or orange, and then you smacking them upside the head because thy used the word reflection instead of refraction. Is it wrong to say reflection rather than refraction? yes, but is the person totally wrong? Hell no, and that person should be encouraged for going out of their way to give an answer that is far more informative and accurate than just saying blue. The refraction part is insignificant information to the question being asked, but the answer is still correct.

So what then should you do in this situation? Do you tell them they are totally wrong? NO
You tell them that it's refraction and not reflection and explain the difference. And you could even try to explain how the terms got confused to the person, dependent upon the situation. And because of the added information that could lead to greater understanding you tell them that they are doing a good job.

If I'm to take what you said then I am forced to believe that my analogy is correct and you are saying that I am "totally" wrong because I said reflection instead of refraction instead of saying, great answer but here's where you messed up, you are saying crap answer you fail. That is counter productive to what this site is trying to do and is flawed way of doing things, which is why we don't do things that way.

[quoted]
[color=#1C2837][size=2]GDNet, in case you hadn't ever noticed, is ranked extremely well in search engines like Google. This means that people will probably be stumbling across this thread in several years' time, long after neither of us is paying attention or available to dispel misconceptions. It is a core responsibility of posting on a long-lived internet forum to ensure that your information is factually accurate. You have something of a duty here to ensure that you're being helpful, lest you (god forbid) get down-rated for peddling inaccuracies. The rating system exists to act as a control mechanism precisely so that people think twice before they post. It's not a personal remark about your manhood, for goodness sake, it's an indicator that your post was not helpful.
[/quoted]

As i pointed out. I have no problem being corrected where I am wrong.
But you do point out what the problem is. My post IS helpful and answers the question that the poster was asking ACCURATELY.
So what you did you used a system that was meant to lead people away from wrong information away from RIGHT information.
Do you see the problem?
And it's made worse now by the fact that you are now stating that you agree with the important pertinent part of what was posted.

But hey, some of us get down repped when we post helpful accurate information and others get up repped when we're d-bags because we're moderators.
And as far as others answering is question, sorry, no, they didn't. We established that he already knew what a a while statement an if statement did. repeating to someone what they already know does not answer a question. The question was about the pros and cons of such things and which is better to use and why. For someone that "read" what was posted you sure can't read.
And it's made worse now by the fact that you are now stating that you agree with the important pertinent part of what was posted.[/quote]
One's entire post should be important and pertinent. There were parts that were correct and parts that were incorrect. This is a tremendously far cry from "totally correct". In mathematics, this wouldn't even be dignified with "partially correct": It'd just be wrong.

And this is how you're being rated. Confirming oversimplifications, inaccuracies, and outright errors could've gotten those confirmations rated up, possibly ending up with a total increase.

Instead we have a tangential, inflammatory, combative, and name calling line of drivel in response to slights that only you perceived, that I doubt convinces any to believe you when you say "I don't want to hi-jack this thread". There are better venues if you truly meant that: Including bringing up ApochiPiQ's supposedly insulting behavior up with him or another moderator directly.

And this is how you're continuing to be rated.

This topic is closed to new replies.

Advertisement