• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Expression Evaluation with && and || operators

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am getting confused with the following example. Eventhough && has higher precedence the expression is evaluated differently. Please explain me.
class Test {
static boolean ant(){
System.out.println("within method");
return true;
}
public static void main(String[] args) {
boolean a , b, c;
a=b=c=false;
System.out.println((a=true) || (b= ant()) && (c=true));
System.out.println(a+ "," + b+"," +c);
}
}
out put is : true
true, false, false
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your clause (a = true) sets variable a to true and returns true. The || (short circuit or) sees true and does not evaluate the remainder. Remember, left to right evaluation.
Bill
 
Anant Jahagirdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
When the Precedence comes in to picure while using && and || operators?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Anant.
I have not been able to observe the evaluation of the right operand to && before its left one in any example I made up. Otherwise the short-circuit operators could not short-circuit.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short circuit behavior of && is to quit if the left operand is false - since the result can't possibly be anything but false. Therefore to see the right operand evaluated you must have a true result on the left.
Bill
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anant, this code shows, at least, that the && operator has a bigger precedence than =

a) v is evaluated to false
b) true is evaluated to true
c) the precedence of = is compared with &&. Thus = is not carried out but instead...
d) m() is evaluated and false is printed
e) && is performed yielding true
f) now the previously obtained true is assigned to v and printed.
[ January 10, 2003: Message edited by: Jose Botella ]
 
Anant Jahagirdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your detailed answers. When short circuit operators are in the expression, the left operand is evaluated first and then if operator is || and evalated operand is true, the expression evaluation process stops. same thing happens for && with false operand.Is this correct?
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that sounds right to me Anant! From what i can tell, && and || are just looking for that short cut. a quick way out. the short circuit. As soon as they see something that says "this aint gonna work" (a false value in the values being evaluated by &&) or "this definately will work" (a true value in the case of ||), then boom, everything is dropped.
[ January 14, 2003: Message edited by: Jasper Vader ]
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic