×

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 program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

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

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 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.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.