Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Practical 5: Study of Decision making statements and Write C programs using

a. if statement
b. if…else statement
c. nested if else statement (else if ladder statements)

Decision Control (Making) Statements

In decision control statements (if, if..else and nested if), group of statements are executed
when condition is true. If condition is false, then else part statements are executed.

There are 3 types of decision making control statements in C language. They are,

 if statements
 if else statements
 nested if else statement (else if ladder statements)

S.no if statement if…else statement nested if else

if (condition1)

Statement1;
}
if (condition) else
{
Statement1; {
if (condition) Statement2;
{ } if (condition2)
1.Syntax
Statements; else {
} { Statement2;
Statement3; }
Statement4; else
}
{
Statement 3;

In these type of In these type of statements, If condition 1 is false,


2.Description statements, if condition group of statements are then condition 2 is
is true, then respective executed when condition is checked and statements
block of code is true. If condition is false, then are executed if it is true.
executed. else part statements are
executed. If condition 2 also gets
failure, then else part is
executed.

Example program for if:

In “if” control statement, respective block of code is executed when condition is true

#include<stdio.h>
void main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
}

Output:

m and n are equal


Example program for C – if else:

In C if else control statement, group of statements are executed when condition is true. If
condition is false, then else part statements are executed.

#include <stdio.h>
void main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal\n");
}
else
{
printf("m and n are not equal\n");
}

Output:

m and n are not equal


Example program for C – nested if:

In “nested if” control statement, if condition 1 is false, then condition 2 is checked and
statements are executed if it is true. If condition 2 also gets failure, then else part is executed.

#include <stdio.h>
void main()
{
int m=40,n=20;
if (m>n)
{
printf("m is greater than n");
}
else
{
if(m<n)
{
printf("m is less than n");
}
else
{
printf("m is equal to n");
}
}}
Output:
m is greater than n

Write a C program to check even and odd number.

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
}
Output: Enter an integer you want to check: 25
25 is odd.
Write a C program to check vowel or constant.

#include <stdio.h>
void main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
}

Output 1:

Enter an alphabet: i
i is a vowel.

Output 2:

Enter an alphabet: G
G is a consonant.

Write a C program to find largest among three numbers.

/* C program to find largest number using if statement only */

#include <stdio.h>
void main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
else if(b>=a && b>=c)
printf("Largest number = %.2f", b);
else
printf("Largest number = %.2f", c);
}

Output:

Enter three numbers: 12.2


13.452
10.193
Largest number = 13.45
else if ladder statements:
In C if-else ladder helps user decide from among multiple options. The if statements are
executed from the top down. As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If
none of the conditions is true, then the final else statement will be executed.
C program to illustrate else-if ladder statement

#include <stdio.h>

void main()
{
int i = 25;

// Check if i is between 0 and 10


if (i >= 0 && i <= 10)
printf("i is between 0 and 10");

// Since i is not between 0 and 10


// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
printf("i is between 11 and 15");

// Since i is not between 11 and 15


// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
printf("i is between 16 and 20");

// Since i is not between 0 and 20


// It means i is greater than 20
else
printf("i is greater than 20");
}

You might also like