布林運算子一覽、if-else 條件判斷
if-else
語法
補充:
你可以單獨寫一個if判斷式就好,else不需要寫。
然而,當你寫了else則一定要幫他搭配一個if,簡單來說就是else不能單獨出現。
好了,想必看完上面大家都能明白程式怎麼寫了吧!我深信你們聰慧,但以防萬一,我還是要跟大家提一些if-else的運作機制。
if-else裡的條件怎麼判斷?
條件判斷
comparison Operators
C++ Notation |
中文 |
== |
等於 |
!= |
不等於 |
< |
小於 |
<= |
小於等於 |
> |
大於 |
>= |
大於等於 |
電腦會去看if裡面的條件是否成立,如果成立的話,會回傳true,不成立的時候則會回傳false。
orange學長剛剛跟我說妳們一定還是看不懂
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
,所以我決定直接舉例給大家看吧!題目: 有兩整數變數big跟small,這兩個變數分別存放兩值3跟1,接下來我們拿這兩個變數來使用if-else
電腦在判斷條件 big<small 結束後,會回傳false,
至於為什麼回傳false?
那是因為3比1還要大,因此big<small的條件不成立。
綜合上述那些
由 big<small 得到false
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在電腦把條件判斷完後,會因為false而去執行else裡的程式碼,或是什麼都不做。
進階思維 - 條件判斷內發生指派會發生甚麼事? (important!)
大家應該還記得之前說「=」在程式語言是指派的意思,而等於只會在條件判斷的時候出現,而在 C++ 裡,等於要用兩個等號,like this「==」。
對就是那個討人厭的表情符號
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
應該非常淺顯易懂,而你說為什麼,沒有為什麼,這是規定,所以把他記起來比較好。
當然一定有人學不乖,會在條件判斷裡只放一個等號。
當然跳脫程式語言的框架,直觀的話他會是等於,但要記得在 C++ 裡「=」就是指派,所以在第三行的這個判斷裡面,我反而是把 b 的值 assign 給 a,而這件事在程式裡永遠都是對的,因為沒有所謂的指派錯這種事,指派就是指派,所以這個條件永遠會成立,因此會印出they're equal
,但這很明顯就是錯的,所以請謹記「==」才是用在比較的哦。
數字判斷
if(0)、if(1)也可以判斷?該怎麼使用?
if(0)裡面的0代表false
if(1)裡面的1代表true。其實不光是1,所有的正負數都可以。
接著舉一個非常簡單的例子
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
題目: 判斷該變數的值是否為0
- 當num為0時,if(0) –>if(false)
電腦因此跑來執行else大括號裡的程式碼
輸出結果:
- 當num為非0的整數時,則會得到true
電腦會執行if大括號裡的程式碼
輸出結果:
布林值邏輯運算子
&& (AND)
以兩個條件為例
- 兩個條件都必須得到true才是true
- 得到一個或0個true則為false
條件1 |
條件2 |
結果 |
T |
T |
T |
T |
F |
F |
F |
T |
F |
F |
F |
F |
條件1 |
條件2 |
結果 |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|| (OR)
以兩個條件為例
- 只要一個或一個以上條件為true則得到true
- 0個true則為false
條件1 |
條件2 |
結果 |
T |
T |
T |
T |
F |
T |
F |
T |
T |
F |
F |
F |
真值表
條件1 |
條件2 |
結果 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
用幾個字來分辨要使用 &&(AND) , ||(OR) 的時機
當你看到「都是」、「且」 那就直接使用 &&
當你看到「任一個」、「或」那就直接使用 ||
(AND) 舉個例子
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
題目: 判斷兩變數 num1 , num2 是否都大於 0
true && true 得到 true,進到if裡面執行程式碼。
輸出:
true && false 得到 false,進到else裡面執行程式碼。
輸出:
false && false 得到 false,進到else裡面執行程式碼。
輸出:
(OR) 舉個例子
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
題目: 判斷兩變數num1,num2是否有任一變數大於0
true || true 得到 true,進到if裡面執行程式碼。
輸出:
true || false 得到 true,進到if裡面執行程式碼。
輸出:
false || false 得到 false,進到else裡面執行程式碼。
輸出: