Last Updated on July 12, 2021 by Rahul
If Else Statement In C (Conditional/control statement: if, else)
If Else Statement In C (Conditional/control statement: if, else)
‘Welcome in Study Learner‘
Today i will teach you about conditional statement(if, else) in C Language. (If Else Statement In C)
Conditional/control statement: if, else
Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value true or false.
Types of if-else conditional statement in C:-
If Else Statement In C
- If statement
2. If-else statement
3. If-elseif ladder
4. Nested If-else statement
If statement:-
The if statement in C language is used to execute the code if condition is true. if statement is known as one-way selection statement.
Syntax:-
if (condition)
{
body of if statement
}
Program:-
#include<stdio.h>
int main()
{
int a;
printf("Enter any value:");
scanf("%d", &a);
if(a < 100)
{
printf("Entered value is less than 100\n");
}
printf("simple program of if statement");
return 0;
}
Output:-
Output
Enter any value:30
Entered value is less than 100
simple program of if statement
Enter any value:120
simple program of if statement
How will the program work?
The if statement evaluate the condition inside the parenthesis().
if the condition is true, statement inside the body of if are execute.
if the condition is false, statement inside the body of if are not execute.
If-else statement:-
The if-else statement in C language is used to execute the code if condition is true or false. if-else statement is known as two-way selection statement.
Syntax:-
if(condition)
{
body of if statement
}
else
{
body of else statement
}
Program:-
#include<stdio.h>
int main()
{
int a,b;
printf("Enter any number:");
scanf("%d", &a);
b = a%2;
if(b == 0)
{
printf("%d is even number", a);
}
else
{
printf("%d is odd number", a);
}
return 0;
}
Output:-
Output
Enter any number:20
20 is even number
Enter any number:25
25 is odd number
How will the program work?
The if-else statement evaluate the condition inside the parenthesis().
if the condition is true, statement inside the body of if are execute and statement inside the body of else are skip.
if the condition is false, statement inside the body of else are execute and statement inside the body of if are skip.
if-else if ladder
The if-else if statement is used to execute one code from multiple conditions. It is also known as multipath decision statement.
The if-else if ladder allows you to check between multiple test expressions and execute different statements.
syntax:-
if(condition 1)
{
body of if statement
}
else if(condition 2)
{
body of else if statement
}
else if(condition 3)
{
body of else if statement
}
.
.
else
{
body of else statement
}
Program:-
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("value of A = %d\n", a);
printf("value of B = %d\n\n", b);
if(a>b)
{
printf("A is biggest and B is smallest");
}
else if(a<b)
{
printf("A is smallest and B is biggest");
}
else
{
printf("A and B both are equal", a, b);
}
return 0;
}
Output:-
Output
Enter the value of A:20
Enter the value of B:10
value of A = 20
value of B = 10
A is biggest and B is smallest
Enter the value of A:10
Enter the value of A:10
value of A = 10
value of B = 10
A and B both are equal
Nested If-else statement
Syntax:-
if(condition)
{
if(condition)
{
body of if statement
}
else
{
body of else statement
}
}
else
{
if(condition)
{
body of if statement
}
else
{
body of else statement
}
}
program:-
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the first value:");
scanf("%d", &a);
printf("Enter the second value:");
scanf("%d", &b);
printf("Enter the third value:");
scanf("%d", &c);
if(a>b)
{
if(a>c)
{
printf("First value(%d) is the biggest value\n", a);
}
else
{
printf("Third value(%d) is the biggest value\n", c);
}
}
else
{
if(b>c)
{
printf("Second value(%d) is the biggest value\n", b);
}
else
{
printf("Third value(%d) is the biggest value\n", c);
}
}
return 0;
}
Output:-
Output
Enter the first value:10
Enter the second value:20
Enter the third value:30
Third value(30) is the biggest value
Enter the first value:10
Enter the second value:20
Enter the third value:15
second value(20) is the biggest value
Create a program to find given year leap or not leap?
Program:-
#include<stdio.h>
int main()
{
int year;
printf("Enter year:");
scanf("%d", &year);
if(year%400 == 0)
{
printf("%d is a leap year", year);
}
else if(year%100 == 0)
{
printf("%d is not leap year", year);
}
else if(year%4 == 0)
{
printf("%d is a leap year", year);
}
else
{
printf("%d is not leap year", year);
}
return 0;
}
Output:-
Output
Enter year:2020
2020 is a leap year
Enter year:2013
2013 is not leap year
Create a program check vowel or consonant using nested if-else?
Program:-
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character:");
scanf("%c", &ch);
if (( ch >= 'a' && ch <='z') || (ch >= 'A' && ch <= 'Z'))
{
if ((ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'))
{
printf("%c is a vowel", ch);
}
else
{
printf("%c is a consonant", ch);
}
}
else
{
printf("%c is neither a vowel nor a consonant", ch);
}
return 0;
}
output:-
Output
Enter a character: 1
1 is neither a vowel nor a consonant
Enter a character: a
a is a vowel
Enter a character: B
B is a consonant
If Else Statement In C
Subscribe :- Click Here
More Post
- Also Read :- Programming
Amazing