Try   HackMD
tags: 大一程設 東華大學 東華大學資管系 基本程式概念 資管經驗分享

布林運算子一覽、if-else 條件判斷

if-else

語法

if(條件){ 該條件成立後,會做什麼 } else{ 該條件不成立,會做什麼 }

補充: 你可以單獨寫一個if判斷式就好,else不需要寫。 然而,當你寫了else則一定要幫他搭配一個if,簡單來說就是else不能單獨出現。

好了,想必看完上面大家都能明白程式怎麼寫了吧!我深信你們聰慧,但以防萬一,我還是要跟大家提一些if-else的運作機制。


if-else裡的條件怎麼判斷?

條件判斷

comparison Operators

C++ Notation 中文
== 等於
!= 不等於
< 小於
<= 小於等於
> 大於
>= 大於等於

電腦會去看if裡面的條件是否成立,如果成立的話,會回傳true,不成立的時候則會回傳false。

if(true){ 該條件成立後,會做什麼 } else{ 該條件不成立,會做什麼 }

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

int big=3; //宣告data type為整數,名稱為big的變數,並在此變數存放值3 int small=1; //宣告data type為整數,名稱為small的變數,並在此變數存放值1 if(big<small){ }

電腦在判斷條件 big<small 結束後,會回傳false, 至於為什麼回傳false? 那是因為3比1還要大,因此big<small的條件不成立。

綜合上述那些

if(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 →

if(false){ }

在電腦把條件判斷完後,會因為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 →

int main(){ string a = "你是小明", b = "我是小華"; if(a == b){ cout << "我們一樣"; } else{ cout << "我們不一樣"; } }

應該非常淺顯易懂,而你說為什麼,沒有為什麼,這是規定,所以把他記起來比較好。

當然一定有人學不乖,會在條件判斷裡只放一個等號。

int main(){ int a = 5, b = 10; if (a = b){ cout << "they\'re equal"; } else{ cout << "they\'re not equal"; } }

當然跳脫程式語言的框架,直觀的話他會是等於,但要記得在 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

int num; if(num){ cout<<"num的值不是0"; } else{ cout<<"num的值是0"; }
  • 當num為0時,if(0) >if(false)

電腦因此跑來執行else大括號裡的程式碼

else{ cout<<"num的值是0"; }

輸出結果:

num的值是0

  • 當num為非0的整數時,則會得到true 電腦會執行if大括號裡的程式碼
if(num){ cout<<"num的值不是0"; }

輸出結果:

num的值不是0

布林值邏輯運算子

&& (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

  • 當num1,num2都大於0
int num1=1,num=2; if((num1>0)&&(num2>0)){ cout<<"兩個都大於0"; }
num1>0 ->得到true
num2>0 ->得到true

true && true 得到 true,進到if裡面執行程式碼。

輸出:

兩個都大於0
  • 當num1,num2只有其中一個大於0
int num1=1,num=0; if((num1>0)&&(num2>0)){ cout<<"兩個都大於0"; } else{ cout<<"並沒有都大於0"; }
num1>0 ->得到true
num2>0 ->得到false

true && false 得到 false,進到else裡面執行程式碼。

輸出:

並沒有都大於0
  • 當num1,num2皆不大於0
int num1=0,num=0; if((num1>0)&&(num2>0)){ cout<<"兩個都大於0"; } else{ cout<<"並沒有都大於0"; }
num1>0 ->得到false
num2>0 ->得到false

false && false 得到 false,進到else裡面執行程式碼。

輸出:

並沒有都大於0

(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

  • 當num1,num2都大於0
int num1=1,num=2; if((num1>0)||(num2>0)){ cout<<"任一或一個以上變數大於0"; }
num1>0 ->得到true
num2>0 ->得到true

true || true 得到 true,進到if裡面執行程式碼。

輸出:

任一或一個以上變數大於0
  • 當num1,num2只有其中一個大於0
int num1=1,num=0; if((num1>0)||(num2>0)){ cout<<"任一或一個以上變數大於0"; } else{ cout<<"都沒有大於0"; }
num1>0 ->得到true
num2>0 ->得到false

true || false 得到 true,進到if裡面執行程式碼。

輸出:

任一或一個以上變數大於0
  • 當num1,num2皆不大於0
int num1=0,num=0; if((num1>0)||(num2>0)){ cout<<"任一或一個以上變數大於0"; } else{ cout<<"都沒有大於0"; }
num1>0 ->得到false
num2>0 ->得到false

false || false 得到 false,進到else裡面執行程式碼。

輸出:

都沒有大於0