• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested ifs  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been learning java in school, and we're way past this simple concept, but I couldn't find anything online for it.

In this code what happens?

say



Since the first condition is met, but the nested conditions are not what happens?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how is the first condition met? as far as I know, three is not greater than four.

each if-block is independent. so, assuming your first statement was "if (3<4)", then you go into the block, on line 2. the condition there is evaluated with no knowledge of anything else. It give false, so we drop to line 5. That is also false, so we drop out of that, to line 8. That ends the first if-block, and yes, nothing happens. line 10 is then run with no knowledge of anything previous running.
 
Jonathan Filion
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Fixed it.
If this is true now what?
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonathan, and welcome to Java Ranch!

The first expression is false, so the lines 2 - 7 don't get executed. The expression at line 10 is true, so the block at line 11 does get executed. Does that clear it up?


Greg

ETA: oops, I missed your fix. Now the condition at line 1 is true, so the execution enters the block. However, the conditions at line 2 and line 5 are both false, so lines 3 and 6 still don't get executed.
 
Jonathan Filion
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Hi Jonathan, and welcome to Java Ranch!

The first expression is false, so the lines 2 - 7 don't get executed. The expression at line 10 is true, so the block at line 11 does get executed. Does that clear it up?


Greg

ETA: oops, I missed your fix. Now the condition at line 1 is true, so the execution enters the block. However, the conditions at line 2 and line 5 are both false, so lines 3 and 6 still don't get executed.

Ok so where does the code go from there after it doesn't execute any code inside the if?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the expression at line 5 is determined to be false, the execution will skip over the block enclosed in the braces and jump to the next executable line after the block. In this case, that's line 10.
 
Jonathan Filion
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:After the expression at line 5 is determined to be false, the execution will skip over the block enclosed in the braces and jump to the next executable line after the block. In this case, that's line 10.


But won't it skip over that line since the first condition was already met?

Or is that just when an else if is there?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're right. That's just when there's an else in there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!