About Question enthuware.ocajp.i.v7.2.1173 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by admin »

In Java (unlike Python), indentation is meant for humans only. Compiler doesn't care about indentation.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by zhengye1 »

Which means the compiler will always find the nearest "if" to match the "else" no matter what the indentation looks like?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by admin »

That is correct.

Consider the following statement.

Code: Select all

if ( num > 0 )   
  if ( num < 10 ) 
    System.out.println( "aaa" ) ;
  else
    System.out.println( "bbb" ) ;
In the example above, the else is indented with the inner if statement. It prints "bbb" only if num > 0 and num >= 10, which means it prints only if num is 10 or greater.
In the example below, the else is indented with the outer if statement.

Code: Select all

if ( num > 0 )   
   if ( num < 10 ) 
      System.out.println( "aaa" ) ;
else
   System.out.println( "bbb" ) ;
This prints if num <= 0. Depending on which if the else is paired with, you can get different results.

Java doesn't pay attention to indentation. So both examples above are the same to Java, which means that it must pick one version over the other.

Which one does it pick? It picks the first one. This phenomenon of having to pick between one of two possible if is called the dangling else.

Rule

An else matches with the nearest, previous, unmatched if that's not in a block.
By not in a block, I mean that the else is outside the block that the if is inside.

Thus,

Code: Select all

if ( num > 0 )   
  if ( num < 10 ) // Previous unmatched if
    System.out.println( "aaa" ) ;
  else  // Matches with previous unmatched if
    System.out.println( "bbb" ) ;
What if we wanted the else to match with the first if?
Then, we need braces.

Code: Select all

if ( num > 0 )  // Previous unmatched if
{
   if ( num < 10 ) // Unmatched, but in block
     System.out.println( "aaa" ) ;
}
else  // Matches with previous unmatched if not in block
   System.out.println( "bbb" ) ;
The else matches with the previous, unmatched if not in a block. This happens to be the outer if. There is an unmatched if that's the inner if, but that's in a block, so you can't match to it. Notice the else sits outside of the block. An else can not match to a previous if if the else is outside a block, while the previous if is inside.
Another Example

Let's add a second else.

Code: Select all

if ( num > 0 )    // Outer if
  if ( num < 10 ) // Inner if
    System.out.println( "aaa" ) ;
  else  // Matches inner if
    System.out.println( "bbb" ) ;
else  // Matches outer if
   System.out.println( "ccc" ) ;
Using our rule, the first else matches the inner if. Since the inner if is already matched, you can't do any match it with the next else.
The second else must match with the outer if because the inner if is already matched.

Each else must match with a unique if

Every else must match with a unique if. You can't have two or more else matching to the same if. If there is no match, then your program won't compile.
Each else be preceded immediately by a valid if

Furthermore, every else has to have a valid if statement just before it. Fortunately, your program fails to compile if you don't do this.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by Javier »

Hi Paul!
I copied and pasted the program and i am running it and the program is showing
ArrayIndexOutOfBoundsException.
(I am passing in the command line as arguments: closed)
What am I missing Paul?
Thank you so much, I am stuck :(

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by admin »

Most probably your code is not exactly same as the one given in the problem statement.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by Javier »

Paul, of course you were right!
I made a mistake because I put a ";" at the end of the first if statement...
Sorry for my question.
And thank you so much for your quick answer!!

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by Mihai1977 »

Hi, I can not understand something, pls help:
The rule for this case is: "An else matches with the nearest, previous, unmatched if that's not in a block."
The first if() condition fails (args[0] not being "open").
The second if() condition fails too (args[1] doesn't exists).
So, applying the rule, the nearest, previous unmatched(not in a block) if is the second if and the else should match with it. Why it does not print:"Go away" + nothing?

thx.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by admin »

Since the else goes with the previous unmatched if, that means the second if and the else together form one statement. This whole statement makes the if true part of the first if i.e. it will be executed only if the first if condition is true.

There is no else part in the first if. Thus, since the first if condition is false, there is nothing to execute.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by Mihai1977 »

Thx, it's clear now.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1173 :

Post by Dreamweaver »

I think, the rule is that "the else belongs to the closest if"

Post Reply

Who is online

Users browsing this forum: No registered users and 131 guests