Syntax:
Expression 1 ? Expression 2 : Expression 3
Meaning:
If the Expression 1 is true then the value returned
will be given by Expression 2 otherwise it will be
given by Expression 3
Example of Else – If:
Example of Conditional Operator:
To find the maximum of a and b:{" "}
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,max;
printf(“Enter a and b: ”);
scanf(“%d %d”,&a,&b);
max = (a>b) ? a : b;
printf(“Maximum is %d”,max);
return;
}
Here the statement,
max = (a>b) ? a : b;
means,
if (a>b)
max = a;
else
max = b;
- If Statement
- Multiple if statement
- If else statement
- Nested if else statement
- Else if statement
- Conditional Operator
- The switch statement