if And if else statement In C [ Full Information With Examples ]
Today in this article we will learn about the if statement along with if-else statement, nested if-else statement, and else if ladder statement with examples.
Let’s first understand about if statement then we will learn if else statement in C.
If Statement in C
When we want to execute a statement inside a block only when the given condition is true, then we use the if statement.
if Statement is a very simple Decision Making Control Statement which decides on the basis of the given condition whether the statement inside the if block will run or not.
If the condition is true then the statement inside the if block is run and if the condition is false then the statement inside the if block is not run.
In C language, when we write one or more statements by covering them with a curly bracket {}, then it is called a block.
The condition in parenthesis () of if is checked as true or false. If the condition is true then it is considered to be a non-zero value and if the condition is false then it is considered zero.
Syntax -:
if (condition)
{
// Statements to execute if
// condition is true
}
Example
If (5>2) // Here () is called parenthesis in which 5> 2 is an expression or condition.
{
printf ("five is greater than two");
}
Output -:
five is greater than two
1. In the condition of the parenthesis () of if, we can put any variable or any expression in that condition.
Example1
int x = 5;
If (x) // Here in parenthesis "()" if x is in the form of variable conditioner.
{
Statements;
}
Example2
If (5>2) // Here the parenthesis of if is in 5 (2) expression conditioner in "()".
{
Statements;
}
2. If we write only one statement after the if, then the condition is checked only for that statement whether that one statement has to be run or not.
3. If there is only one statement in the block of if, then we do not need to put a curly bracket “{}”, the compiler understands. That the statement immediately after the if is only the statement of the if block for which this condition is being checked.
Example
#include <stdio.h>
int main () {
int x = 6;
if (x>2)
printf ("Value of x is: %d" x);
return 0;
}
Output -:
Value of x is: 6
4. If there is more than one statement in the block of if, then we need to put the curly bracket “{}”.
if (Condition)
{
Statement;
Statement;
Statement;
}
Let’s now learn about what is the if-else statement, and what is if-else statement in C language?
If else Statement In C
The if-else statement is an extended form of the If statement. In this, we execute the statement of the block containing else, if the if condition is false.
The if-else statement is used when we have to perform two operations on the same condition.
Meaning that when we want to run the statement of another block when a condition is true and the statement of another block should run when the condition is false.
Syntax of If else Statement In C -:
if (Condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}
Let us understand this by creating a program.
Example -:
Here we will create a program in which we will enter the user’s age and based on the input entered by the user, we will check whether the user is eligible for voting?
If the user’s age is more than 18, then we will run a statement inside the if block and if the user is less than 18, then we will run a statement inside the else block.
// c program to check user are eligible or not for voting
#include <stdio.h>
int main ()
{
int age;
printf ("Enter Your Age \n");
scanf ("%d", &age);
if (age>=18)
{
/* The codes in this block will only execute when
The above condition (age> = 18) is true.
*/
printf ("You are eligible for vote");
}
else
{
/* The codes in this block will only execute when
The above condition (age> = 18) will be False.
*/
printf("You are not eligible for vote");
}
return 0;
}
In this example, we take input from the user and check whether he is more than 18 years old.
Based on the input given by the user, its output will be something like this.
Output -:
Enter Your Age
21
You are eligible for vote
We can use the if-else statement in many ways such that we can use the if-else statement within the if statement itself, and the if-else statement inside the else block.
When we use the if-else statement and if statement inside the if-else statement then it is called nested if-else statement and When we use the if-else statement inside the else block, then it is called else if ladder.
Let us now know one by one about these remaining uses of the if else statement in C.
Nested if-else statement in C
A nested if-else statement is also similar to an if-else statement, but the slight difference is that in a nested if-else statement, we use many more if-else statements within the if-else statement.
When we use an if-else statement or if statement inside an if block or inside an else block then, it is called a Nested if-else statement.
The nested if-else statement is used when we have a lot of conditions, which we have to execute a statement only after checking.
Let us understand these things by making a program.
Example -:
Below we are going to create a program in which we will take three numbers of inputs from the user and based on the input given by that user, we will check which of those numbers is bigger. We will finally print the value of whatever number will be bigger.
#include <stdio.h>
int main ()
{
int x, y, z;
printf ("Enter three number \n");
scanf ("%d %d %d", &x, &y, &z);
if (x>y) // first condition
{// The code of this block will run when the first condition is true
if (x>z) // second condition
printf ("Greater value is: %d", x);
else
printf ("Greater value is: %d", z);
}
else
{ // If the first condition is False, then the code of this block will run.
if (y>z)
printf ("Greater value is: %d", y);
else
printf ("Greater value is: %d", z);
}
return 0;
}
You can run the live demo of this program by clicking on the Run Code Live button above.
In this program, we input three values from the user via scanf (). Then we first checked if x>y in the parenthesis of if the value of x is greater than y?
There can be two points here, either the value of x will be greater than y or smaller.
If the value of x is larger, then we will enter the inside of the if block and run the statement inside it, and if the value of x is smaller, then we will enter the inside else block and run the statement inside it.
Both these things depend on the input given by the user, so let’s see what the output of this program is.
Output -:
Enter three number
12
6
10
Greater value is: 12
Now let’s know about the else if ladder statement And how it is used.
else if ladder statement in C
When we use an if statement or if-else statement inside an else block then, it is called else if ladder statement.
else if ladder statement is used when we have many conditions in else statement which we have to execute a statement only after checking.
Syntax -:
if (condition1)
{
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true
}
else if (condition3) {
// code to be executed if condition3 is true
}
.
.
.
else {
// code to be executed if all the conditions are false
}
Let us understand the else if ladder statement by creating a program
Example -:
Below we are going to create a program in which we will input two values from the user, then compare the input given by the user to each other.
Are the two values equal?
Which of the two is smaller and which is bigger.
#include <stdio.h>
int main ()
{
int num1, num2;
printf ("Input the value of var1:");
scanf ("%d", &num1);
printf ("Input the value of var2:");
scanf ("%d", &num2);
if (num1 == num2)
{
printf ("num1 is equal to num2 \n");
}
else if (num1>num2)
{
printf ("num1 is greater than num2 \n");
}
else
{
printf ("num2 is greater than num1 \n");
}
return 0;
}
Output -:
Input the value of num1: 10
Input the value of num2: 20
num2 is greater than num1
Read More -:
- Control Statements in C
- Switch Statement in C
- Loop in C
- For Loop in C
- While and Do while Loop in C
- Goto Statement in C
- Break Statement in C
- Continue statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion -:
Friends, I hope that after reading this post today, you have learned what is if statement in C language. Along with all the information related to the if-else statement In C, nested if else statement In C, and else if ladder statement In C.
Friends, I hope you have found the answer of your question and you will not have to search about the what is if statement in C language, nested if else statement In C, and else if ladder statement In C.
However, if you want any information related to this post or related to programming language, and computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the what is if statement in C language, nested if else statement In C, and else if ladder statement In C.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.