In this post, we will learn with examples how to use if else statement in C programming.
For any task in our C programs, which we give instructions, such as printing something on the screen or taking input from the user, these are called statements.
You can also say that a C program is a collection of statements and when the program is run then all these statements are executed one by one in top to bottom order.
Now it happens that sometimes we have to control the flow of run of our program’s statements on the basis of conditions and hence we use if else conditional statement.
In other words, we use if else conditional statement to change or control the execution sequence of the program on the basis of conditions.
We also call Condition-Statements as Control Flow Statements or Decision Making Statements.
To understand if else conditional statements, first you must have knowledge of relational operators.
That’s why I would suggest that if you have not read the relational operators post, then you must read the post thoroughly by clicking on the link given below.
You can also read: Type Casting In C Programming
Let us now understand with examples how we can use if else decision making statements.
IF Else Statement in C Programming
Contents
if statement in c programming
While programming, it is very common that we want to execute some statements in our program only when a specific condition is true and for this we use if statement.
if statement syntax:
if(condition)
{
Statement1;
Statement2;
.
.
.
.
Statement N;
}
The if statement is a very simple conditional statement, to understand it, you just have to understand its syntax.
As you are seeing in the syntax of if statement that in parenthesis ( ) of if we write condition or any such boolean expression whose result should come true (non-zero) or false (0).
The curly braces { } after parenthesis ( ) of if are called body, block or compound statement of if.
Inside these curly braces we write all those statements which we want to control on the basis of if condition.
If you want to control more than one statement inside the if i.e. in the body of the if, then definitely use curly braces for the body of the if.
And if you want to control only one statement inside the if, then it is not necessary to put curly braces { } with if, you can apply if you want.
IF Else Statement in C Programming
Note
While writing statements inside curly braces, take special care of indentation.
How does an if statement work?
If the result of the if condition or Boolean expression is true, then all the statements inside the curly braces of the if will be executed (run).
If the result of the if condition or Boolean expression is false then no statements inside the curly braces of the if will execute (run).
IF Else Statement in C Programming
if statement example program:
#include <stdio.h>
int main()
{
int age;
printf("Enter Your Age : ");
scanf("%d",&age);
if(age>=18)
{
printf("True Statement of if\n");
}
printf("Universal Statement");
return 0;
}
Output 1:
Enter Your Age : 11
Universal Statement
Output 2:
Enter Your Age : 23
True Statement of if
Universal Statement
IF Else Statement in C Programming
Explanation:
First of all, you should understand that in the above program, we have controlled only True Statement of if through if conditional statements, that is, Universal Statement have nothing to do with if statements.
In the above example, we have first asked the users for their age input, which we have held in a variable named age. Now different users will input their different age like 12, 15, 18, 23, 34 etc.
If the age of the user is less than 18 then the condition written in the if will become false and only Universal Statement will be visible on the output as you can see in output 1.
If the age of the user is 18 or more then the condition written in the if will become true and he will see both True Statement of if and Universal Statement on the output as you can see in output 2.
if else statement in c programming
Now because you have understood the use of if statement, you will not have much problem in understanding how to use else with if.
As you know if means in Hindi if and else means in Hindi otherwise and we also use both of these in our conversation like we say ” if so then do this work otherwise Do this work.”
In C language also, the use of if else conditional statement is also used for some similar work. If the condition of the if is true then run the if statement otherwise run the else statement.
IF Else Statement in C Programming
if else statement syntax:
if(condition)
{
Statement1;
Statement2;
.
.
.
.
Statement N;
}
else
{
StatementA;
StatementB;
.
.
.
.
Statement X;
}
First of all, you should understand that if and else are connected to each other i.e. connected. You can use if without else as we did above but you cannot use else without if.
As you are seeing in the above syntax that we put a condition in the if and if the condition of the if becomes true then all the statements inside the if run.
If the if condition becomes false then the control of the program goes to else and all the statements inside else run.
To understand in simple words, the thing is that when if statements run then else statements will not run and when else statements run then if statements will not run.
IF Else Statement in C Programming
Note
Parenthesis ( ) and condition is never used with else because execution of else depends on if.
if else statement example program:
#include <stdio.h>
int main()
{
int age;
printf("Enter Your Age : ");
scanf("%d",&age);
if(age>=18)
{
printf("You are an adult.");
}
else
{
printf("You are not an adult.");
}
return 0;
}
Output 1:
Enter Your Age : 11
You are not an adult.
Output 2:
Enter Your Age : 23 You are an adult.
IF Else Statement in C Programming
How to put multiple conditions in an ‘if’ at the same time?
If you want to put more than one condition in the if conditional statement, then for this you have to use logical operators.
if(condition && condition2)
{
Statement1;
Statement2;
Statement3;
........
........
........
........
StatementN;
}
OR
if(condition || condition2)
{
Statement1;
Statement2;
Statement3;
........
........
........
........
StatementN;
}
IF Else Statement in C Programming
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com.