In C program the if Statement is the decision-making statement decides the execution of block of code based upon conditions or value of the given expression.
It is one of the most simple and important condtional statement in C language.
Using If statement the flow of execution of a program can be change or modify.
It decides whether a certain statement or block of statemets will be execute or not.
i.e if a given condtion is true then the statemets or block of statements is executed otherwise not.
IF, allows the code to be executed conditionaly based upon the boolen expression. if statenet makes the block of code more redable. It allows user or programmer reuse the code in different part of the program.if statements helps in simplify to trace the problem in particular block of code.It provides the flexibility to programmer to control the flow of a execution.
In Summary we can say that if statement in C programming is a fundamental tool to control flow of the program depends on conditions.
its adavntage is flexibility,reusability and redability.
if Statement in C Language
Syntax : if statement
if(condition)
{
/* if body
Statements to execute if condition is true */
}
The condition given in the above sysntax evaluates to either true or false. True is always a non-zero value(i.e. 1), and false is a value that contains zero(i.e. 0). Statements or Instructions can be a single or a multiple line of code block enclosed by curly braces { }
Given Diagram is for the syntax and flowchart of the if statement.
lets learn how syntax (if 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".
2. if the condition tested is false. then the code outside if executes i.e. "statements outside if" executes.
"statements outside if" always executes if the condition is true or false.
Example 1: C program to illustrates the use of if statement .
#include<stdio.h>
int main()
{
int x=10;
int y=2;
if(x<y) /*test-condition */
{
printf("\n %d is smaller than %d",x,y);
}
if(y<x) /*test-condition */
{
printf("\n %d is smaller than %d",y,x);
}
return 0;
}
Output:
2 is smaller than 10
The above program illustrates the use of if statements to check smaller of two numbers.
1. In the above program, we have declared and initialized two integer variables with x, y and assigned values as 10, 2 respectively.
2. Then, we have used if with a test-expression to check which number is the smallest .
We have used a relational operator < in if statement. Since the value of x is greater than y, the condition will evaluate to false and dose not give any output.
3. Then, we have used second if with a test-expression to check which number is the smallest .
We have used a relational operator < in if statement. Since the value of y is smaller than x, the condition will evaluate to true
4. Thus it print the statement inside the block of if i.e.2 is smaller than 10 After that, the control goes outside of the block and program terminates with a successful result.
Example 1: C program to test Given number is Even or odd.
/* C program to find given number is even or odd using modulus (%) Operator */
#include<stdio.h>
int main()
{
int n;
printf("\n Enter Any Number");
scanf("%d",&n);
if(n%2==0)
{
printf("\n %d is Even",n);
}
if(n%2==1)
{
printf("\n %d is Odd",n);
}
return(0);
}
Output:
Enter Any Number
5
5 is Odd
Program Explanation:
In C programming language, When we divide any two integer numbers, we get an integer result.
e.g 7/2 =3.5 but in C language when we divide 7/2 gives result 3.
we can use it to find whether given number is even or odd.
2*n is the form of Even numbers and (2*n+1) is the form of odd number.
In the given program above we have the variable 'n' declared as integer.
the entered number 5 is stored in the variable n using scanf("%d",&n);
after that if(n%2==0) becomes false (result of 5%2 is 1) then statements inside if body does not execute.
Similarly the second if(n%2==1) evaluates to true (result of 5%2==1) then statements of code inside (second) if body execute and shows the final output.
i.e. 5 is Odd.
Previous Topic:-->> Conditions in C || Next topic:-->>if else in C.