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
}
Printable View
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).Quote:
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
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
}
Eewww, gross! ;)Quote:
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
To each his own, I guess. ;)Quote:
Originally posted by jlou
Eewww, gross! ;)
actually, jlou's idea puts it in, which is a bad idea... check the original code ;)Quote:
Originally 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;
Just did. Original post was:Quote:
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:Quote:
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
}
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.