Skip to main content
gamedev.net
🔒 Locked

if else

Started by hello_there Mar 25, 2004 at 12:12 AM 11 replies 650+ views
Original Post
hello_there
hello_there
Say i have something like this if(mark >= 50) if(mark >= 80) //do stuff else //do stuff else //do stuff how would those if''s and else''s be used. Would it match the first if with the last else and the second if with the first else?(in java)
____________How could hell be worse?
SiCrane
SiCrane
Seriously, how long would it take for you to write a quick test to figure it out? And more importantly, why are you writing code like this? You can use braces with if/else statements for a reason.
Makoy
Makoy
if(mark >= 50)
if(mark >= 80)
//do stuff
else
//do stuff
else
//do stuff

I think you constructed the code incorrectly. The correct code should be look like this.

if(mark >= 50)
{
do stuff
}
else if (mark >= 80)
{
do stuff
}
else
{
do stuff
}

Flamers are worst than Newbies
Flamers are worst than Newbies
aidan_walsh
aidan_walsh
quote:
Original post by Makoy
if(mark >= 50)
if(mark >= 80)
//do stuff
else
//do stuff
else
//do stuff

I think you constructed the code incorrectly. The correct code should be look like this.

if(mark >= 50)
{
do stuff
}
else if (mark >= 80)
{
do stuff
}
else
{
do stuff
}

Flamers are worst than Newbies


Both sets of code do the same thing, and each are perfectly valid ways of doing it. It just comes down to a personal preference.

BTW: Makoy, end tags have a / in them...


SketchSoft OFFLINE | SketchNews OFFLINE | NewKlear Studios



[edited by - doodle_sketch on March 25, 2004 4:15:59 AM]
www.aidanwalsh(.net)(.info)
Ravyne
Ravyne
quote:
Original post by doodle_sketch
quote:
Original post by Makoy
if(mark >= 50)
if(mark >= 80)
//do stuff
else
//do stuff
else
//do stuff

I think you constructed the code incorrectly. The correct code should be look like this.

if(mark >= 50)
{
do stuff
}
else if (mark >= 80)
{
do stuff
}
else
{
do stuff
}

Flamers are worst than Newbies


Both sets of code do the same thing, and each are perfectly valid ways of doing it. It just comes down to a personal preference.

BTW: Makoy, end tags have a / in them...


True, BUT, that assumes that "do stuff" is only one statement( a '';'', not a line essentially.) Brackets would be good, but they''re not necessary.

''else if'' and ''else'' statements are paired with the nearest previous ''if'' statement. Only one ''else'' per ''if'' is allowed. As a matter of style, I personally only use unbracketed ''if'' statements in the innermost level of nested ''if''s.

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]
throw table_exception("(? ???)? ? ???");
Useless Hacker
Useless Hacker
quote:
Original post by Makoy
if(mark >= 50)
if(mark >= 80)
//do stuff
else
//do stuff
else
//do stuff

I think you constructed the code incorrectly. The correct code should be look like this.

if(mark >= 50)
{
do stuff
}
else if (mark >= 80)
{
do stuff
}
else
{
do stuff
}

if (mark >= 80)
{
//do stuff
}
else if (mark >= 50)
{
//do stuff
}
else
{
//do stuff
}





"The ability to speak does not make you intelligent" - Qui-Gon Jinn
[ DGDev - The Delphi Games Development Community ] [ Help GameDev.net fight cancer ] [ Shareaza - The Ultimate P2P Client ]
Evil Bachus
Evil Bachus

if(mark >= 50)
if(mark >= 80)
//do stuff

else
//do stuff

else
//do stuff



is equivalent to...


if (mark >= 50)
{
if (mark >= 80)
{
// do stuff

}
else
{
// do stuff

}
}
else
{
// do stuff

}


You should always use brackets if you have nested ifs. You *have* to use brackets if you want "// do stuff" to be multi-lined.

[edited by - Evil Bachus on March 25, 2004 7:53:02 AM]
fredizzimo
fredizzimo
Well it's quite clear even witout bracets if you use proper indenation.


if(mark >= 50)
if(mark >= 80)
//do stuff

else
//do stuff

else
//do stuff



But personally I would use just brackets around the outer if, because the inner one consits of just one line statements, like this. For symmetry I would put a bracket around the last else too.


if(mark >= 50)
{
if(mark >= 80)
//do stuff

else
//do stuff

}
else
{
//do stuff

}



[edited by - fredizzimo on March 25, 2004 8:32:02 AM]
twanvl
twanvl
quote:
Original post by fredizzimo
Well it's quite clear even witout bracets if you use proper indenation.


if(mark >= 50)
if(mark >= 80)
//do stuff

else
//do stuff

else
//do stuff



But the compiler doesn't look at indentation, so the following code will not do what te indentation sugests:

if(mark >= 50)
if(mark >= 80)
//do stuff

else
//do stuff



Also, there is no need to use nested if statements in this code, the following will do:

if (mark >= 80) {
// do stuff a

} else if (mark >= 50) {
// do stuff b

} else {
// do stuff c

}

(Actually the second if is nested inside the else, but you don't have to worry about that)

[edited by - twanvl on March 25, 2004 8:50:05 AM]
Useless Hacker
Useless Hacker
EDIT: Doh, beaten to it by twanvl.

quote:
Original post by fredizzimo
Well it's quite clear even witout bracets if you use proper indenation.


if(mark >= 50)
if(mark >= 80)
//do stuff

else
//do stuff

else
//do stuff



The problem with that is that if the inner if statement does not have an else, then the outer else will be used for the inner if, which would not be clear from the indentation, i.e.
if (mark >= 50)
if (mark >= 80
//do stuff
else
//do stuff

looks from the indentation to mean:
if (mark >= 50)
{
if (mark >= 80
{
//do stuff
}
}
else
{
//do stuff
}

but is actually the same as:
if (mark >= 50)
{
if (mark >= 80
{
//do stuff
}
else
{
//do stuff
}
}

Basically, always wear braces.

[edited by - Useless Hacker on March 25, 2004 8:54:39 AM]
fredizzimo
fredizzimo
quote:
Original post by twanvl
But the compiler doesn't look at indentation



Yes, that's true, and that's why I said I would personally prefer the second option in my post. As soon as there's more than a single row in the if statement, brackets makes it easier to read, and less chances of getting logical errors. In the example, the whole inner part is part of the if statement, so that's more than one row.

The same thing can be said about Useless Hackers example
quote:

if (mark >= 50)
if (mark >= 80
//do stuff
else
//do stuff



There's more than a single row below the first if, so I would use brackets, for the outer if, and for the outer else too, for symmetry again. But not for the inner if, as there's only a single row below it. Like this

if (mark >= 50)
{
if (mark >= 80
//do stuff
}
else
{
//do stuff
}

That said, I sometimes cheat and use only indentation, like my first example in my previous post, if everything is fully symmetric to start with, like the original example in this thread is. I can't remember when the last time I made an error because of this is, only that's it's very long time ago, so I would say it's quite safe to do it, if you are experienced and know the rules.

The reason why I sometimes cheat, and why I don't like unneccessary brackets for the inner if statements is that it's easier to read code if it's less lines, and you don't need to scroll up and down.

What I really have problem with is code like this, from twanvl's post
quote:

if (mark >= 80) {
// do stuff a
} else if (mark >= 50) {
// do stuff b
} else {
// do stuff c
}


It's really hard to catch misplaced brackets(for me). Generally I have problems with a coding style that puts the { on the same row as the block starts. Somehow I have hard time to see the blocks clearly that way. Also it's the same thing here with catching missing or misplaced brackets, if you forgot the first bracket, it's very hard to see that without compiling.



[edited by - fredizzimo on March 25, 2004 10:15:13 AM]
kdogg
kdogg
This brings up a good point. always use brackets. you''re just gonna confuse yourself otherwise. with brackets, you have paranthesis matching in your editor to help you out with confusion.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.