5.11 – Conditional operators in C language

by subbu on September 22, 2013

Conditional operators in C language: (Ternary operators)

?, : are two operators used to perform conditional execution. These are also called ternary operators because these work on three operands.
We can use these operators in place of if else selection statement.
Conditional operators support two syntaxes

  1. Statement Syntax
  2. Expression Syntax

Statement syntax – Conditional operators:

Conditional operators in C language

According to the above syntax if the condition is true then the first statement is executed, otherwise the second statement is executed.

Specification:
Accept any integer and print whether the number is an even or odd.

Using simple if selection statement:

if(n%2==0)
   printf("Even number");
else
   printf("Odd number");
/* using conditional operators */
#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
(n%2==0)?printf("Even number"):printf("Odd number");
return 0;
}

Execution 1:
Enter any integer:25
Odd number

Execution 2:
Enter any integer:12
Even number

Example explained:
When 25 is input to the program then the condition n%2==0 gives false and the second statement printf(“Odd number”); is executed.
When 12 is input to the program then the condition n%2==0 gives true and the first statement printf(“Even number”); is executed.

Specification:
Accept any number and print whether the number is a zero, positive or negative.

Using nested if else selection statement:

if(n==0)
  printf("Zero);
else
{
   if(n>0)
      printf("Positive");
   else
      printf("Negative");
}
/* using conditional operators*/
#include<stdio.h>
int main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
(n==0)?printf("Zero"):((n>0)?printf("Positive"):printf("Negative"));
return 0;
}

Execution 1:
Enter any number: 25
Positive

Execution 2:
Enter any number: -14
Negative

Execution 3:
Enter any number: 0
Zero

Expression syntax – Conditional Operators:

Conditional operators in C language

According to the above syntax if the condition is true then the value of expression 1 is assigned to the variable otherwise the value of expression 2 is assigned to the variable.

Specification:
Accept the price, quantity of a product and print the bill, discount and the net bill. Allow 5% discount if bill exceeds to 500 otherwise allow 3% discount.

Using simple if else selection statement:

if(bill>500)
  dis=bill*5/100;
else
  dis=bill*3/100;
/* using conditional operators */
#include<stdio.h>
int main()
{
float price,bill,dis,net_bill;
int qty;
printf("Price:");
scanf("%f",&price);
printf("Quantity:");
scanf("%d",&qty);
bill=price*qty;
dis=(bill>500)?bill*5/100:bill*3/100;
net_bill=bill-dis;
printf("Total bill %.2f",bill);
printf("\nDiscount %.2f",dis);
printf("\nNet bill %.2f",net_bill);
return 0;
}

Execution:
Price: 100
Quantity: 10
Total bill 1000.00
Discount 50.00
Net bill 950.00

Specification:
Accept two integers and print the biggest number.

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two integers:\n");
scanf("%d%d",&x,&y);
if(x==y)
  printf("Equals");
else
  printf("Biggest number is %d",((x>y)?x:y));
return 0;
}

Execution:
Enter two integers:
45      78
Biggest number is 78

Example explained:
Here the result of expression is directly printed rather assigning to variable (l-value)

Specification:
Accept any character as input and print whether the character is a special symbol or not.

#include<stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c",&ch);
!(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>='0'&&ch<='9'||ch==' ')?printf("Special symbol"):printf("Not a special symbol");
return 0;
}

Execution 1:
Enter any character : D
Not a special symbol

Execution 2:
Enter any character:+
Special symbol

Previous post:

Next post: