-1

I am trying to run the below code with x =10 ."X is not equal to 10!" is getting printed. but As per documentation, the else belongs to the innermost if()? Also if we replace "1==2" in the if block with 1==1 then also "X is not equal to 10!" is getting printed. I have corrected the code ..
Edit : Got the answer.

   if(1==2)
        if(x!=10)
            System.out.println("X is equal to "+x);
       else
        System.out.println("X is not equal to 10!");
Community
  • 1
  • 1
parteek goyal
  • 41
  • 1
  • 5
  • You are missing braces `{}` for the outer `if` – Thiyagu Jul 10 '18 at 17:21
  • The code being run for `if(1==2)` is just `System.out.println(x);`. Use curly braces – ernest_k Jul 10 '18 at 17:22
  • I suggest using curly braces and an IDE. They will help you debug these kinds of issues. – Sam Orozco Jul 10 '18 at 17:26
  • If `x = 10`, then `x!=10` is false so the `else` block is executed, and it will print `X is not equal to 10!`. The fact that the text printed is incorrect is because you wrote it that way. – Andreas Jul 10 '18 at 17:28
  • So down voting just because your confused or don't like the question. Its a legitimate question and we aren't all so busy we can't handle repeat questions. If that was the case there wouldn't be three answers and 5 comments in a matter of 5 minuets... just saying. – Rob Jul 10 '18 at 17:29
  • @Rob It's a badly written question stating how the code works, which we can see for ourselves, and then asking *"Any thoughts on it ?"* My thought is: The code works as written. Don't know what thoughts OP is looking for, because I have no idea what thoughts OP has about the code. What did OP expect to happen differently? No idea, because the question doesn't say what OP expected. Hence down-vote: Question is **unclear**. – Andreas Jul 10 '18 at 17:32
  • @Rob We can all try to *guess* what thoughts OP is asking for, and provide random thoughts about the code, but the question is badly written. It seems that most people believe OP is confused about the fact that anything prints at all (because of missing braces), but it's more likely that OP is confused that code prints "not equal to 10" when value *is* 10, so which is it? What is it **really** that OP is confused about? Missing braces? The incorrect statement of the output? Do you know what is truly being asked? I mean, you haven;t tried to answer it. **Unclear** = downvote. – Andreas Jul 10 '18 at 17:41
  • Let's refer to help center: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Jul 10 '18 at 17:48
  • @Andreas I don't see a reason to answer the question because Yunnosch already answered the question. – Rob Jul 10 '18 at 17:50
  • @parteek goyal You can select an answer as being accepted by checking the check-marks next to them below. – Rob Jul 10 '18 at 17:56
  • @parteek please can you provide a link to the documentation which says "the else belongs to the innermost if". This is simply wrong; it is likely that if it says that, the code in the documentation is different (e.g. no `System.out.println` between the ifs.) – Andy Turner Jul 10 '18 at 18:10

3 Answers3

2

Indent correctly to see

if(1==2)
    System.out.println(x);
if(x!=10)
    System.out.println("X is equal to "+x);
else
    System.out.println("X is not equal to 10!");

I think that answers it already.
But more detailed:

First if is not true, the first println does not get executed (apart from the described second version).
Second if is unconditionally evaluated, is not true, the else is executed.

Or ot reflect it on the quote, there is no "inner" if because there is no if enclosing any other if.

Or another aspect, you seem to think that the output is occurring because the first ifs condition is false, i.e. the else is attached to the first if. It is however attached to the second if because the conditional code influenced by the first if ends at the ;. To have more code influenced by the first if you could introduce a block of several lines, with a pair of {}.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

your control structure is missing enclosing brackets. these are important to enclose the scope of your logic otherwise unless you're careful you'll end up with incorrect logic.

public class flow {
 public static void main(String[] args) {
  int x = 10;
  if(1==2)
   System.out.println(x); <--- this is 'dead code' will not be reached
   if(x!=10)
    System.out.println("x is equal to " + x);
   else
    System.out.println("x is not equal to 10!");
 }
}

furthermore, your answer is printing 'x is not qual to 10!' in the console because your logic is evaluating the 'else' clause. the logic, if(x!=10) print equals to x, but if it is equal to 10 then print not equal to 10.

EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21
-1

Please put open and close curly braces on your code:

if(1 == 2){
    System.out.println(x);

    if(x!=10){
        System.out.println("X is equal to " + x);
    } else {
        System.out.println("X is not equal to 10!");
    }           
}

for more info: Is it bad practice to use an if-statement without brackets?