Professional Documents
Culture Documents
PPS Practical 5
PPS Practical 5
a. if statement
b. if…else statement
c. nested if else statement (else if ladder 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)
if (condition1)
Statement1;
}
if (condition) else
{
Statement1; {
if (condition) Statement2;
{ } if (condition2)
1.Syntax
Statements; else {
} { Statement2;
Statement3; }
Statement4; else
}
{
Statement 3;
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:
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:
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
#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.
#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:
#include <stdio.h>
void main()
{
int i = 25;