im not getting the program n its output for ternary operator plzzzzzzzzzzzzzzz help me

## 5 Replies - 3909 Views - Last Post: 29 December 2009 - 10:57 AM

##
**Replies To:** ternary operator

### #2

## Re: ternary operator

Posted 29 December 2009 - 08:32 AM

printf("result: %s\n", isTrue ? "true" : "false");

is funadamentally the same as

printf("result: "); if (isTrue) { printf("true\n"); } else { printf("false\n"); }

### #3

## Re: ternary operator

Posted 29 December 2009 - 09:23 AM

For reference, ternary operator is also known as a

**conditional operator**.
This post has been edited by **sparkart**: 29 December 2009 - 11:04 AM

### #4

## Re: ternary operator

Posted 29 December 2009 - 10:18 AM

When you use an operator you create what is called an "expression" -- in C/C++ an expression evaluates to some value.

So for example the expression, 5 + 2 evaluates to 7.

The operator + is a binary operator, meaning that it has two argument expressions, a left hand side (LHS) and a right hand side (RHS), so an expression using the + operator looks like this: LHS + RHS

Note that I said "argument expressions" -- that is because the LHS and RHS don't have to be simple values/variables, they can be complex expressions made up of other expressions with other operators. For example:

4 * 5 + 2, LHS = 4 * 5 and the RHS = 2

A ternary operator has three arguments. C/C++ only has 1 ternary operator ( ?: ) and it takes three arguments: conditional-expression, true-expression, false-expression

conditional-expression ? true-expression : false-expression is the syntax for a ternary expression in C/C++.

The

If the

So now some examples:

(a > b ) ? a : b evaluates to the value that is greater, if a > b then the expression evaluates to the value of a, if a <= b then the expression evaluates to the value of b. This makes a nice little max value function:

int max(int a, int b ) { return (a > b ) ? a : b; }

isTrue ? "true" : "false" if the variable isTrue evaluates to true, then the expression evaluates to the const char* to the string "true", else it evaluates to the pointer to "false".

for example to solve the riddle of 99 bottles vs 1 bottle of beer on the wall:

note that in the example above I enclosed the expression in parentheses -- this is to preserve order of operation. The ?: operator has high precedence than << so to ensure that the compiler understood exactly what I meant I used the parentheses.

Sometimes the ternary operator can be vary useful in calculations that have conditional dependencies. For example writing an expression to return the number of days in a month:

The ternary operator is actually quite useful for simplifying code... but it can also make code less readable. For example the daysInMonth logic might be a little more clear if the function had looked like this:

Personally I like the first version...

So for example the expression, 5 + 2 evaluates to 7.

The operator + is a binary operator, meaning that it has two argument expressions, a left hand side (LHS) and a right hand side (RHS), so an expression using the + operator looks like this: LHS + RHS

Note that I said "argument expressions" -- that is because the LHS and RHS don't have to be simple values/variables, they can be complex expressions made up of other expressions with other operators. For example:

4 * 5 + 2, LHS = 4 * 5 and the RHS = 2

A ternary operator has three arguments. C/C++ only has 1 ternary operator ( ?: ) and it takes three arguments: conditional-expression, true-expression, false-expression

conditional-expression ? true-expression : false-expression is the syntax for a ternary expression in C/C++.

The

**conditional-expression**is an expression that evaluates to either true or false. In C/C++ zero means false, and NON-ZERO means true.If the

*conditional-expression*evaluates to true, then the entire ternary expression evaluates to the value of the**true-expression**, else the entire ternary expression evaluates to the value of the**false-expression**.So now some examples:

(a > b ) ? a : b evaluates to the value that is greater, if a > b then the expression evaluates to the value of a, if a <= b then the expression evaluates to the value of b. This makes a nice little max value function:

int max(int a, int b ) { return (a > b ) ? a : b; }

isTrue ? "true" : "false" if the variable isTrue evaluates to true, then the expression evaluates to the const char* to the string "true", else it evaluates to the pointer to "false".

for example to solve the riddle of 99 bottles vs 1 bottle of beer on the wall:

void printBeers(int num) { std::cout << num << ((num == 1) ? "bottle" : "bottles") << " of beer on the wall!" << std::endl; }

note that in the example above I enclosed the expression in parentheses -- this is to preserve order of operation. The ?: operator has high precedence than << so to ensure that the compiler understood exactly what I meant I used the parentheses.

Sometimes the ternary operator can be vary useful in calculations that have conditional dependencies. For example writing an expression to return the number of days in a month:

#include <iostream> #include <cstdlib> #include <ctime> using namespace std; #define THIS_YEAR 2009 bool isLeapYear(int year) { return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); } int daysInMonth(int month, int year) { if (month > 7) { month--; } return (month % 2 == 0) ? (month == 2) ? !isLeapYear(year) ? 28: 29 : 30 : 31; } const char* MonthNames[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", }; enum Months { M_NONE, M_JANUARY, M_FEBRUARY, M_MARCH, M_APRIL, M_MAY, M_JUNE, M_JULY, M_AUGUST, M_SEPTEMBER, M_OCTOBER, M_NOVEMBER, M_DECEMBER, M_ALL }; void testDate(int month, int year) { cout << MonthNames[month] << " of " << year << ((year > THIS_YEAR) ? " will have " : " had ") << daysInMonth(month, year) << " days in it." << endl; } int main() { srand(time(0)); cout << "testing leap years:" << endl; testDate(M_FEBRUARY, 1804); testDate(M_FEBRUARY, 1900); testDate(M_FEBRUARY, 1904); testDate(M_FEBRUARY, 2000); testDate(M_FEBRUARY, 2009); cout << "Testing full year:" << endl; for (int i = M_JANUARY; i < M_ALL; i++) { testDate(i, rand() % 500 + 1900); } return 0; }

The ternary operator is actually quite useful for simplifying code... but it can also make code less readable. For example the daysInMonth logic might be a little more clear if the function had looked like this:

int daysInMonth(int month, int year) { if (month > 7) { month--; } if (month % 2 == 0) { if (month == 2) { if (isLeapYear(year)) { return 29; } else { return 28; } } else { return 30; } } else { return 31; } }

Personally I like the first version...

### #5

## Re: ternary operator

Posted 29 December 2009 - 10:28 AM

Moving off topic, I prefer Bruce Eckel's solution to the days-in-month problem, which is to store two arrays

duplication of the other 11 months doesn't matter since the chance of them ever needing to change are pretty slim

static const int leap[] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; static const int noleap[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

duplication of the other 11 months doesn't matter since the chance of them ever needing to change are pretty slim

### #6

## Re: ternary operator

Posted 29 December 2009 - 10:57 AM

Yea it really is a better solution to the problem. That was just the first calculation that came to mind that requires "choices" -- I am sure I could have found others but that was what came to mind.

Page 1 of 1