Image

C++ - Operator - Conditional Operator

Conditional Operator

C/C++ contains a very powerful and convenient operator that replaces certain statements of the if-then-else form. The ternary operator ? takes the general form

Syntax     :        (Condition? true_side: false_side);
Example :         (A > 50 ?  0  :  1);

In this example, if A is greater than 50, 0 is returned else 1 is returned. This is like if else conditional statements.

x = 10;
y = x>9 ? 100 : 200;
y is assigned the value 100. If x had been less than 9, y would have received the value
200. The same code written using the if-else statement is

Relational Operator

In the term relational operator, relational refers to the relationships that values can have with one another.

>               x > y (x is greater than y)
<               x < y (x is less than y)
>= 		 x >= y (x is greater than or equal to y)
<=		 x <= y (x is less than or equal to y)
==		 x == y (x is equal to y)
!=		 x != y (x is not equal to y)

Logical Operator

In the term logical operator, logical refers to the ways these relationships can be connected. Because the relational and logical operators often work together, they are discussed together here.(logical and &&, logical or || nad not !)

&& Logical AND
|| Logical OR
!  Logical NOT

Increment/Decrement Operator

C/C++ includes two useful operators not generally found in other computer languages. These are the increment and decrement operators, ++ and − −. The operator ++ adds 1 to its operand, and − − subtracts one. In other words:

Bitwise Operator

Unlike many other languages, C/C++ supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming tasks, it needed to be able to support many operations that can be done in assembler, including operations on bits. Bitwise operation refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the char and int data types and variants.

& –   Bitwise AND
| –    Bitwise OR
~ –  Bitwise NOT
^ –   XOR
<< – Left Shift
>> – Right Shift