C++ Ternary Operator

  • This operator is also known as Conditional operator.
  • It is know as ternary because it contains three operands.
  • This operator is represented as ? :
  • It check a condition and depending upon the condition, particular instruction is executed.

Syntax

Condition? Block1 : Block2; 

  • Condition is any relational or logical expression which will check to see which block of statement should be executed.
  • Block1 - Statement will be executed if condition is true.
  • Block2 - Statement will be executed if condition is false.

Program To show use of Ternary Operator.



#include
#include
void main()
{
     clrscr();
     int a,b,c;
     a=45;
     b=80;
     c=a>b?a:b;
     cout<<"c="<<c;
     getch();
}

Output

c=80

Back To Top