Jump to content

I'm stumped by this simple, simple code...

- - - - -

  • Please log in to reply
6 replies to this topic

#1
scottbomb

scottbomb

    Newbie

  • Members
  • PipPip
  • 21 posts
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??

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");

    }

}


#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
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
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
scottbomb

scottbomb

    Newbie

  • Members
  • PipPip
  • 21 posts
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
scottbomb

scottbomb

    Newbie

  • Members
  • PipPip
  • 21 posts
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.

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The indentation is what's confusing... or lack of curly braces.
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
scottbomb

scottbomb

    Newbie

  • Members
  • PipPip
  • 21 posts
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?

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users