C++ if/else語句


在C++程式設計中,if語句用於測試條件。 在C++中有多種型別的if語句,它們分別如下所示 -

  • if語句
  • if-else語句
  • 巢狀if語句
  • if-else-if階梯

1. C++ if語句

C++ if語句測試條件。 如果條件為真,則執行。

if(condition){    
    //code to be executed    
}

C++ if語句的執行流程圖如下所示 -

C++ if語句範例

#include <iostream>  
using namespace std;  

int main () {  
    int num = 10;    
    if (num % 2 == 0)    
    {    
        cout<<"It is even number";    
    }   
    return 0;  
}

執行上面程式碼,得到以下結果 -

It is even number

2. C++ if-else語句

C++ if-else語句也測試條件。 如果if條件為真,則執行if塊中的程式碼,否則執行else塊中的程式碼。

if(condition){    
    //code if condition is true    
}else{    
    //code if condition is false    
}

if-else執行的流程如下圖中所示 -

C++ if-else範例

#include <iostream>  
using namespace std;  
int main () {  
   int num = 11;    
   if (num % 2 == 0)    
   {    
       cout<<"It is even number";    
   }   
   else  
   {    
       cout<<"It is odd number";    
   }  
   return 0;  
}

執行上面程式碼得到以下結果 -

It is odd number

C++ if-else範例:帶有來自使用者的輸入

#include <iostream>  
using namespace std;  
int main () {  
    int num;  
    cout<<"Enter a Number: ";  
    cin>>num;  
    if (num % 2 == 0)    
    {    
        cout<<"It is even number"<<endl;    
    }   
    else  
    {    
        cout<<"It is odd number"<<endl;    
    }  
   return 0;  
}

執行上面程式碼得到以下結果 -

[yiibai@localhost cpp]$ g++ ifelse.cpp && ./a.out
Enter a Number: 43
It is odd number
[yiibai@localhost cpp]$ g++ ifelse.cpp && ./a.out
Enter a Number: 22
It is even number
[yiibai@localhost cpp]$

3. C++ if-else-if梯形語句

C++ if-else-if梯形語句用於從多個語句中執行一個條件。

if(condition1){    
    //code to be executed if condition1 is true    
}else if(condition2){    
    //code to be executed if condition2 is true    
}    
else if(condition3){    
    //code to be executed if condition3 is true    
}    
...    
else{    
    //code to be executed if all the conditions are false    
}

if-else-if語句的執行流程如下圖所示 -

C++ if-else-if範例

#include <iostream>  
using namespace std;  
int main () {  
       int num;  
       cout<<"Enter a number to check grade:";    
       cin>>num;  
       if (num <0 || num >100)    
       {    
           cout<<"wrong number\n";    
       }    
       else if(num >= 0 && num < 50){    
           cout<<"Fail\n";    
       }    
       else if (num >= 50 && num < 60)    
       {    
           cout<<"D Grade\n";    
       }    
       else if (num >= 60 && num < 70)    
       {    
           cout<<"C Grade\n";    
       }    
       else if (num >= 70 && num < 80)    
       {    
           cout<<"B Grade\n";    
       }    
       else if (num >= 80 && num < 90)    
       {    
           cout<<"A Grade\n";    
       }    
       else if (num >= 90 && num <= 100)    
       {    
           cout<<"A+ Grade\n";  
       }
    return 0;
}

執行上面範例程式碼,得到以下結果 -

[yiibai@localhost cpp]$ g++ if-else-if.cpp && ./a.out
Enter a number to check grade:99
A+ Grade
[yiibai@localhost cpp]$ g++ if-else-if.cpp && ./a.out
Enter a number to check grade:59
D Grade
[yiibai@localhost cpp]$ g++ if-else-if.cpp && ./a.out
Enter a number to check grade:49
Fail
[yiibai@localhost cpp]$