• 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

Conditional Operators

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When are conditional (or ternary) operators used? Thanks!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say there are two answers
1) Never
2) when you want to create unreadable code
As I understand it, the ternary operator boolean?condition1:elseCondition was included in Java becuase it existed in C
Since you can easily live without it and ppl who have to read your code afterwards would otherwise hate you, I've learnt to live without it.
You can write horrible things like this tho...

but I don't think anyone will mind if you don't

Dave
(i think the code is more or less correct, I'm certainly not putting too much effort into it)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Ternary Operator requires three operands.
The Java language has one ternary operator, ?:, which is a short-hand if-else statement.
Syntax is
expression1 ? expression2 : expression3
The operator is used to mean that if expression1 is true then the value evaluates to expression 2 else it evaluates to expression 3.
It is used to program conditional logic, assign a value to a variable based on one or more boolean decisions.
At the begining it is bit confusing, but it lets you make powerful decisions with a minimal amount of coding.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
expression1?expression2:expression3
if exp1 is true then expre2 will be executed else expr 3 will get
it is same as
if(exp1)
exp2
else
exp3

bye for now
Rajesh Purohit
INDIA
------------------------------------------------------
visit me at www.geocities.com/rajesh_purohit
 
Karen Michelle
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your inputs!
Regards!
Karen
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic