C Programming: Day 5 (Use of Else If With Logical Operator, Ternary Operator)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

C Programming

Day 5
(Use of else if with logical
operator,Ternary operator)
Use of if else with logical operator
• We use logical operators with if-else statement when we want to
check multiple conditions.
• Example

• if (condition1 && condition2) // logical AND


{
printf(“both condition satisfied”);
}
else if(condition1 || condtion2) // logical OR
{
printf(“any of the two conditions satisfied”);
}
Example
• if ((x>20) && (x<100)) printf("x is inside open
interval 20-100");

if ((x<5) || (x>20)) printf("x is not inside closed


interval 5-20");

if (!(x>20)) printf("x is smaller or equal to 20");


Ternary Operator
• The ternary operator is an operator that takes
three arguments. The first argument is a
comparison argument, the second is the result
upon a true comparison, and the third is the
result upon a false comparision.
• The ternary operator consists of three parts, the
condition, the result if true, and the result if false.
• Syntax
• $variable = ($x==1) ? true: false;
If else and ternary comparison
//if-else

if(zone=='East')
{
     shipping_charge=15.00;
 }
 else
 {
 shipping_charge=10.00;
 }
 
//ternary operator
shipping_charge = (zone=='East') ? 15.00 : 10.00;

You might also like