public class TestClass
{
public static void main(String[] args)
{
int x = 9, y = 8, z = 7;
if (x > 9)
if (y > 8)
System.out.println("x > 9 and y > 8");
else if (z >= 7)
System.out.println("x <=9 and z >= 7"); // indenting added since original post
else
System.out.println("x <= 9 and z < 7");
}
}
6 replies to this topic
#1
Posted 10 March 2011 - 05:22 PM
Please, anyone, help me see where I'm wrong here. I got this problem wrong on a test. According to the test, this code is not supposed to output anything, and it doesn't. I got it wrong because I said it would output the string "x <= 9 and z >= 7". In other words, I expected the second System.out.println statement to execute. Why doesn't it??
|
|
|
#2
Posted 10 March 2011 - 05:32 PM
Well, you didn't post with code tags so I may be wrong, however one reason that you never get to that line is because this else if
is paired with
If you never get inside the y > 8 portion of the code the else if( z >= 7) will never execute.
else if (z >= 7)
System.out.println("x <=9 and z >= 7");
is paired with
if (y > 8)
If you never get inside the y > 8 portion of the code the else if( z >= 7) will never execute.
#3
Posted 10 March 2011 - 05:41 PM
Sorry, I don't know what code tags are and the editor is not displaying my indenting [I just added HTML tags and that seems to now preserve the indenting]. So if I understand correctly, the first comparison, " if (x > 9) " is false, therefore the " if (y > 8) " will never execute. The else if statement is paired with if (y > 8) so it doesn't execute. In that case, why doesn't the last else statement execute? Is it not paired with the first (false) comparison " if (x > 9) " ?
#4
Posted 10 March 2011 - 05:52 PM
The weirdest thing just happened. I added the following line right below the first if statement:
System.out.println("testing string");
It doesn't execute, but the program now executes the if statement:
else if (z >= 7)
System.out.println("x <=9 and z >= 7");
And it printed that line of text "x <=9 and z >= 7"
Now I'm really lost.
System.out.println("testing string");
It doesn't execute, but the program now executes the if statement:
else if (z >= 7)
System.out.println("x <=9 and z >= 7");
And it printed that line of text "x <=9 and z >= 7"
Now I'm really lost.
#5
Posted 10 March 2011 - 05:53 PM
The indentation is what's confusing... or lack of curly braces.
W/o braces, the code should indent like so:
I'd add braces just to get rid of any confusion.
If I'm understanding correctly you might want:
W/o braces, the code should indent like so:
public class TestClass
{
public static void main(String[] args)
{
int x = 9, y = 8, z = 7;
if (x > 9)
if (y > 8)
System.out.println("x > 9 and y > 8");
else if (z >= 7)
System.out.println("x <=9 and z >= 7");
else
System.out.println("x <= 9 and z < 7");
}
}
I'd add braces just to get rid of any confusion.
If I'm understanding correctly you might want:
public class TestClass
{
public static void main(String[] args)
{
int x = 9, y = 8, z = 7;
if (x > 9)
if (y > 8)
System.out.println("x > 9 and y > 8");
else if (z >= 7)
System.out.println("x <=9 and z >= 7");
}
else {
System.out.println("x <= 9 and z < 7");
}
}
}
#6
Posted 10 March 2011 - 07:01 PM
I think I've got it. Referring to the code at the top of this thread:
The "else" statement at the bottom belongs to the "else if" above it. That "else if" never executes because it's "else" part belongs to the "if" above it (y > 8) - which doesn't execute because the preceeding "if" (x > 9) is false.
Am I correct?
The "else" statement at the bottom belongs to the "else if" above it. That "else if" never executes because it's "else" part belongs to the "if" above it (y > 8) - which doesn't execute because the preceeding "if" (x > 9) is false.
Am I correct?
#7
Posted 10 March 2011 - 08:35 PM
I believe so, yes.
It is implied that the else belongs to the second if statement. If you had 2 else statements, the first else would belong to the second if, and the second else would belong to the first if statement.
It is implied that the else belongs to the second if statement. If you had 2 else statements, the first else would belong to the second if, and the second else would belong to the first if statement.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









