IF Statment In C/C++ Language

IF -Else Statement IN C/C++

Before we use IF -Else statement we must Understand the IF Statement and IF-Else Statement:
In c Language In IF statement is decision control statement . As we know that in C all the statement execute one by one in that order in which they are written. Some time we transfer the control from one statement to other statement on our own choice the we use IF statement.
In this statement we give a condition if condition is true the statement or set of statements in the body of IF structure are execute other wise control is transfer to next statement after the IF structure.
Syntax of IF Statement
if (condition)

{
Statements;
}
IN IF-Else structure when the condition which is given in IF statement is True the set of statements in the body of IF structure are executed other wise statements in the body of Else Structure are executed.
Syntax of IF-Else Statement if (condition)

{
Statements;
}
else
{
Statements;
}

Example 1:


#include<stdio.h>
#include<conio.h>     
int main()
{
int x;

printf("Enter a Number \n");

scanf("%d",&x);

if (x>0)

{

printf("\n  Your Number is +ve");

}

return 0;
}

In above example user enter a number which is store in x and the IF statement check this number if number is grater then 0 then display a message on screen that this number is +ve other wise nothing will be display. But if we use Else statement with IF statement then we have two opportunity that if number is grater then 0 then display the message "Your Number is +ve" other wise display the message "Your Number is "-ve"


#include<stdio.h>
#include<conio.h>     
int main()
{
int x;

printf("Enter a Number \n");

scanf("%d",&x);

if (x>0)

{

printf("\n  Your Number is +ve");

}

else

{

printf("\n  Your Number is -ve");

}

 

return 0;
}


Note :- if the body of IF and Else structure have single statement then braces or "curly brackets" { } or not required.
Lets take an other view if number is grater then zero he display message that number is +ve , if number is less then zero then display the message number is -ve and if user enter zero the display the message number is zero .
For this purpose we use Nested IF . (When one IF statement is placed in the body of other IF statement then it is called Nested IF)


#include<stdio.h>
#include<conio.h>     
int main()
{
int x;

printf("Enter a Number \n");

scanf("%d",&x);

if (x>0)

{

printf("\n  Your Number is +ve");

}

else if(x<0)

{

printf("\n  Your Number is -ve");

}

else

{

printf("\n  Your Number is zero");

}

return 0;
}

For this purpose we use Nested IF . (When one IF statement is placed in the body of other IF statement then it is called Nested IF)