建中電子計算機研習社
迴圈
for while
do-while
break continue
array陣列
struct
函式
敘述1;
敘述2;
...
條件判定
for(初始值;條件式;更新式){
敘述1;
敘述2;
...
}
# FOR CODE
初始化
敘述
更新
條件判斷
end
true
false
for(int i=0;i<3;i++){
cout<<i<<"\n";
}
# FOR CODE
int i=0
cout<<i<<"\n";
i++
i<7
end
true
false
0
1
2
while(條件式){
敘述1;
敘述2;
...
}
# while CODE
敘述
條件判斷
end
true
false
int i=3;
while(i--){
cout<<i<<"\n";
}
# while CODE
cout<<i<<"\n";
i--
end
true
false
int i=3;
2
1
0
變數
for(;;){
敘述1;
敘述2;
...
}
陣列
while(true){
敘述1;
敘述2;
...
}
迴圈(){
敘述1;
break;
敘述2;
...
}
# FOR CODE
敘述1;
敘述2;
break;
end
迴圈(){
敘述1;
continue;
敘述2;
...
}
# FOR CODE
敘述1;
敘述2;
continue;
變數
陣列
索引:
0
1
2
n-1
n-2
n-3
...
[n]項
int i;
int i=7;
int i[3];
int i[3]={7,5,8};
i=8+7;
i++;
i[2]=i[0]+i[1];
i[0]++;
int main(){
cout<<"Hello world\n";
}
回傳值型態 函式名稱(傳入值){
函式主體;
return 回傳值;
}
const int mod=1e9+7;//這行不是
int power(int x,int y){
int ans=1;
for(int s=1;s<=y;s<<=1,x=x*x%mod)
if(s&y) ans=ans*x%mod;
return ans;
}
函式名稱(傳入值);
int main(){
int a=power(2,5);
cout<<a<<"\n"; //32
}
int hello(string name){
cout<<"hello "<<name<<"\n";
}
int main(){
hello();
}
int hello(string);
int main(){
hello();
}
int hello(string name){
cout<<"hello "<<name<<"\n";
}
struct struct名稱{
// 宣告變數
// 宣告函數
};
struct student{
string name;
int number;
void output(){
cout<<number<<"號: "<<name<<"\n";
}
};
struct名稱 變數名稱;
student _807;
struct a{
int b;
int c;
a(int B,int C):b(B),c(C){
cout<<"a is contructed";
}
};
int main(){
a A(1,2);
}