條件判斷/迴圈
if else/for while
Index
條件判斷
條件判斷
如果...就...
條件判斷
如果...就...
如果今天天氣晴朗,我就要吃蛋餅

條件判斷
如果...就...
如果今天天氣下雨,我就要吃飯糰

if(條件1)
{
條件1成立時執行;
}
else
{
條件1不成立時執行;
}#include <iostream>
using namespace std;
int main()
{
string weather,breakfast;
cin>>weather;
if(weather=="sunny"){
breakfast="Chinese omelet";
}
else{
breakfast="rice ball";
}
cout<<breakfast;
return 0;
}條件判斷
如果...就...
如果今天天氣多雲,我就要吃漢堡

if(條件1)
{
條件1成立時執行;
}
else if(條件2)
{
條件1不成立條件2成立時執行;
}
else
{
條件1、2皆不成立時執行;
}#include <iostream>
using namespace std;
int main()
{
string weather,breakfast;
cin>>weather;
if(weather=="sunny"){
breakfast="Chinese omelet";
}
else if(weather=="cloudy"){
breakfast="hamburger";
}
else{
breakfast="rice ball";
}
cout<<breakfast;
return 0;
}?
現實世界沒那麼單純吧
但如果...
事情有先後順序跟重疊性的話
獎勵機制
| 分數 | 獎金 |
|---|---|
| 100 | 1000 |
| 90~99 | 500 |
| 89~80 | 300 |
| 79~70 | 100 |
| 69以下 | 0 |
#include <iostream>
using namespace std;
int main()
{
int score,money;
cin>>score;
if(score==100) money=1000;
if(90<=score<=99) money=500;
if(80<=score<=89) money=300;
if(70<=score<=79) money=100;
if(score<=69) money=0;
cout<<money;
return 0;
}#include <iostream>
using namespace std;
int main()
{
int score,money;
cin>>score;
if(score==100) money=1000;
else if(score>=90) money=500;
else if(score>=80) money=300;
else if(score>=70) money=100;
else money=0;
cout<<money;
return 0;
}#include <iostream>
using namespace std;
int main()
{
int score,money;
cin>>score;
if(score==100) money=1000;
if(90<=score<=99) money=500;
if(80<=score<=89) money=300;
if(70<=score<=79) money=100;
if(score<=69) money=0;
cout<<money;
return 0;
}- 條件越明確擺在越前面(注意順序!!)
- else if可以有好幾個
- else 後面不用加條件
- 當第一個條件不成立時,才會確認第二個條件是否成立
三元運算子
a?b:c
//若a為真 執行b 否則執行c#include<iostream>
using namespace std;
int main()
{
int grade=88;
cout<<(grade==100?"get 1000":"noooo");
return 0;
}noooo
While迴圈
while(條件)
{
達成條件時執行;
}#include <iostream>
using namespace std;
int main()
{
int date;
cin>>date;
while(date==228)
{
cout<<"放假"<<endl;
}
cout<<"面對現實吧!"<<endl;
return 0;
}放假放假放假放假
.
.
.
一直放假的無窮迴圈
while(條件)
{
達成條件時執行;
}#include <iostream>
using namespace std;
int main()
{
int date;
cin>>date;
while(date==228)
{
cout<<"放假"<<endl;
}
cout<<"面對現實吧!"<<endl;
return 0;
}放假放假放假放假
.
.
.
一直放假的無窮迴圈
while(條件)
{
達成條件時執行;
}#include <iostream>
using namespace std;
int main()
{
int date;
cin>>date;
while(date==228)
{
cout<<"放假"<<endl;
cin>>date;
}
cout<<"面對現實吧!"<<endl;
return 0;
}放假放假放假放假
.
.
.
改變變數值
For迴圈
for(宣告初始變數;條件;更新)
{
達成條件時執行;
}#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=0;i<5;i++)
{
sum+=1;
}
cout<<sum;
}| i值 | sum值 |
|---|---|
| 0 | |
| 0 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
練習
在等差數列A<n>中,首項為A1、公差為d、末項為An,輸入A1、d、An,求S<n>為何?
輸入:1 2 11
輸出:36
A1=1, d=2, An=11
S<n>=1+3+5+7+9+11=36
(試試while跟for的寫法)
While
#include <iostream>
using namespace std;
int main()
{
int A1,d,An,Sn=0,num=0;
cin>>A1>>d>>An;
num=A1;
while(num!=An)
{
Sn+=num;
num+=d;
}
Sn+=An;
cout<<Sn;
}For
#include <iostream>
using namespace std;
int main()
{
int A1,d,An,Sn=0;
cin>>A1>>d>>An;
for(int i=A1;i!=An;i+=d)
{
Sn+=i;
}
Sn+=An;
cout<<Sn;
}continue
break
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{
if(i==2) continue;
cout<<i<<endl;
}
}0
1
3
4
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{
if(i==2) break;
cout<<i<<endl;
}
}0
1
繞過他
跳離迴圈
巢狀迴圈
#include <iostream>
using namespace std;
int main()
{
int n;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
n=i*j;
cout<<n<<' ';
}
cout<<endl;
}
}輸出結果
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
KAHOOT
| 年齡 | 姓名 | 身高 |
|---|---|---|
| 10 | 柏拉圖 | 120 |
| 7 | 亞里斯多得 | 130 |
| 12 | 蘇格啦底 | 115 |
| 5 | 亞莉山大 | 110 |
| 19 | 西羅多德 | 160 |
| 4 | 柏里克里斯 | 50 |
| 3 | 必達哥拉斯 | 100 |
都我亂掰的
#include <iostream>
using namespace std;
int main()
{
int age,tall;
bool a,b;
cin>>age>>tall;
if(tall>=120) a=true;
else a=false;
if(age<=12) b=true;
else b=false;
bool s=(a==b);
cout<<s;
return 0;
}#include <iostream>
using namespace std;
int main()
{
int sum;
int A1,An,r;
cin>>A1>>An>>r;
for(int i=A1;i!=An;i=i*r)
{
sum=sum+i;
}
cout<<sum;
return 0;
}#include <iostream>
using namespace std;
int main()
{
int date;
cin>>date;
if(date==228||date==301||date==302) cout<<"dayoff";
else if(date==308) cout<<"sitcon";
else cout<<"class";
}#include <iostream>
using namespace std;
int main()
{
int grade=70;
cout<<(grade!=80?"ha?":"wo");
}條件判斷與迴圈
By chainy
條件判斷與迴圈
- 136