<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C++ 運算符 > 原文: [https://www.programiz.com/cpp-programming/operators](https://www.programiz.com/cpp-programming/operators) #### 在本教程中,我們將借助示例學習 C++ 中不同類型的運算符。 在編程中,運算符是對值或變量進行運算的符號。 運算符是對變量和值執行運算的符號。 例如,`+`是用于加法的運算符,而`-`是用于減法的運算符。 C++ 中的運算符可分為 6 種類型: 1. 算術運算符 2. 賦值運算符 3. 關系運算符 4. 邏輯運算符 5. 按位運算符 6. 其他運算符 * * * ## 1\. C++ 算術運算符 算術運算符用于對變量和數據執行算術運算。 例如, ```cpp a + b; ``` 此處,`+`運算符用于相加兩個變量`a`和`b`。 同樣,C++ 中還有其他各種算術運算符。 | 運算符 | 工作方式 | | --- | --- | | `+` | 加法 | | `-` | 減法 | | `*` | 乘法 | | `/` | 除法 | | `%` | 模運算(除法后的余數) | ### 示例 1:算術運算符 ```cpp #include <iostream> using namespace std; int main() { int a, b; a = 7; b = 2; // printing the sum of a and b cout << "a + b = " << (a + b) << endl; // printing the difference of a and b cout << "a - b = " << (a - b) << endl; // printing the product of a and b cout << "a * b = " << (a * b) << endl; // printing the division of a by b cout << "a / b = " << (a / b) << endl; // printing the modulo of a by b cout << "a % b = " << (a % b) << endl; return 0; } ``` **輸出** ```cpp a + b = 9 a - b = 5 a * b = 14 a / b = 3 a % b = 1 ``` 在這里,運算符`+`,`-`和`*`分別計算了我們可能期望的加,減和乘。 **除法運算符** 注意我們程序中的操作`(a / b)`。`/`運算符是除法運算符。 從上面的示例可以看出,如果一個整數除以另一個整數,我們將得到商。 但是,如果除數或被除數是浮點數,我們將以小數形式得到結果。 ```cpp In C++, 7/2 is 3 7.0 / 2 is 3.5 7 / 2.0 is 3.5 7.0 / 2.0 is 3.5 ``` **模運算符** 模運算符`%`計算余數。 當`a = 9`除以`b = 4`時,余數為 **1**。 **注意**:`%`運算符只能與整數一起使用。 * * * ### 增減運算符 C++ 還提供了遞增和遞減運算符:分別為`++`和`--`。`++`將操作數的值增加 **1**,而`--`將其減少值 **1**。 例如, ```cpp int num = 5; // increasing num by 1 ++num; ``` 在此,`num`的值從其初始值 **5** 增加到 **6**。 ### 示例 2:遞增和遞減運算符 ```cpp // Working of increment and decrement operators #include <iostream> using namespace std; int main() { int a = 10, b = 100, result_a, result_b; // incrementing a by 1 and storing the result in result_a result_a = ++a; cout << "result_a = " << result_a << endl; // decrementing b by 1 and storing the result in result_b result_b = --b; cout << "result_b = " << result_b << endl; return 0; } ``` **輸出** ```cpp result_a = 11 result_b = 99 ``` 在上面的程序中,我們將`++`和`--`運算符用作**前綴**。 我們還可以將這些運算符用作**后綴**。 這些運算符用作前綴與用作后綴時略有不同。 要了解有關這些運算符的更多信息,請訪問[遞增和遞減運算符](https://www.programiz.com/article/increment-decrement-operator-difference-prefix-postfix)。 * * * ## 2\. C++ 賦值運算符 在 C++ 中,賦值運算符用于將值賦給變量。 例如, ```cpp // assign 5 to a a = 5; ``` 在這里,我們為變量`a`分配了`5`的值。 | 運算符 | 示例 | 相當于 | | --- | --- | --- | | `=` | `a = b;` | `a = b;` | | `+=` | `a += b;` | `a = a + b;` | | `-=` | `a -= b;` | `a = a - b;` | | `*=` | `a *= b;` | `a = a * b;` | | `/=` | `a /= b;` | `a = a / b;` | | `%=` | `a %= b;` | `a = a % b;` | ### 示例 2:賦值運算符 ```cpp #include <iostream> using namespace std; int main() { int a, b, temp; // 2 is assigned to a a = 2; // 7 is assigned to b b = 7; // value of a is assigned to temp temp = a; // temp will be 2 cout << "temp = " << temp << endl; // assigning the sum of a and b to a a += b; // a = a +b cout << "a = " << a << endl; return 0; } ``` **輸出** ```cpp temp = 2 a = 9 ``` * * * ## 3\. C++ 關系運算符 關系運算符用于檢查兩個操作數之間的關系。 例如, ```cpp // checks if a is greater than b a > b; ``` 在此,`>`是關系運算符。 它檢查`a`是否大于`b`。 如果關系為**為真**,則返回 **1**,而如果關系為**為假**,則返回 **0**。 | 運算符 | 含義 | 示例 | | --- | --- | --- | | `==` | 等于 | `3 == 5`求值為`false` | | `!=` | 不等于 | `3 != 5`求值為`true` | | `>` | 大于 | `3 > 5`求值為`false` | | `<` | 小于 | `3 < 5`求值為`true` | | `>=` | 大于或等于 | `3 >= 5`求值為`false` | | `<=` | 小于或等于 | `3 <= 5`求值為`true` | ### 示例 4:關系運算符 ```cpp #include <iostream> using namespace std; int main() { int a, b; a = 3; b = 5; bool result; result = (a == b); // false cout << "3 == 5 is " << result << endl; result = (a != b); // true cout << "3 != 5 is " << result << endl; result = a > b; // false cout << "3 > 5 is " << result << endl; result = a < b; // true cout << "3 < 5 is " << result << endl; result = a >= b; // false cout << "3 >= 5 is " << result << endl; result = a <= b; // true cout << "3 <= 5 is " << result << endl; return 0; } ``` **輸出** ```cpp 3 == 5 is 0 3 != 5 is 1 3 > 5 is 0 3 < 5 is 1 3 >= 5 is 0 3 <= 5 is 1 ``` **注意**:關系運算符用于決策和循環。 * * * ## 4\. C++ 邏輯運算符 邏輯運算符用于檢查表達式是`true`還是`false`。 如果表達式為`true`,則返回 **1**,而如果表達式為`false`,則返回 **0**。 | 運算符 | 示例 | 含義 | | --- | --- | --- | | `&&` | `expression1 && expression2` | 邏輯與。僅當所有操作數均為`true`時為`true`。 | | <code>&#124;&#124;</code> | <code>expression1 &#124;&#124; expression2</code> | 邏輯或。如果至少一個操作數為`true`,則為`true`。 | | `!` | `!expr` | 邏輯非。僅當操作數為`false`時為`true`。 | 在 C++ 中,邏輯運算符通常用于決策制定。 為了進一步了解邏輯運算符,讓我們來看以下示例, ```cpp Suppose, a = 5 b = 8 Then, (a > 3) && (b > 5) evaluates to true (a > 3) && (b < 5) evaluates to false (a > 3) || (b > 5) evaluates to true (a > 3) || (b < 5) evaluates to true (a < 3) || (b < 5) evaluates to false !(a == 3) evaluates to true !(a > 3) evaluates to false ``` ### 示例 5:邏輯運算符 ```cpp #include <iostream> using namespace std; int main() { bool result; result = (3 != 5) && (3 < 5); // true cout << "(3 != 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 < 5); // false cout << "(3 == 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 > 5); // false cout << "(3 == 5) && (3 > 5) is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) is " << result << endl; result = (3 != 5) || (3 > 5); // true cout << "(3 != 5) || (3 > 5) is " << result << endl; result = (3 == 5) || (3 > 5); // false cout << "(3 == 5) || (3 > 5) is " << result << endl; result = !(5 == 2); // true cout << "!(5 == 2) is " << result << endl; result = !(5 == 5); // false cout << "!(5 == 5) is " << result << endl; return 0; } ``` **輸出** ```cpp (3 != 5) && (3 < 5) is 1 (3 == 5) && (3 < 5) is 0 (3 == 5) && (3 > 5) is 0 (3 != 5) || (3 < 5) is 1 (3 != 5) || (3 > 5) is 1 (3 == 5) || (3 < 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0 ``` **邏輯運算符**的說明 * `(3 != 5) && (3 < 5)`的值為 **1**,因為兩個操作數`(3 != 5)`和`(3 < 5)`均為 **1**(`true`)。 * `(3 == 5) && (3 < 5)`的值為 **0**,因為操作數`(3 == 5)`為 **0** (`false`)。 * `(3 == 5) && (3 > 5)`的值為 **0**,因為兩個操作數`(3 == 5)`和`(3 > 5)`均為 **0**(`false`)。 * `(3 != 5) || (3 < 5)`的值為 **1**,因為兩個操作數`(3 != 5)`和`(3 < 5)`均為 **1**(`true`)。 * `(3 != 5) || (3 > 5)`的值為 **1**,因為操作數`(3 != 5)`為 **1**(`true`)。 * `(3 == 5) || (3 > 5)`的值為 **0**,因為兩個操作數`(3 == 5)`和`(3 > 5)`均為 **0**(`false`)。 * `!(5 == 2)`的值為 **1**,因為操作數`(5 == 2)`為 **0**(`false`)。 * `!(5 == 5)`的值為 **0**,因為操作數`(5 == 5)`為 **1**(`true`)。 * * * ## 5\. C++ 按位運算符 在 C++ 中,按位運算符用于對單個位執行操作。 它們只能與`char`和`int`數據類型一起使用。 | 運算符 | 描述 | | --- | --- | | `&` | 二進制與 | | `&#124;` | 二進制或 | | `^` | 二進制異或 | | `~` | 二進制補碼 | | `<<` | 二進制左移 | | `>>` | 二進制右移 | 要了解更多信息,請訪問 [C++ 按位運算符](https://www.programiz.com/cpp-programming/bitwise-operators)。 * * * 除了上面討論的運算符外,還有一些其他運算符,例如`sizeof`,`?`,`.`,`&`等,不能整齊地分為一種或另一種類型。 我們將在后面的教程中了解有關這些運算符的更多信息。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看