×

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

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

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.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Types of Pointers in C

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

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute 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.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

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.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.