Try   HackMD

CPP基礎.Lesson 2

if, eles if, else

水獺今天對發票,他心裡想著
如果我中200我要喝一杯珍奶
中500想吃麥當當
中1000萬想要環遊世界
但是因為他很健忘常常忘記
於是他寫下了這個程式

#include <iostream> using namespace std; int main() { int money = 0; cin >> money; if( money == 200 ){//== 比較左右兩邊的值 cout << "喝珍奶" << endl; } else if( money == 500 ){ cout << "吃麥當當" << endl; } else if( money == 10000000 ){ cout << "環遊世界" << endl; } else{ cout << "生氣氣" << endl; } }

語法教室

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:

true 就會執行{}內的code

if( 條件 ){
    如果條件成立要做的事...
}

else:

if條件不成立時要做的事(一定要接在if後面)

else{
    要做的事...
}

else if:

夾在ifelse中間,用法同if,但是最多只會執行其中一個{ }內的程式。通常用在多種情況選一種的時候。

if( 條件 ){
    如果條件成立要做的事...
}
else if( 條件1 ){
    要做的事...
}
else if( 條件2 ){
    要做的事...
}
else if( 條件3 ){
    要做的事...
}
else{
    要做的事...
}

可以使用>,==,<來當作條件
像是a>b之類的

在程式的世界裡==是比較兩邊的值一不一樣
=是把右邊的值複製一份給左邊
兩者不太一樣要注意唷

for loop

假設今天我們想要印出10行我想躺躺我們可以這麼做

我好累好累我要天天躺躺水獺Wed, May 19, 2021 8:34 PM

#include <iostream> using namespace std; int main() { cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; cout << "我想躺躺" << endl; }

但是如果一次想印100行,總不能永遠的copy paste 吧
所以就發明了loop(迴圈)來幫助我們執行重複的指令~
我們只需將程式寫成這樣

#include <iostream> using namespace std; int main() { for(int i=0; i<10; i++){ cout << "我想躺躺" << endl; } }

語法教室

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 →

for loop

做什麼事

for( 一開始先做什麼事; 條件; 每執行完一次{}內的code,就做什麼事){
    要重複做的事...
}

小練習

a244: 新手訓練 ~ for + if

while loop

水獺抱怨完了
是時候補充能量了!!

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 →

#include <iostream> using namespace std; int main() { int energy=0; while( energy < 100 ){ bool comfortable; cin >> comfortable; if( comfortable == true ){//== 比較左右兩邊的值 energy++; } else{ cout << "生氣氣" << endl; } } cout << "充飽電ㄌ" << endl; }

語法教室

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 →

while loop

true 就會執行{ }內的code

while( 條件 ){
    要重複做的事...
}

回家作業

d072: 格瑞哥里的煩惱 (Case 版)

c420: Bert的三角形 (3)