Is it better to use:
or:Code:if(stuff) { stuff } else { if(stuff) { stuff } else { stuff } }
I'm not sure if I got that right, but whatever.Code:if(stuff) { stuff } break; if(stuff) { stuff } else { stuff }
Is it better to use:
or:Code:if(stuff) { stuff } else { if(stuff) { stuff } else { stuff } }
I'm not sure if I got that right, but whatever.Code:if(stuff) { stuff } break; if(stuff) { stuff } else { stuff }
the first one since break is used to exit a loop.
Code:while(1) { if(...) break; .... }
Your two pieces of code do completely different things. The first one won't even work outside of a loop or switch. Also, in your first one, if you are in a loop, anything after the first if statement will never be executed because no matter what, the break statement will be reached. You most-likely meant to put the break inside your first if statement. Post the rest of your code and then we can say which you should use (or provide another solution).Originally posted by gcn_zelda
Is it better to use:
or:Code:if(stuff) { stuff } else { if(stuff) { stuff } else { stuff } }
I'm not sure if I got that right, but whatever.Code:if(stuff) { stuff } break; if(stuff) { stuff } else { stuff }
Yeah. I was just wondering. I haven't written the code yet.
I see what I did wrong.
This is the same logically as your first one, but in my opinion is more readable.Code:if(stuff) { stuff } else if(stuff) { stuff } else { stuff }
like everybody else said, the break; won't compile unless it's in a loop... and what's under it won't happen because it breaks out of the loop before it does... for readability, I would set it up like this:
Code:if(true) doSomething if(SomeThingElseIstrue) DoSomethingElse else DoSomethingDifferent
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
I prefer a minor change to jlou's idea:
major_small's idea simply removes the first else, which is probably not a good idea.Code:if(stuff) { stuff } else if(stuff) { stuff } else { stuff }
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Eewww, gross!Originally posted by WaltP
Code:else if(stuff)
andCode:if(cond1) ... else if(cond2) expr2 else ...
Are potentially different depending on your conditions, so make sure you know which one you intend to use. The first will only execute expr2 if cond1==false and cond2==true, and expr3 if both cond1 and cond2 are false. The second will execute expr2 if cond2==true regardless of what cond1 is, and similarly, the execution of expr3 is dependent only on the truth value of cond2.Code:if(cond1) ... if(cond2) expr2 else expr3
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
To each his own, I guess.Originally posted by jlou
Eewww, gross!
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
actually, jlou's idea puts it in, which is a bad idea... check the original codeOriginally posted by WaltP
major_small's idea simply removes the first else, which is probably not a good idea.
what jlou did could make the program skip a check that the programmer wanted done, for example:that doesn't take into account that in this run, both foo and bar are true and the program is supposed to do this and thatCode:if(foo==true) do that; else if(bar==true) //skipped if foo is true do nothing; else do this;
Last edited by major_small; 01-01-2004 at 10:24 PM.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Just did. Original post was:Originally posted by major_small
actually, jlou's idea puts it in, which is a bad idea... check the original code
Code:if(stuff) { stuff } else { if(stuff) { stuff } else { stuff } }No, what jlou did was reformat the code to:Originally posted by major_small
what jlou did could make the program skip a check that the programmer wanted done...
All he really did was change the indentation.Code:if(stuff) { stuff } else if(stuff) { stuff } else { stuff }
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
oh I see... I read the second piece of code, which was meant to do the same thing as the first piece of code... sorry.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT