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

Moderator: admin

Post Reply
Dan Lee
Posts: 4
Joined: Tue May 20, 2014 12:41 am
Contact:

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

Post by Dan Lee » Sun Jun 01, 2014 5:37 pm

I don't understand this,

boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)

My understanding,
i1 == i2 is false
b1 = i1 == i2 which is "if (false= false)" should be false

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

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

Post by admin » Sun Jun 01, 2014 7:36 pm

Why do you think false = false should compile?
If you like our products and services, please help us by posting your review here.

Dan Lee
Posts: 4
Joined: Tue May 20, 2014 12:41 am
Contact:

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

Post by Dan Lee » Sun Jun 01, 2014 8:30 pm

Hm... I think should be,

if (b1 = false) which is false. Am I correct?

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

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

Post by admin » Sun Jun 01, 2014 9:03 pm

= is assignment operator and false is not a variable. Why do you think you can assign false to false?
"if (b1 = false)" is valid but that is not same as false = false.

if(b1 = false) implies you are assigning false to b1 and so the expression resolves to if(b1) i.e. if(false)

Hence, the complete expression b1 = i1 == i2 resolves like this:
b1 = (2 == 3)
=> b1 = false
=>b1
=>false

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon » Wed Nov 26, 2014 1:25 am

Here I would like to add a point. This question is based on operator precedence, == has higher precedence than =. Hence, 2==3 results into false, which is assigned to boolean variable, b1. so, if(false){}else{} here, else part is evaluated.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

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

Post by Venceslas » Thu Feb 05, 2015 3:54 pm

I agree, I have failed to this question due to operator precedence. I have the feeling that the explanation should explain that JVM interpret the expression like if (b1 = (i1 == i2) )

By the way, is it required from us to know the precedence between the full range of the operators please?

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

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

Post by admin » Thu Feb 05, 2015 9:05 pm

It has now been added to the explanation.


Yes, you need to know operator precedence.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post by NickWoodward » Sat Aug 08, 2015 1:16 pm

thought i'd quickly log in and just echo what the above posters have said.

explaining this question from the perspective of precendence makes it a simple answer, and to date it appears not to have been added (to my version at least!).

although i understand the answer, i don't think the current explanation is very good (sorry!).

fantastic piece of software though, it's a great help!

nick

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

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

Post by admin » Sat Aug 08, 2015 9:39 pm

I see that the explanation is included in question bank version 2.20.
The expression b1 = i1 == i2 will be evaluated as b1 = (i1 == i2) because == has higher precedence than =. Further, all an if statement needs is a boolean. Now i1 == i2 returns false which is a boolean and since b1 = false is an expression and every expression has a return value (which is actually the Left Hand Side of the expression), it returns false which is again a boolean. Therefore, in this case, the else condition will be executed.
If you like our products and services, please help us by posting your review here.

TimothyTuckers
Posts: 2
Joined: Fri Jan 08, 2016 3:06 pm
Contact:

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

Post by TimothyTuckers » Sun Jan 17, 2016 11:19 am

so, basically the first condition is:

if (false) {do this}. since the first condition is false, the program moves to the next step.

in this case the next step is else {do this}

is that a correct way of thinking about it??

thanks in advance.

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

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

Post by admin » Sun Jan 17, 2016 9:29 pm

Yes, that is correct.
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 1 guest