
- C++基礎
- C++首頁
- C++概述
- C++環境搭建
- C++基本語法
- C++註釋
- C++ Hello World
- C++省略名稱空間
- C++常量/字面量
- C++關鍵字
- C++識別符號
- C++資料型別
- C++數值資料型別
- C++字元資料型別
- C++布林資料型別
- C++變數型別
- C++變數作用域
- C++多個變數
- C++基本輸入/輸出
- C++修飾符型別
- C++儲存類
- C++運算子
- C++數字
- C++列舉
- C++引用
- C++日期和時間
- C++控制語句
- C++決策制定
- C++ if語句
- C++ if else語句
- C++巢狀if語句
- C++ switch語句
- C++巢狀switch語句
- C++迴圈型別
- C++ while迴圈
- C++ for迴圈
- C++ do while迴圈
- C++ Foreach迴圈
- C++巢狀迴圈
- C++ break語句
- C++ continue語句
- C++ goto語句
- C++建構函式
- C++建構函式和解構函式
- C++複製建構函式
C++巢狀if語句
總是可以合法地巢狀if-else語句,這意味著可以在另一個if或else if語句中使用一個if或else if語句。
語法
巢狀if語句的語法如下所示:
if( boolean_expression 1) { // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true } }
您可以像巢狀if語句一樣,以類似的方式巢狀else if...else。
示例
#include <iostream> using namespace std; int main () { // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) { // if condition is true then check the following if( b == 200 ) { // if condition is true then print the following cout << "Value of a is 100 and b is 200" << endl; } } cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; }
當以上程式碼編譯並執行時,會產生以下結果:
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
廣告