Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Precedence

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
My question is which of the following operators has highest precedence?
a). &&
b). ||

Which operator gets first evaluated when both are present in an expression?

in Khalid-Mughal book he has shown && operator having highest precedence than ||.

According to Khalid,the output of the following code should be "false,true,true" but i am getting "true,false,false"(jdk1.4.2_03).
can u tell me why so ?

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true, false, false is correct.

Rewriting the expression with parentheses according to operator priority gives:

( a = true ) || ( ( b = true ) && ( c = true ) )

The first thing executed is the a = true assigment which also evaluates to true. Because || is a shortcut operator, the sub-expression ((b=true) && (c=true)) is not executed, therefore b and c remain at their defaulted values of false.

So a is true, b and c are false.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic