Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Tue Oct 16, 2012 8:59 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Tue Oct 16, 2012 10:42 am
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
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Mon May 04, 2015 7:39 am
by subhamsdalmia
Why would it print true, true?
Is it in general to take if(false) as true?
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Mon May 04, 2015 8:17 am
by admin
Did you read the detailed explanation? Let me know which part of it do you not understand.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Sun Feb 05, 2017 7:42 am
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)
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Sun Feb 05, 2017 9:37 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Feb 10, 2017 5:38 pm
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)?
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Feb 10, 2017 9:33 pm
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".
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Apr 07, 2017 4:33 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Apr 07, 2017 9:03 pm
by admin
Yes Walter, that is correct.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Apr 14, 2017 8:51 pm
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");
}
}
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Fri Apr 14, 2017 8:56 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Sat Apr 15, 2017 2:51 am
by mjmsausava
Does it means that a programmer cant execute a line of cod based on falsity of the "if" condition?
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Sat Apr 15, 2017 3:15 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Sat Apr 15, 2017 7:56 am
by mjmsausava
Thank you , sir. Very clear.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Thu Apr 04, 2019 10:00 am
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?
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Thu Apr 04, 2019 10:48 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Posted: Thu Apr 04, 2019 1:35 pm
by li_hanglin@bah.com
I see. Thank you!