字幕列表 影片播放 列印所有字幕 列印翻譯字幕 列印英文字幕 The numerical operators in C++ can be grouped into five types: arithmetic, assignment, comparison, 在C++裏,和數值相關的運算子可區分為5大類:算術、指定、比較 logical, and bitwise operators. 邏輯、和位元運算子 First, we have the four basic arithmetic operations, as well as modulus to get the division remainder. 算術運算子除了基本的加減乘除之外,還有同餘運算 Notice that the division sign gives an incorrect result here. This is because it operates on 請注意這裏,除的結果是錯的。這是因為 two integer values and will therefore trunctate the result and return an integer. To get the 兩個整數相除所得的商的小數部份會被無條件捨去。為了得到 correct value we need to explicitly convert one of the numbers to a floating-point number. 正確的商數,我們必須將被除數或除數轉型為浮點數 A common use of the assignment and arithmetic operators is to operate on a variable and 指定運算子和算術運算子常用來運算變數 then to save the result back into that same variable. These operations can be shortened 再將運算的結果存回原來的變數。這些運算可以簡化成 with the combined assignement operators. 複合指定運算子 Another common operation is to increment or decrement a variable by one. 對變數遞增1或遞減1是另一種常見的運算 This can be simplified with the increment (++) and decrement (--) operators. 這可以簡寫成遞增(++)、和遞減(--)運算子 Both of these can either be used before or after a variable. 這兩種運算子可用在變數之前或之後 The result on the variable is the same whichever we use. The difference is that the post-operator 對於該變數而言,最後的結果是一樣的,不同之處在於後置運算子 returns the original value before it changes the variable while the pre-operator changes 在改變變數內容之前會傳回此變數的值,而前置運算子會在改變 the variable first and then returns the value. 變數的內容之後才傳回變數的值 Next, there's the comparison operators that compares two values and returns either true or false. 比較運算子用來比較兩數值的大小 Used together with these we have the logical operators. Logical and (&&) evaluates to true if both the left and right sides are true, 可以和邏輯運算子一起使用,如果邏輯且運算(&&)的左右兩邊皆為真,則其結果為真 and logical or (||) is true if either the left or right side is true. 如果邏輯或運算(||)左右之一為真,則其結果為真 To invert a boolean result we also have logical not (!). Note 邏輯非運算(!)將布林值反相 that for both "logical and" and "logical or" the righthand side won't be evaluated if the 值得注意的是:在「邏輯且」和「邏輯或」右方的運算式不會被計算 result is already determined by the left side. 如果左方的運算式已經推導出結果 Lastly, we have the bitwise operators which allow us to manipulate individual bits inside 最後要介紹的是位元運算子。位元運算子可以用來處理整數的個別位元 of an integer. For example, the or operator (|) makes the 例如,或運算子(|)把 resulting bit 1 if the bits are set on either side of the operator. These bitwise operators 目的位元設成1,如果左右兩方相對應位置的位元之一為1的話 also have shorthand assigment operators. 位元運運子也能簡化為複合指定運算子 Now, let's look at operator precedence. In C++ expressions are normally evaluated from 接下來看看運算子的優先順序。在C++中,運算式一般由左至右運算 left to right. However, as can be seen in this table different operators also have different 如同這張表格呈現的,不同的運算子有不同的運算優先權 precedents that determine which one gets evaluated first. This same order also applies to many 這些運算優先權決定運算式的那些部份應該優先計算。優先權的概念也適用於許多 other languages such as Java and C# also. As an example, the logical and operator binds 其他的程式語言,比如說Java及C#。舉個例來說,邏輯且運算子 weaker than the relational operators, which in turn binds weaker than the arithmetic operators. 比關係運算子的優先權低;而關係運算子比算術運算子的優先權低 To make things clearer we should instead use parenthesis to decide what part of the expression 為了讓事情更清晰,我們應該使用括號來決定運算式的那個部份該優先執行 will be evaluated first since parenthesis has the highest precendence of all operators. 因為括號比其他的運算子擁有更高的優先權
B1 中級 中文 英國腔 運算 變數 邏輯 優先 整數 程式 C++教程 - 06 - 運算符 (C++ Tutorial - 06 - Operators) 383 42 Jjli Li 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字