Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

hello_there

if else

This topic is 5349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

if(mark >= 50) // should really BRACE HERE
if(mark >= 80)
//do stuff <<< condition mark >= 50 && mark >= 80
else
//do stuff <<< condition mark >= 50 && mark < 80
else // CLOSES BRACE
//do stuff <<< condition mark < 50

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

if(mark >= 50)
{
do stuff <<< condition mark >= 50
}
else if (mark >= 80)
{
do stuff <<< condition mark < 50 && mark >= 80 ????
}
else
{
do stuff <<< condition mark < 50 && mark < 80
}


Err, these are DIFFERENT


#include <iostream>

using std::cout;
using std::endl;

void grade_1 (int mark)
{
cout << "Grade 1 is ";
if (mark >= 50)
if (mark >=80)
cout << "Grade 80+";
else
cout << "Grade 50+";
else
cout << "Grade F";
cout << endl;
}

void grade_2 (int mark)
{
cout << "Grade 2 is ";

if (mark >= 50)
cout << "Grade 50+";
else if (mark >= 80)
cout << "Grade 80+";
else
cout << "Grade F";
cout << endl;
}

int main()
{

for (int mark=45; mark<100; mark+=25)
{
cout << "Mark " << mark << ''\n'';
grade_1 (mark);
grade_2 (mark);
}

}

Mark 45
Grade 1 is Grade F
Grade 2 is Grade F
Mark 70
Grade 1 is Grade 50+
Grade 2 is Grade 50+
Mark 95
Grade 1 is Grade 80+
Grade 2 is Grade 50+

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!