# Questions about if statements and if-else statements

• March 22nd, 2014, 11:51 PM
chakana101
Questions about if statements and if-else statements
I have an exam on monday on java programming and i'm still a bit confused about a certain question involving nested if statements, the question is:

What is the output of the following code:

int x= 9;
int y = 8;
int z = 7;

if ( x> 9 )
if ( y > 8)
System.out.println(" x > 9 and z>=7");
else if ( z>= 7 )
System.out.println(" x <=9 and z>= 7" );
else
System.out.println(" x <=9 and z , 7 ");

the answer turned out to be" none of the above ", but I'm confused since when you have a nested if statements like this, why wouldn't it output "x<= 9 and z>= 7" since the condition for z is true?

I get that the condition for 9 being greater than x isn't true, so it moves onto the next if statement, and y is not greater than 8, so wouldn't it jump to the ' else if statement ' and output that? Or would it skip everything past "y>8" since that condition isn't true? Just a bit confused.
• March 23rd, 2014, 02:32 AM
GregBrannon
Re: Questions about if statements and if-else statements
Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.
Quote:

I get that the condition for 9 being greater than x isn't true, so it moves onto the next if statement
Not exactly. In this case, the next if statement is only executed if the first if statement is true. Formatting (and posting) the code 'correctly' may help you see:
Code java:

int x= 9;
int y = 8;
int z = 7;

if ( x > 9 )
{
if ( y > 8)
{
System.out.println(" x > 9 and z>=7");
}
else if ( z >= 7 )
{
System.out.println(" x <=9 and z>= 7" );
}
else
{
System.out.println(" x <=9 and z , 7 ");
}
}

Now examine the code and determine what happens if the first if statement is false.
• March 23rd, 2014, 03:45 PM
chakana101
Re: Questions about if statements and if-else statements
The way my professor presented the problem in the practice exam, is without the brackets. Because they weren't shown, are they implied to be there? Would it still produce the same output without the brackets? This is the syntax that she used to format the problem:

int x= 9;
int y = 8;
int z = 7;

( the periods represent spacing since for some reason it's not showing what i'm typing spaced)

if ( x> 9 )
....if ( y > 8)
..System.out.println(" x > 9 and z>=7");
else if ( z>= 7 )
..System.out.println(" x <=9 and z>= 7" );
else
..System.out.println(" x <=9 and z , 7 ");

Would the exclusion of brackets change the output, or would it still be the same?
• March 23rd, 2014, 03:59 PM
GregBrannon
Re: Questions about if statements and if-else statements
The reason the problem was presented without the brackets was to test your understanding and suck you into making the mistake you made. The results are exactly the same. You can answer your questions by coding a quick example of each and running it.
Quote:

the periods represent spacing since for some reason it's not showing what i'm typing spaced
That's why we ask you to use code tags to display code. The formatting is preserved between code tags.
• March 23rd, 2014, 04:34 PM
chakana101
Re: Questions about if statements and if-else statements
So using something else as a similar example:

Code :

if (score >= 90) grade = 'A';
if (score >= 80) grade = ' B ';
if (score >= 70 ) grade = 'C';
if (score >= 60 ) grade = 'D';

in this case, if i were to input a value for example, because conditions 'A' through 'D' are true, then the code " would only work correctly if the grade <70", since D or F would be the output? just making sure I have the logic correct, I tend to overthink way too much

like If I were to put in 92, conditions A-D are true, so D is printed

if I were to put in 73, conditions c-d are true, d is printed

56 is put in, F is only printed.
• March 23rd, 2014, 05:37 PM
GregBrannon
Re: Questions about if statements and if-else statements
Chained if statements in which only one of the statements can be correct should include 'else'. What you've written is very similar to a switch statement with no breaks and a default result.

Over thinking can be reinforced by and tempered with more coding. You should be turning this thinking into code and wringing out the possible results by playing with that instead of theorizing what might happen and maybe being wrong.