• 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

difference between | and || operator

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between | and || operator?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The || operator when used between two boolean will use short-circuit evaluation. That is, if the truth of the entire statement can be determined by examining the first operand, then the other operand will not be evaluated. Using the | operator both will be evaluated.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's more | is a bitwise operator too, where as || is just a boolean operator.

10 | 5
//will give you 15


true | false
//will give you true,
//both the operands will be evaluated,
//even though first operand is already true.


10 || 15
//a nice compiler error!


true || false
//as the first operand is true,
//second operand will not be evaluated

Regards,
Priyanka.

[ March 03, 2006: Message edited by: Priyanka Kolamba Patabendige ]

[ March 03, 2006: Message edited by: Priyanka Kolamba Patabendige ]
[ March 03, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic