Conditional Operator

The conditional operator is also known as ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols that is '?' and ':'. As conditional operator works on the three operands, so it is also known as the ternary operator. The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else'statement is also a decision making statement.

Syntax

Expression!?expression2: expression3;
#include<stdio.h>
#include<conio.h>

void main ( )
{
int a=10,b=5;
clrscr();
c=(a>b)?a:b;
printf (“Biggest Value is=”,c);
getch();
}
Output is:
Biggest value is=10

Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements execute sequentially when there is no condition around the statements. The different types of conditional statements are available in C language.

if statement

Simple if statement is used to verify the given condition and executes the block of statements based on the condition result. The simple if statement evaluates specified condition. If it is TRUE, it executes the statement or block of statements. If the condition is FALSE, it skips the execution. The general syntax and execution flow of the simple if statement is as follows...

Simple if statement is used when we have only one option that is executed or skipped based on a condition.

#include<stdio.h>
#include<conio.h>
void main ( )
{
int a; // Decalring a variable
clrscr();// This function is used to clear the previous output in the output window
printf (“enter a number”);// Printing the output in the output window
scanf (“%d”, &a);
if (a>50) // Checking the condition is true or not
{
a=a*10;
}
printf(“a=%d”,a);
}

Eg1: Input
enter a number 60
Output
a=600

Eg2:
Input
enter a number 20
Output
a=20

If else statement:

  • If - else statement takes care of true as well as false conditions
  • 'Statement block-1' is executed when the condition is true and 'Statement block-2' (or) 'else block' is executed when the condition is false.
Syntax:
if (condition)
{
Statement block1;
}
else
{
Statement block2;
}
Program
/* checking for even (or) odd number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf (“enter a number”);
scanf (“%d”, &n);
if (n%2 ==0)
{
printf (“%d is even number”, n);
}
else
{
printf( “%d is odd number”, n);
}
getch();
}
Eg1:

Input
enter a number 10
output
10 is even number
Eg2:
Input
enter a number 5
output
5 is odd number

Nested if - else statement:

The if-else statement is placed within another if-else statement such methods are called nested if-else statement.

Syntax: if (condition1)
{
  if (condition2)
  {
  Statement block1;
  }
  else
  {
  Statement block2;
  }
}
else
{
  if (condition3)
  {
  Statement block3;   }
  else
  {
  Statement block4;
  }
}
Program: #include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c
printf (“enter the values of a,b,c=”)
scanf (“%d%d%d”, &a,&b,&c)
if (a>b)
{
if (a>c)
printf (“%d is largest”, a)
else
printf (“%d is largest”, c)
}
else
{
if (b>c)
printf (“%d is largest”, b)
else
printf (“%d is largest”, c)
}
Printf(“the values of a,b,c are a=%d b=%d c=%d”,a,b,c)
getch()
}

input
enter the values of a,b,c = 10 20 30
output:
30 is largest
the values of a,b,c are a=10 b=20 c=30