C++

2 - 條件判斷、迴圈

講師 陳曉璇

目錄 Index

迎新

10/30 七校聯合!

關於Zerojudge

– 課程代碼:Vdz01P

AC後都直接按交卷就好

今天的練習在 zsisc29&30-9/29 裡面!

在ZeroJudge 完成作業之後,建議到GC作業留言妳的帳號

條件判斷

if else

#include <iostream>

using namespace std;

int main()
{
   float W,H ;
   cin >> W >> H;
   cout << W/(H*H) <<endl;
   return 0;
}

如果想要程式在計算完BMI是多少後,

判斷是否過輕、適中、過重、肥胖,要怎麼做呢

條件判斷

if(條件1){
如果條件1成立時執行;
}
else if(條件2){
如果條件1不成立、條件2成立時執行;
}
else if(條件3){
如果條件1不成立、條件2不成立、條件3成立時執行;
}
else{
如果上述條件皆不成立時執行
}
int score = -10;
if (score == 100){
cout << "滿分" << endl;
}
else if (score > 60){
cout << "及格" << endl;
}
else if (score > 0){
cout << "不及格" << endl;
}
else{
cout << "輸入錯誤" << endl;
}

輸入錯誤

條件的順序

if(條件1){
如果條件1成立時執行;
}
else if(條件2){
如果條件1不成立、條件2成立時執行;
}
else if(條件3){
如果條件1不成立、條件2不成立、條件3成立時執行;
}
else{
如果上述條件皆不成立時執行
}
int score = 100;
if (score == 100){
cout << "滿分" << endl;
}
else if (score > 60){
cout << "及格" << endl;
}
else if (score > 0){
cout << "不及格" << endl;
}
else{
cout << "輸入錯誤" << endl;
}

滿分

當score == 100時,

符合條件1、條件2、條件3

但只會執行最上面的那個⇒

條件的順序很重要!

順序亂排,條件就要訂的很完整

int score = 100;
if (score > 60){
cout << "及格" << endl;
}
else if (score == 100){
cout << "滿分" << endl;
}
else if (score > 0){
cout << "不及格" << endl;
}
else{
cout << "輸入錯誤" << endl;
}

及格

#include<iostream>
using namespace std;

int main(){
    int M,D,S;
    cin >> M >> D;
    S = (M*2+D)%3;
    
    if(S == 0){
    cout << "普通" <<endl;
    }
    else if(S == 1){
    cout << "吉" <<endl;
    }
    else{
    cout << "大吉" <<endl;
    }
    
    return 0;
}

巢狀If if裡面還有if

if(條件1){
    if(條件a){
      如果條件1成立且條件a成立時執行;
    }
    else if(條件b){
      如果條件1成立且條件a不成立、條件b成立時執行;
    }
    else{
      如果條件1成立且條件a、條件b皆不成立時執行
    }
}

else if 、else 同上

巢狀If 例題

年齡 過輕 適中 過重 肥胖
15 16.7 16.7~22.7 22.7 25.2
16 17.1 17.1~22.7 22.7 25.3
17 17.3 17.3~22.7 22.7 25.3
//提示
if(年齡等於...){
    if(過輕的範圍){
    輸出過輕
    }
    else if(適中的範圍){
    輸出適中
    }
    else if(過重的範圍){
    輸出過重
    }
    else{
    輸出肥胖
    }
}

補充 switch case

多用於取代一連串的 if-else 對於一個變數的連續判斷

switch (變數名稱或運算式) { 
    case 符合數字或字元: 
        執行; 
        break; 
    case 符合數字或字元: 
        執行; 
        break; 
    default: 
        執行; 
}

補充 switch case

#include<iostream>
using namespace std;

int main(){
    int M,D,S;
    cin >> M >> D;
    S = (M*2+D)%3;
    
    if(S == 0){
    cout << "普通" <<endl;
    }
    else if(S == 1){
    cout << "吉" <<endl;
    }
    else{
    cout << "大吉" <<endl;
    }
    
    return 0;
}
#include<iostream>
using namespace std;

int main(){
    int M,D,S;
    cin >> M >> D;
    S = (M*2+D)%3;
    
    switch(S){
    case 0:
        cout << "普通" <<endl;
        break;
    case 1:
        cout << "吉" <<endl;
        break;
    case 2:
        cout << "大吉" <<endl;
        break;
    }
    
    return 0;
}

改寫a003

補充   條件運算子 ? :

(條件 ? 條件為真回傳 : 條件為假回傳)
cout << ((19%2) ? "19不是偶數" : "19是偶數") << endl;
// 19%2 = 1,條件為真,回傳 "19不是偶數"
int x , y;
cin >> x >> y ;
cout << ((x>y) ? "x>y" : "X<=y") << endl;

for 迴圈

loop

#include <iostream>

using namespace std;

int main()
{
   float W,H ;
   cin >> W >> H;
   cout << W/(H*H) <<endl;
   return 0;
}

如果要計算BMI的身高體重不只一組要怎麼做呢

for 迴圈

for( 宣告初始變數 ; 須符合條件 ; 更新 ){
  執行;
}
for( int i=0 ; i<10 ; i++ ){
  cout << "Bless you" << '\n';
}
for( int i=0 ; i<10 ; i++ ){
  cout << i << '\n';
}

範例:級數

有一等差數列:

首項為4、公差為-2、末項為-50

求總和為多少

int sum=0;
for(int i=4 ; i>=-50 ; i-=2){
  sum+=i;
 }
cout << sum << endl;

有一等差數列,

首項為a0、公差為d、末項為an(正負不定)

求總和為多少

int a0,d,an,sum=0;
cin >> a0 >> d >> an;
for(int i=a0 ; i!=an ; i+=d){
  sum+=i;
}
sum+=an;
cout << sum << endl;

如果換成等比級數呢?

while 迴圈

loop

While 迴圈

while(須符合的條件){
  執行;
}
int DAY;
cin>>DAY;
while(DAY < 7){
  cout<<"Bless you"<<'\n';
}

避免無窮迴圈

更新條件

int DAY;
cin>>DAY;
while(DAY < 7){
  cout<<"Bless you"<<'\n';
  cin>>DAY; //DAY++;
}

補充 do while

先執行再判斷是否符合條件

#include <iostream>

using namespace std;

int main()
{
  int DAY;
  cin>>DAY;
  
  while(DAY < 7){
  cout<<"Bless you"<<'\n';
  cin>>DAY;
  }
  
  return 0;
}
#include <iostream>

using namespace std;

int main()
{
  int DAY;
  
  do{
  cout<<"Bless you"<<'\n';
  cin>>DAY;
  }while(DAY < 7)
  
  return 0;
}

無條件先執行 ⇒ 默認第一次輸入的DAY>7

迴圈應用

continue/break

繼續 / 破壞

for(int i=0;i<10;i++){
   if(i==5){
     break;
   }
   cout<<i<<" ";
}
for(int i=0;i<10;i++){
   if(i==5){
     continue;
   }
   cout<<i<<" ";
}

continue

break

0 1 2 3 4

0 1 2 3 4 6 7 8 9

迴圈配合條件判斷

for(int i=0;i<10;i++){
   if(i > 5 || i == 0){
     cout << i << ' ';
   }
}

0 6 7 8 9

巢狀迴圈 迴圈裡面還有迴圈

for(int i=1 ; i<=9 ; i++ ){
  for(int j=1 ; j<=9 ; j++ ){
    cout << j << '*' << i << '=' << i*j <<' '; 
    if(i*j < 10){
      cout<<' ';
    }
   }
   cout << endl;
}
//這是巢狀迴圈範例
int k=0,h=0;
for(int i=1 ; i<=9 ; i++ ){
  for(int j=1 ; j<=9 ; j++ ){
        h-=1;
    }
   k -= 1;
}
int i=4,sum=0;
while(i>=-50){
  sum+=i;
  i-=2;
}

for跟while是可以交換的!

有一等差數列:

首項為4、公差為-2、末項為-50

求總和為多少

while迴圈裡的條件

True / 1

→ 恆為正確

False / 0

→ 恆為錯誤

i--

→ 減到0時停止迴圈

配合break/continue

cin >>  變數

→ 直到EOF(End Of File)

Ctrl+Z

Kahoot!

前三名有獎品喔!

zsisc29th-2

By CHEN, SIAO SYUAN