一:for循環語句
1.當語句需要進行多次累加計算時需運用循環語句.
2.for語句的一般形式爲:for(循環變量初始化;終止條件表達式;循環變量的增量){ 中間循環體;}
3.for循環執行時,會先判斷條件表達式是否成立,如果條件成立則執行中間循環體,執行完中間循環體後接着執行末尾循環體 。在執行完末尾循環體後對條件表達式再次進行判斷,若條件還成立,則繼續重複中間循環體,當條件不成立時則跳出for循環。
[ 總結就是: for(a; b; c) { d} 就是先判斷a在b條件是否成立, 若成立則執行d, 再接着執行c看條件是否成立若條件還成立,則繼續重複d,當條件不成立時則跳出for循環。]
4.for循環的實例
public class TestFor4{
public static void main(String[] args){
int x=1;
for(System.out.println("a"); x<3;System.out.println("c")){
System.out.println("d");
x++;
}
}
}
運行結果:
二:for循環嵌套語句
1.循環嵌套:一個循環語句可以包含另一個循環語句.常用的循環嵌套是二重循環,外層循環稱爲 外循環,內層循環稱爲內循環。
2.for循環語句的嵌套的格式:
for (循環變量初始化;終止條件表達式;循環變量的增量){語句或語句塊;
for(循環變量初始化;終止條件表達式;循環變量的增量){語句或語句塊;}
}
3.嵌套循環的執行邏輯:
3.1.外層判斷循環條件,滿足進入外層循環體
3.2.內層判斷循環條件
3.3.內層循環體執行
3.4.內層循環變量累加,回到3.2執行,直到不滿足內層條件
3.5.外層循環變量累加,回到3.1執行,直到不滿足外層循環條件,徹底退出循環
三:for循環嵌套語句的實例
/*
for循環的嵌套應用
(循環嵌套先執行外循環的條件成立時則執行內循環,知道內循環執行結束;
再執行外循環條件,成立時再執行內循環.依次循環)
1,打印五行五列的圖形 *****
*****
*****
*****
*****
2.打印右面的圖形 *****
****
***
**
*
思路:1.1每個圖形都是五行五列,且第一個圖形其行數與列數相同,第二個圖形的行數在依次減少
2.1一看圖形應使用循環語句且需嵌套
3.1圖形每到5個*時都需要換行
*/
public class TestFor2{
public static void main(String[] args){
for(int x=0;x<5;x++){ //控制行數
for(int y=0;y<5;y++){ //控制列數
System.out.print("*");
}
System.out.println();
}
System.out.println("----------------------------------");//分割符,需注意執行語句是順序
testFor3();
}
public static void testFor3(){
for(int a=0;a<5;a++){
for(int b=a;b<5;b++){
System.out.print("*");
}
System.out.println();
}
}
}
運行結果:
/*
*****
*****
*****
*****
*****
-----------
*****
****
***
**
*
--------
*
**
***
****
*****
---------
54321
5432
543
54
5
----------
5
54
543
5432
54321
----------
1
22
333
4444
55555
---------
11111
2222
333
44
5
*/
public class TestFor5{
public static void main(String[] args){
testFor1();
testFor2();
testFor3();
testFor4();
testFor6();
testFor7();
testFor8();
}
public static void testFor1(){
for(int x=1;x<=5;x++){
for(int y=1;y<=5;y++){
System.out.print("*");
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor2(){
for(int a=1;a<=5;a++){
for(int b=a;b<=5;b++){
System.out.print("*");
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor3(){
for(int c=1;c<=5;c++){
for(int d=1;d<=c;d++){
System.out.print("*");
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor4(){
for(int m=1;m<=5;m++){
for(int n=5;n>=m;n--){
System.out.print(n);
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor6(){
for(int e=1;e<=5;e++){
for(int f=1;f<=e;f++){
System.out.print(f);
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor7(){
for(int e=1;e<=5;e++){
for(int f=1;f<=e;f++){
System.out.print(e);
}
System.out.println();
}
System.out.println("--------------");
}
public static void testFor8(){
for(int e=1;e<=5;e++){
for(int f=5;f>=e;f--){
System.out.print(e);
}
System.out.println();
}
}
}
運行結果:
五:用for循環嵌套語句寫出99乘法表
public class TestForJiu{
public static void main(String[] args){
for(int x=1;x<=9;x++){
for(int y=1;y<=x;y++){
System.out.print(y+"*"+x+"="+y*x+"\t");
}
System.out.println( );
}
}
}
運行結果: