# Conditional Operator in C/C++

235 views

It is also called the ternary operator and used for inline if-else mostly for assignment operations.

### SYNTAX

``````Condition ? expression 2 : expression 3
If Condition is true expression 1 is returned
If Condition is false expression 2 is returned
``````

Example
Say we have following C/C++ if-else statement

``````if (a > b) {
result = x;
} else {
result = y;
}
``````

This can be rewritten as the following statement using the conditional or ternary operator.

``````result = a > b ? x : y;
``````

### Differences between C and C++ behavior

The conditional operator in C++ can return an lvalue, whereas C does not allow for similar functionality. Hence, the following is legal in C++:

``````(true ? a : b) = 1;
``````

To replicate this in C, you would have to resort to if/else, or deal with references directly:

``````*(true ? &a : &b) = 1;
``````
posted May 25, 2014

## Related Articles

C language supports the following bit-wise operators.

``````| – Bitwise OR
& – Bitwise AND
~ – One’s complement
^ – Bitwise XOR
<< – left shift
>> – right shift
``````

The | (OR) operator compares two values, and returns a value that has its bits set if one or the other values, or both, have their corresponding bits set. The bits are compared using the following table

``````1   |   1   ==   1
1   |   0   ==   1
0   |   1   ==   1
0   |   0   ==   0
``````

The & (AND) operator compares two values, and returns a value that has its bits set if, and only if, the two values being compared both have their corresponding bits set. The bits are compared using the following table

``````1   &   1   ==   1
1   &   0   ==   0
0   &   1   ==   0
0   &   0   ==   0
``````

The ~ (Ones Complement or inversion) operator acts only on one value and it inverts it, turning all the ones int zeros, and all the zeros into ones.

The ^ (XOR) operator compares two values, and returns a value that has its bits set if one or the other value has its corresponding bits set, but not both. The bits are compared using the following table

``````1   ^   1   ==   0
1   ^   0   ==   1
0   ^   1   ==   1
0   ^   0   ==   0
``````

The >> (Right shift) and << (left shift) operators move the bits the number of bit positions specified. The >> operator shifts the bits from the high bit to the low bit. The << operator shifts the bits from the low bit to the high bit.

Example

``````#include <stdio.h>

main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c;

c = a & b;       /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );

c = a | b;       /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );

c = a ^ b;       /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );

c = ~a;          /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );

c = a << 2;     /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );

c = a >> 2;     /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
``````

In C we use if, if...else and nested if...else or switch (descried in later part of the article) statements as conditional statements which are one-time decisions making. Check the following flow diagram which explain the code flow -

### Syntax

Case 1

``````if(boolean_expression)
/* statement -  will execute if the boolean expression is true */
/* Next Statement  - will always be executed

or

if(boolean_expression)
{
/* statement or more statement -  will execute if the boolean expression is true */
}
/* Next Statement  - will always be executed
``````

Case 2

``````if(boolean_expression)
/* statement1 -  will execute if the boolean expression is true */
else
/* Statement 2 - will execute if the boolean expression is false */

OR

if(boolean_expression)
{
/* statement1 or more statements -  will execute if the boolean expression is true */
}
else
{
/* Statement 2 or more statements - will execute if the boolean expression is false */
}
``````

### Nested IF

If statement inside if statement is called nested IF or compound if statement. IF can be present in the success part of the IF or else part of the IF.
Case 1

``````if(boolean_expression 1)
if( boolean_expression 2)
{
/* Executes when the boolean expression 2 is true along with boolean expression 1 is true */
}
else
{
/* Executes when the boolean expression 2 is false */
}
``````

Case 2

``````if(boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
/* Executes when the boolean expression 3 is true */
}
else
{
/* executes when the none of the above condition is true */
}
``````

### Switch Case Statement

Consider the following statement which is highly unreadable

``````if(Condition 1)
Statement 1
else
{
Statement 2
if(condition 2)
{
if(condition 3)
statement 3
else
if(condition 4)
{
statement 4
}
}
else
{
statement 5
}
}
``````

Switch case statement is just rewriting the same in a more readable way.

``````switch(expression)
{
case value1 :
body1
break;

case value2 :
body2
break;

case value3 :
body3
break;

default :
default-body
break;
}
next-statement;
``````

Sample Flow of Switch Statement