0% found this document useful (0 votes)
44 views2 pages

Conditional Ternary Operator (?)

The document discusses three C++ operators: 1. Logical AND (&&) - If the left expression is true, the combined result is true and the right expression is not evaluated. This can be used to conditionally increment a variable. 2. Conditional/Ternary ( ? ) - Evaluates a condition and returns one value if true and another if false. Used to assign the greater of two values to a variable. 3. Comma ( , ) - Separates multiple expressions where only one is expected. Only the right-most expression is evaluated and returned. Can be used to assign the result of multiple operations to a variable.

Uploaded by

elly_popa7182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Conditional Ternary Operator (?)

The document discusses three C++ operators: 1. Logical AND (&&) - If the left expression is true, the combined result is true and the right expression is not evaluated. This can be used to conditionally increment a variable. 2. Conditional/Ternary ( ? ) - Evaluates a condition and returns one value if true and another if false. Used to assign the greater of two values to a variable. 3. Comma ( , ) - Separates multiple expressions where only one is expected. Only the right-most expression is evaluated and returned. Can be used to assign the result of multiple operations to a variable.

Uploaded by

elly_popa7182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

evaluated).

||

if the left-hand side expression is true, the combined result is true (the right-hand
side expression is never evaluated).

This is mostly important when the right-hand expression has side effects, such as altering
values:
if ( (i<10) && (++i<n) ) { /*...*/ }
increments i

// note that the condition

Here, the combined conditional expression would increase i by one, but only if the condition
on the left of && istrue, because otherwise, the condition on the right-hand side (++i<n) is
never evaluated.

Conditional ternary operator ( ? )


The conditional operator evaluates an expression, returning one value if that expression
evaluates to true, and a different one if the expression evaluates as false. Its syntax is:
condition ? result1 : result2

If condition is true, the entire expression evaluates to result1, and otherwise to result2.
1 7==5 ? 4 : 3
2 7==5+2 ? 4 : 3
3 5>3 ? a : b
4 3.
a>b ? a : b

// evaluates to 3, since 7 is not equal to 5.


// evaluates to 4, since 7 is equal to 5+2.
// evaluates to the value of a, since 5 is greater than
// evaluates to whichever is greater, a or b.

For example:
1 // conditional operator
2 #include <iostream>
3 using namespace std;
4
5 int main ()
6{
7 int a,b,c;
8
9 a=2;
10 b=7;
11 c = (a>b) ? a : b;
12
13 cout << c << '\n';
14 }

In this example, a was 2, and b was 7, so the expression being evaluated (a>b) was not true,
thus the first value specified after the question mark was discarded in favor of the second
value (the one after the colon) which was b(with a value of 7).

Comma operator ( , )
The comma operator (,) is used to separate two or more expressions that are included where
only one expression is expected. When the set of expressions has to be evaluated for a value,
only the right-most expression is considered.
For example, the following code:
a = (b=3, b+2);

You might also like