c++ basic syntax
— Große Gefieder&Greg Chiu
布林值
1 = True
0 = False
邏輯運算子
== | 等於 |
---|---|
!= | 不等於 |
! | not |
&& | and(和) |
|| | or(或) |
#include<iostream>
using namespace std;
int main()
{
bool homework=true;
bool laundry=true;
bool mopfloor=false;
if(homework && laundry)
{cout << "PLAY\n";}
if(!homework || !laundry || !mopfloor)
{cout << "STAY\n";}
}
if(判斷式){
你想要做的事;
}
else{
不然就做什麼事;
}
#include<iostream>
using namespace std;
int main(){
string a;
cout<<"請問你繳社費了嗎?(yes/no)\n";
cin>>a;
if(a=="yes"){
cout<<"你超棒的\n";
}
else{
cout<<"請盡快繳交社費喔\n";
}
return 0;
}
if(判斷句)
{你想要做的事}
else if(判斷句)
{你想要做的事}
else
{不然就做什麼事}
#include<iostream>
using namespace std;
int main(){
int a=1;
if(a==1){
cout << "if-else好好玩\n";
}
else if(a==10){
cout << "還是不太懂\n";
}
else{
cout << "我需要支援\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a=1;
if(a==1)
{cout << 'A' << '\n';}
if(a<10)
{cout << 'B' << '\n';}
if(a<=2147483647)
{cout << 'C' << '\n';}
return 0;
}
switch(變數名稱)
{case 符合ㄉ數字、字元:
{你想做的事};
break;
case 符合ㄉ數字、字元:
{你想做的事};
break;
defualt:
break;
}
#include<iostream>
using namespace std;
int main()
{
int tar;
cin >> tar;
switch(tar)
{
case 0:
cout << "zero\n";
break;
case 1:
cout << "one\n";
break;
case 2:
cout << "two\n";
break;
}
}
#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 if(S==2){
cout<<"大吉" <<endl;
}
}
#include<iostream>
using namespace std;
int main()
{
int num;
cin>>num;
if (num<=10){
cout<<6*num;
}
else if (num<=20){
cout<<60+2*(num-10);
}
else if (num<=40){
cout<<80+(num-20);
}
else{
cout<<"100";
}
}
#include<iostream>
using namespace std;
int main()
{
int i;
cin>>i;
if (i%2==1){
cout<<"Odd";
}
else{
cout<<"Even";
}
}