Wednesday, August 26, 2020

9.1.9 Conditional (?:) Operator

  • Conditional operator is also called ternary operator. 
  • This is a short form of if-else statement.
  • Its general structure is like this.


contion ? stmnt1 : stmnt 2;


If condition is true then the statement will return one or the statement will return 2. An
example of this is being given below.


5>3 ? true : false;



EXAMPLE :-


// Conditional (?:) operators in C.
#include <stdio.h>
int main()
{
int x=2, y ;
y = ( x ==2 ? 3 : 1 ) ;
printf("X value is : %d\n", x);
printf("Y value is : %d", y);
}


OUTPUT


X value is : 2 Y value is : 3



No comments:

Post a Comment

Please do not any spam link in Comment Box