×

If else Programs in C Programming

If else Programs in C programming

The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in if block is executed.

If Statement

Depending on the validity of that condition, the if statement is used to test the condition and perform the operations. It is mostly used in the scenario for which there exist different conditions; we need to perform the different operations. The if statement syntax is provided below.

if(expression){  
 //code to be executed  
      }  
 Let's see a simple example of C language if statement.
 #include <stdio.h>
 int main()
 { 
     int x = 50;
     int y = 52;
     if (x<y)
     {
         printf("Variable x is less than y");
     }
     return 0; 
 } 

Output:

Variable x is less than y

Program to use multiple if statement to check more than one conditions.

#include <stdio.h>
 int main()
 {
     int x, y;
     printf("Enter the value of x:");
     scanf("%d", &x);
     printf("Enter the value of y:"); 
     scanf("%d", &y);
     if (x>y)
     {
           printf("x is greater than y\n");
     }
     if (x<y)  
     {
           printf("x is less than y\n");
     }
     if (x==y)
     {
           printf("x is equal to y\n"); 
     }
     printf("End of Program");
     return 0;
 } 

Output:

Enter the value of x: 12
 Enter the value of y: 12
 x is equal to y
 End of Program 

If-else Statement

The if-else statement is used to achieve two single condition operations. The if-else statement is an extension of the if statement that allows one to perform two different operations, one for the correct condition, and the other for incorrect condition. Here we must remember that if block can't be executed simultaneously, then otherwise.

 if(expression){  
 //if condition is true  
 }else{  
 //if condition is false  
 }   

Example of simple if-else statement

#include<stdio.h>
        void main()
        {
          int num;
          printf("Enter the number: ");
          scanf("%d",&num);
          if (num>0) 
          printf("%d is Positive.");
              else
              printf("%d is Negative.");
            } 

Output:

Enter any number: 24
24 is Positive 

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

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

Output:

Enter your Original age: 20
You are legal to vote...
Enter your Original age: 14
Sorry ..you can't vote Now. 

If else-if ladder Statement

The ladder expression if-else-if is an extension of the state if-else. It is used in a scenario where there are multiple cases for different conditions to be performed. If a condition is true, then the statements specified in the if block will be executed in the if-else-if ladder statement, and if any other condition is true, then the statements specified in the other block will be executed. When the last condition is not valid, the statements specified in the other block are executed. Many other blocks-if possible. It is similar to the switch case statement, where if none of the cases match, the default is executed instead of another row.

if(condition1){  
 //code to be run if condition1 is true  
 }else if(condition2){  
 //code to be run if condition2 is true  
 }  
 else if(condition3){  
 //code to be run if condition3 is true   
 }  
 ...  
 else{  
 //code to be run if all the conditions are false  
 }  

The example of an if-else-if statement in C language is given below.

#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;  
 }     

Output:

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

Program to calculate 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("You got grade A");   
     }  
     else if (marks > 60 && marks <= 85)   
     {  
         printf("You got grade B +");  
     }   
     else if (marks > 40 && marks <= 60)   
     {  
         printf("You got grade B");  
     }  
     else if (marks > 30 && marks <= 40)   
     {  
         printf("You got grade C");    
     }  
     else   
     {  
         printf("Fail");   
     }  
 }   

Output:

Enter your marks:10
 Fail
 Enter your marks:40
 You got grade C 
 Enter your marks:90
 you got grade A 

One more example

#include <stdio.h>
 int main () {
    /* local variable definition */
    int a = 100;
    /* check the boolean condition */
    if( a == 10 ) { 
       /* if condition is true then print the following */
       printf("The value of a is 10\n" );
    } else if( a == 20 ) {
       /* if else if condition is true */
       printf("The value of a is 20\n" );
    } else if( a == 30 ) {
       /* if else if condition is true  */ 
       printf("The value of a is 30\n" );
    } else {
       /* if none of the conditions is true */
       printf("There is No value Matching\n" );
    }
    printf("The total value of a is: %d\n", a ); 
    return 0;
 } 

Output:

There is NO value Matching
 The total value of a is: 100 

Related Topics

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.