Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-09-10 16:05:13
forward
799 people have browsed it

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;
Copy after login

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);
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template