**
for循環結構的使用
**
1).循環的4要素
①初始化條件—>boolean類型
②循環條件
③循環體
④迭代條件
2)for循環的結構
for(①;②;④){
③
}
執行過程:①-②-③-④-②-③-④……②
嵌套循環的使用
1.嵌套循環:將一個循環結構A聲明在另一個循環結構B中。
2.外層循環:循環結構B
內存循環:循環結構A
3.說明:
①內層循環結構遍歷一遍,只相當於外層勳魂循環體執行一次
②假設外層循環需要執行m次,內層循環需要執行n次,此時內層循環的循環體一共執行 了m*n次
4.技巧:
外層循環控制行數,內層循環控制列數
練習1:
for(int i= 0; i<4;i++){
for(int j= 0; j<5;j++){
System.out.print("*");
}
System.out.println("*");
}
運行結果:
練習2:
for(int i= 0; i<5;i++){
for(int j= 0; j<i;j++){
System.out.print("*");
}
System.out.println("*");
}
運行結果:
練習3:
for(int i= 5; i>0;i--){
for(int j= 0; j<i;j++){
System.out.print("*");
}
System.out.println("*");
}
運行結果: