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

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

Moderator: admin

Post Reply
Samuel Prette

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

Post by Samuel Prette »

Hi,

Thanks for those great questions.

I don't understand why this will not compile :

while(false){ //do stuff}

And this will compile :

if(false){ //do stuff}

I know that the compiler is not happy about dead code, but why is he happy with if dead code and not with while dead code ?

Thanks again,
Samuel Prette.

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

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

Post by admin »

This is because if(true) is an exception to the rule. Java language designers have allowed this construct to support the feature of conditional compilation.

Please see this for more information: http://docs.oracle.com/javase/specs/jls ... #jls-14.21
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Why would it print true, true?
Is it in general to take if(false) as true?

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

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

Post by admin »

Did you read the detailed explanation? Let me know which part of it do you not understand.
If you like our products and services, please help us by posting your review here.

Deleted User 3513

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

Post by Deleted User 3513 »

Just wanted to reassure myself, the 2nd if statement(if (false)) was ignored because it has a condition of false(because an if will only execute if the boolean condition results to true, right?) and not because it compared the first boolean condition in the first if-statement to the 2nd inner if-statement?(e.g. true == false is false)

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

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

Post by admin »

Not sure if I understand what you are implying correctly but if you see the if loop with formatting as given in the explanation, that will make it very clear:

Code: Select all

      if (true) { <----- Since the if condition here is true, this block will execute
            if (false) { <----- Since the if condition here is false, this block will not execute
                System.out.println("True False");  
            } else { <----- But this else block will execute
                System.out.println("True True");
      } 
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

I don't see how the code can be reformatted as shown in the explanation. If statements without braces only include the first line in the if statement.

Or is it that this first line "comes with extra baggage" (i.e. the rest of the second if statement's code)?

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

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

Post by admin »

The first line in the if statement in this case is another if statement. So yes, it is that first line that "comes with extra baggage".
If you like our products and services, please help us by posting your review here.

Walter
Posts: 5
Joined: Tue Apr 04, 2017 3:35 pm
Contact:

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

Post by Walter »

Hello
I was wondering if this question(40) comes under the banner of the 'dangling else problem' like the code of question 21- 7.2.1173 and the java language allows it.

public class Test{
   public static void main(String[] args){
      if (args[0].equals("open"))
         if (args[1].equals("someone"))
            System.out.println("Hello!");
      else System.out.println("Go away "+ args[1]);
    }
}

Regards
Walter

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

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

Post by admin »

Yes Walter, that is correct.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Seems "else" does not relate here and is unnecessary because when I comment it out, as in code below, still the out put is "True True".

Code: Select all

public class IfTest{
public static void main(String args[]){
if (true)
if (false)
System.out.println("True False");
//else
System.out.println("True True");
}
}

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

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

Post by admin »

Yes, in this case the output doesn't change. But do you understand how the flow works after commenting out else? and how is it different from when else is not commented? Try changing the conditions to understand.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Does it means that a programmer cant execute a line of cod based on falsity of the "if" condition?

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

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

Post by admin »

I am sorry but I am not sure what are you talking about.

If the if condition is false then the if part of the if/else statement will not execute and if the if condition is true then the else part will not execute. It has nothing to do with the statements that follow the if or if/else statement. They will execute as per the normal flow of execute.

If you comment the else, the println statement that is written after else is no more a part of the if/else statement. It is outside the scope of if/else. It will execute as per the normal flow of execution. It has no relation to if/else any more.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Thank you , sir. Very clear.

li_hanglin@bah.com
Posts: 6
Joined: Mon Jan 28, 2019 4:18 pm
Contact:

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

Post by li_hanglin@bah.com »

Isn't curly braces required in if statement for block of multiple statement? How can you rewrite the first if like the rewritten version when you are omitting a required component in the answer?

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

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

Post by admin »

But there are no multiple statements in the outermost if block. There is only one statement and that statement is the if-else statement.
If you like our products and services, please help us by posting your review here.


Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests