DAY 3
1
Software Development

## 99乘法表到底可以怎麼玩? [2] 迴圈

``````int main(){
for(int y=1;y<=9;y++){
for(int x=1;x<=9;x++){
printf("%d*%d=%d\t",y,x,y*x );
}
printf("\n");
}
return 0;
}
``````

======================分格線======================

``````int y=1;
int x=1;
int main(){
while (y<=9){
while (x<=9){
printf("%d*%d=%d\t",y,x,y*x );
x++;
}
y++;
x=1;
printf("\n");
}
return 0;
}
``````

======================分格線======================

``````int y=1;
int x=1;
int main(){
do{
do{
printf("%d*%d=%d\t",y,x,y*x );
x++;
}while (x<=9);
y++;
x=1;
printf("\n");
}while (y<=9);
return 0;
}
``````

while跟do...while loop，兩者最大分別是：前者會先條件判斷，後者會先執行一次再作條件判斷

======================分格線======================

for、while、do...while一定要同時一對使用嗎？

``````int main(){
for(int y=1,x=1; y<=9 ;x++){
printf("%d*%d=%d\t",y,x,y*x);
x<9 ? x:(x=0,y++,printf("\n"));
}
return 0;
}
``````

======================分格線======================

``````int y=1;
int x=1;
int main(){
while(y<=9){
printf("%d*%d=%d\t",y,x,y*x);
x<9 ? x++:(x=1,y++,printf("\n"));
}
return 0;
}
``````

======================分格線======================

while loop可以，同樣道理do...while loop也是沒問題了

``````int y=1;
int x=1;
int main(){
do{
printf("%d*%d=%d\t",y,x,y*x);
x<9 ? x++:(x=1,y++,printf("\n"));
}while(y<=9);
return 0;
}
``````

======================分格線======================

3個常用的loop已經用完了

``````int y=1;
int x=1;
int main(){
if(x<=9 && y<=9){
printf("%d*%d=%d\t",y,x,x*y);
x++;
}
if(x>9){
x=1;
y++;
printf("\n");
}
return main();
}
``````

======================分格線======================

``````int y=1;
int x=1;
int main(){
LOOP: if(x<=9 && y<=9){
printf("%d*%d=%d\t",y,x,x*y);
x++;
}
if(x>9){
x=1;
y++;
printf("\n");
}
goto LOOP;
return 0;
}
``````

======================分格線======================

``````int x=1;
int y=1;
int table (int x, int y){
if (x<=9){
if (y<=9){
printf("%d*%d=%2d\t",x,y,x*y);
table(x,y+1);
}
else{
printf("\n");
x++;
table(x,1);
}   }   }
int main() {
table(x, y);
return 0;
}
``````

======================分格線======================

「方法很多種，但答案只有一個。」一個簡單的九九乘法表至少可以用9種方式來完成。