if Statement in c programming

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.