大社-if & else

c++ basic syntax

— Große Gefieder&Greg Chiu

布林值

 

1 = True

0 = False

邏輯運算子

== 等於
!= 不等於
! not
&& and(和)
|| or(或)

範例code

#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";}

}
  • 條件判斷語法
  • 應該會很常用到(?

what is if & else 

if(判斷式){
   你想要做的事;
}
else{
   不然就做什麼事;
}


語法

    Text

範例 code

#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;
}

範例 code

  • 當有三個條件的時候用(有點像是英文中的"another")
  • 比序:if          else if          else 

else if

if(判斷句)
{你想要做的事}
else if(判斷句)
{你想要做的事}
else 
{不然就做什麼事}

語法

    Text

範例code

#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;
}
  • 結論:else if 是在判斷if 之後如果不成立才會進行

如果全部都用if呢(有點毒瘤,但我喜歡)

#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;
}
  • 與if-else偏像
  • 分case處理所有條件

switch

switch(變數名稱)
{case 符合ㄉ數字、字元:
        {你想做的事};
          break;
 case 符合ㄉ數字、字元:
        {你想做的事};
           break;
}

 

語法

  • Conceptualization
  • Product Design
  • Development
  • UI/UX Testing
  • Branding

範例code

#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;
}
}

好玩ㄉ題目

 
    Text

ans(a003)

#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; 
    }
}
    Text

ans(a053)

#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";
  }
}
    Text

ans(d064)

#include<iostream>
using namespace std;

 
int main()
{
  int i;
  cin>>i;

  if (i%2==1){
    cout<<"Odd";
  }
  else{
    cout<<"Even";
  }
}
Made with Slides.com