If else Statement in C Programming Language

If else Statement in C Programming Language
If-else statement is used to perform the operations based on some specific condition. If block are executed if and only if the given condition is true. There are the some variants of if statement. Like if statement, if-else statement, if else-if ladder and nested if

If Statement

To check some given condition and perform some operations depending on the condition, there we need to use if statement. The syntax of if statement is:

    if(expression){  
//code to be executed
}

Flowchart of if statement in C:

If else Statement in C Programming Language

Let’s see an example of if statement. 

    #include<stdio.h>    
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}

After executing the code, our output looks like

Enter a number:4
4 is even number

Find the largest number of the three in C.

    #include <stdio.h>  
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}

After executing the code, our output looks like

Enter three numbers?
12 23 34
34 is largest

If-else Statement in C

To perform two operations for a single condition, we need to use if-else statement. Here one is for correctness of that condition, & other is for the incorrectness of the condition. If and else block cannot be executed simultaneously.

Syntax:

    if(expression){  
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

Flowchart of the if-else statement 

If else Statement in C Programming Language

Following example check whether a number is even or odd using if-else statement

    #include<stdio.h>    
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}

After executing the code, our output looks like

enter a number:4
4 is even number

enter a number:5
5 is odd number

Flowing Program check whether a person is eligible to vote or not

#include <stdio.h>  
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}

After executing the code, our output looks like

Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote

If else-if ladder Statement


It is an extension of if-else statement, which is used to perform multiple cases for different conditions.

Flowchart of else-if ladder

If else Statement in C Programming Language
#include<stdio.h>    
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

After executing the code, our output looks like

enter a number:4
number is not equal to 10, 50 or 100

enter a number:50
number is equal to 50

Following Program is calculating the grade of the student according to the specified marks

#include <stdio.h>  
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}

After executing the code, our output looks like

Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
Post a Comment