Home  >  Article  >  Backend Development  >  How to use 'else if ladder' conditional statement in C language?

How to use 'else if ladder' conditional statement in C language?

PHPz
PHPzforward
2023-09-10 16:05:13801browse

Else - If ladder is the most general way to write multi-way decisions.

else if syntax for ladder is as follows-

if (condition1)
   stmt1;
else if (condition2)
   stmt2;
   - - - - -
   - - - - -
   else if (condition n)
      stmtn;
   else
      stmt x;

Flowchart

Please refer to the flowchart given below-

如何在C语言中使用“else if ladder”条件语句?

Example

The following is a C program that executes the Else If Ladder conditional statement-

Live demonstration

#include<stdio.h>
void main (){
   int a,b,c,d;
   printf("Enter the values of a,b,c,d: ");
   scanf("%d%d%d%d",&a,&b,&c,&d);
   if(a>b){
      printf("%d is the largest",a);
   }
   else if(b>c){
      printf("%d is the largest",b);
   }
   else if(c>d){
      printf("%d is the largest",c);
   }
   else{
      printf("%d is the largest",d);
   }
}

Output

When the above program is executed, produces the following results -

Enter the values of a,b,c,d: 2 3 4 5
5 is the largest

The above is the detailed content of How to use 'else if ladder' conditional statement in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete