About Question enthuware.ocajp.i.v7.2.1325 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1325 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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
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.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Why would it print true, true?
Is it in general to take if(false) as true?
Is it in general to take if(false) as true?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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.
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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)
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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:
HTH,
Paul.
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");
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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)?
Or is it that this first line "comes with extra baggage" (i.e. the rest of the second if statement's code)?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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.
-
- Posts: 5
- Joined: Tue Apr 04, 2017 3:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Yes Walter, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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");
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Does it means that a programmer cant execute a line of cod based on falsity of the "if" condition?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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 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.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
Thank you , sir. Very clear.
-
- Posts: 6
- Joined: Mon Jan 28, 2019 4:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
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.
-
- Posts: 6
- Joined: Mon Jan 28, 2019 4:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1325 :
I see. Thank you!
Who is online
Users browsing this forum: Bing [Bot] and 103 guests