4 Replies - 755 Views - Last Post: 28 February 2008 - 12:42 AM Rate Topic: -----

#1 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 15-August 07

conditional operator

Posted 27 February 2008 - 10:19 PM

while using conditional operator, should there always be an else statement?
if not, what is the syntax?
can we just write the 1st expression and leave it by putting a colon after it and no else statement?
Is This A Good Question/Topic? 0
  • +

Replies To: conditional operator

#2 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: conditional operator

Posted 27 February 2008 - 11:30 PM

you dont need an else statement with conditional operator

the syntax is as follows

(condition) ? (expression1) : (expression2)


where condition is an expression that is tested

if the condition is TRUE, expression1 is executed else expression2

EDIT: hey did yeh meant only using the ? not the colon : ?

if so, i dont think that's possible

This post has been edited by manzoor: 27 February 2008 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 15-August 07

Re: conditional operator

Posted 28 February 2008 - 12:37 AM

wat i meant was if tis is possible
 (condition)? (expressiontrue);

or
 (condition)?(expression true):();



neway thnx...

This post has been edited by dan_ram: 28 February 2008 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: conditional operator

Posted 28 February 2008 - 12:38 AM

To the best of my knowledge no that isn't possible
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5305
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: conditional operator

Posted 28 February 2008 - 12:42 AM

View Postdan_ram, on 27 Feb, 2008 - 10:19 PM, said:

while using conditional operator, should there always be an else statement?
if not, what is the syntax?
can we just write the 1st expression and leave it by putting a colon after it and no else statement?

If the code is getting deep, what I like to do to keep it readable, is handle only one of the conditions. That way I don't have to thumb through a lot of code to find what I was checking in the 1st place. This helps with major nested ifs, use select case statements...

if(condition=="error") {
  printf("Error statement!\n");
  exit 0;
}
// We never reach this code in the event of an error, so if we've gotten this far, it's safe to assume the test was successful


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1