Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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.

2. ## 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.
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:
```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.

3. ## 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?

4. ## 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.
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.

5. ## Re: Questions about if statements and if-else statements

So using something else as a similar example:

```
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.

6. ## 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.