CONDITIONAL
STATEMENTS
2/6/24 Sample Footer Text 2
2/6/24 Sample Footer Text 3
Program to calculate largest of two numbers using if condition
#include <stdio.h>
main()
{
int a=10, b=20, max;
max = a;
if(b > max)
max=b;
printf(“Larger number is %d\n”,max);
}
Program to check validity of age to vote
#include <stdio.h>
main()
{
int age;
scanf(%d”,&age);
if(age >= 18)
printf(“Eligible to vote”);
else
printf(“Not eligible to vote”);
}
Multiple if condition or if-else-if ladder
• Tests for multiple if conditions
• Once condition is met, rest of the statements are skipped
• Format:
if( expression1 )
statement1;
else if( expression2 )
statement2;
…………..
else
statementn;
next_statement
Switch-case construct
• Useful when a variable or expression is tested for all the values it can assume and different
actions are taken accordingly
Format: switch (expression)
{
case expression_1: statement_1;
break;
……….
case expression_n: statement_n;
break;
default: statement;
}
Expression has to be of integral type
No two expressions in case should evaluate to same value
Example:
#include<stdio.h>
main()
{
char operator;
float op1, op2;
printf ( “ \n Please enter two numbers and operator: ");
scanf("%f %f %c",&op1,&op2, &operator);
switch (operator)
{
case '+’ : printf ( “Sum of %f and %f is %f \n", op1 + op2) ;
break;
case '-’ : printf ( “Difference of %f and %f is %f \n", op1 - op2);
break;
case '* ': printf ( “Product of %f and %f is %f \n ", op1 * op2);
break;
default: printf ( “Sorry…option not available \n”);
break;
}
Example: main()
{
char ch;
printf(“Enter a character a,b,or c \n”);
scanf(“%c”, &ch);
switch (ch)
{
case ‘a’ :
case ‘A’:
printf(“You have entered A\n”);
break;
case ‘b’ :
case ‘B’:
printf(“You have entered B\n”);
break;
case ‘c’ :
case ‘C’:
printf(“You have entered A\n”);
break;
default:
printf(“invalid input\n”);
}
Is switch-case a replacement to if-elseif-else
Comparison between if-elseif-else and switch case-
If-elseif-else Switch-case
It is cumbersome to match the ifs and It leads to a more structured program and
the elses and to manage the parenthesis is more manageable if the conditions are
many.
Can be used to test all types of Can be used only if the condition is a int
conditions or char constant. Floats or
logical/relational expressions are not
allowed.
Exercise: Write a program to read a number between
(1-5) and print its value using if- elseif-else construct.
Rewrite the above example using switch case.
1. Write a C program to accept two integers and check whether they are equal or not.
2. Write a C program to check whether a given number is even or odd.
3. Write a C program to check whether a given number is positive or negative.
4. Write a C program to find the largest of three numbers using nested if.
5. Write a C program to accept a coordinate point in an XY coordinate system and determine in which
quadrant the coordinate point lies.
6. Write a C program to read temperature in centigrade and display a suitable message according to
the temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
7. Write a C program to check whether a triangle can be formed with the given values for the angles
then check whether a triangle is Equilateral, Isosceles or Scalene.
8. Write a C program to check whether a character is an alphabet, digit or special character.
9. Write a C program to check whether an alphabet is a vowel or a consonant.