首頁 > 後端開發 > C++ > 主體

如何在C語言中使用「else if ladder」條件語句?

PHPz
發布: 2023-09-10 16:05:13
轉載
718 人瀏覽過

Else - 如果梯子是寫多路決策最通用的方式。

else if 梯子的語法如下-

if (condition1)
   stmt1;
else if (condition2)
   stmt2;
   - - - - -
   - - - - -
   else if (condition n)
      stmtn;
   else
      stmt x;
登入後複製

流程圖

#請參考下面給出的流程圖-

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

範例

以下是執行Else If Ladder 條件語句的C 程式-

 現場示範

#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);
   }
}
登入後複製

輸出

執行上述程式時,會產生以下結果-

Enter the values of a,b,c,d: 2 3 4 5
5 is the largest
登入後複製

以上是如何在C語言中使用「else if ladder」條件語句?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!