if else Statement in C Language
In C programming the if-else Statement is the decision-making statement.this statement decides whether the statements or block of code to be executed or not based upon conditions.
It is one of the most simple and important condtional statement in C language.
Using If else statement the flow of execution of a program can be change or modify.
Basically, the if-else statement controls the flow of a program and is hence also termed as the Control Flow statement.
It decides whether a certain statement or block of statemets will be execute or not based on given condition.
i.e if a given condtion is true then the statemets or block of statements inside if body is executed otherwise code in else block will be executed.
if-else allows the code to be executed conditionaly based upon the boolen condition. if-else statements makes the block of code more redable.
For more understanding the working of if else statement learn the following expression.
let’s take an expression pqr:
If the "pqr expression" is true:
• The statements inside the body of if control statement are executed
• statements inside the body of else statement are ignored
If the "pqr expression" is false:
• The statements inside the body of if are ignored
• statements inside the else part are executed
Syntax : if else statement
if(condition)
{
// if body
// Statements to execute if condition is true
}
else
{
// Statements to execute if condition is false
}
1. the program control first comes to the if and then test the condition .
2. If the test condition is true:
• The if block is executed.
the // if body and // statements to execute if condition is true is executed. In Real we must write some executable code here.
3. If the test condition evaluates false:
• The else block(// Statements to execute if condition is false) is executed
4. After that, the program control continues to the remaining statements below inside the program.
5. The condition given in the syntax evaluate true(i.e.1) or false(i.e. 0)
The C language instruction or Statements can be a multiple or single line codes enclosed by curly braces { }.
Given Diagram is for the syntax and flowchart of the if-else statement.
lets learn how syntax (if else statement) works from given Diagram.
We know that program execution starts from top and goes to end.
After step by step successful execution of program the control
falls into the if block.
after that
The flow jumps to Condition.
and starts testing the Condition
1. if the condition tested is true. or its result is true then
executes statements inside if body.
i.e. //body of if
/* statements to be
executed when
conditions in if is true */
after successful execution of above code the control comes out of the code and executes the code "Statements outside if-else".
2. if the condition tested is false. then the code or statements inside else executes i.e. "statements in else"
"statements outside if" always executes if the condition is true or false.
Example 1: C program to illustrates the use of if-else statement .
/* C program to check Given number is even or odd using if else statement */
#include<stdio.h>
int main()
{
int x;
printf("\n Enter any number");
scanf("%d",&x);
if(x%2==0) /*test-condition */
{
printf("\n %d is even number",x);
}
else /*else executes when condition is false */
{
printf("\n %d is odd number",x);
}
return 0;
}
Output:
Enter any number
7
7 is odd number.
Program Explanation:
The above program illustrates the use of if-else statement to check given number is odd or even.
1. In the above program, we have declared integer variables int x ; to store numeric value.
2. Then, we have used printf("\n Enter any number");
scanf("%d",&x);
Which shows the message "Enter any number" on console and control waits until we do not enter any number, but here we have entere number is 7
which is get stored in variable x in memory by scanf("%d",&x);
3. Then, we have used if with a test-expression to check whether the number is the even or odd by using if(x%2==0).
Here the test expression evaluates (7%2 gives remainder i.e. 1 hence x%2==0 is false) false .
4. Thus condition is false in if block so else block going to be execute.
Statements inside else is executed .
i.e. printf("\n %d is is odd number",x);
which gives the final output: " 7 is odd"
%d is the format specifier which is the place holder for integer value,so here value of x is placed at %d hence output "7 is odd"