The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

mistervernon (6) [Avatar] Offline
#1
When I run the code on page 253 I get the results below from the boolean if-condition,
based on the respective variables. This seems to suggest to me that, each condition
should be evaluated from top to bottom (outside to inside). But I'm not confident that I'm analyzing this correctly. Can anyone provide me a bit of clarity? Thanks.


class NestedIf {
public static void main(String args[]) {
int score = 110;

if (score > 200) { // if true, continue to next if - if false, go to else
if (score < 400) // if true, continue to next if - if false, go to else
if (score > 300) // if true, continue to println - if false, go to else
System.out.println(1);
else
System.out.println(2);
}
else
System.out.println(3);
}
}

/* returns
1 if int score = 301 thru 399
2 if int score = 201 thru 300
3 if int score = 200 or less
*/
LazLondon (13) [Avatar] Offline
#2
Re: Nested If …. pg.253
Hi mistervernon,

I am not an expert but I think it is indeed evaluated from outside to inside.

In the book says: 'Start working from the inside out, with the innermost if-else statement, matching each else with its nearest unmatched if statement.'

So when you are trying to match the else clause with its if part, then the way to do it is from inside out. But its not the way of the evaluation!

Kind regards,
Laz
mistervernon (6) [Avatar] Offline
#3
Re: Nested If …. pg.253
Absolutey. Agreed. However, there's more to it than that! Just figured it out! smilie))
Specifically, "When an if statement is in the if body, then both the outer if condition and the inner if must evaluate to true before the inner if body can run."

In other words, we need to define what constitutes "the if body" in any case where nested-if constructs are used. For example (as Mala has essentially pointed out in the sample code…), if you execute the code at the bottom of page 252 as written, nothing is returned - because there is no 'else' construct associated with the first if-condition (e.g. if score > 200 evaluates to false nothing happens).

Meanwhile, if we execute the code at the top of page 253 (which is exactly the same code, except that the use of braces forces an 'else' construct to run if score > 200 evaluates to false), the result is entirely different.

Hope that this is clear. Thanks!

Message was edited by:
mistervernon
mistervernon (6) [Avatar] Offline
#4
Re: Nested If …. pg.253 // Amended response ...
Disregard….

Message was edited by:
mistervernon
LazLondon (13) [Avatar] Offline
#5
Re: Nested If …. pg.253
If the question is what the if-body is, it can be

; (just a semicolon)
Other statement (including if statement)
Or block of statements within {}

Thanks for the explanation smilie


Cheers, Laz