Java学习—— for循环

For双重循环

/*
循环语句嵌套

*/
class ForForTest
{
    public static void main(String[] args)
    {
        /*int x,y = 0;
        for(x = 0; x<=5; x++)
        {
            for(y = x; y<=5; y++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("------------------------->");
        */
        /*int x,y = 0;
        for ( x = 0; x < 5 ; x++ )
        {
            for (y = 0; y<=x ; y++ )
            {
                System.out.print("*");
            }
            System.out.println();
        }
        */
        for (int x = 1; x <= 9 ; x++ )
        {
            for (int y = 1; y<=x ; y++ )
            {
                System.out.print(y+"*" + x + "=" + y*x + "	");
            }
            System.out.println();
        }
    }    
}
/*

对于打印长方形:外循环控制的行数,内循环控制每一行的列数,也就是一行中元素的个数。
使用循环嵌套。原理:形象说法:大圈套小圈。

----*
   * *
  * * *
 * * * * 
* * * * *
*/

break跳出:

class OtherDemo {

    //break:
        public static void main(String[] args)
       {
             q:for (int x=0,;x<3; x++) {
                w:for (int y=0; y<4 ; y++ )
                {
                    System.out.println("x="+ x);
                    break q;//跳出q循环。
                }
            }
        //continue:只能用于循环结构。继续循环。结束本次循环,
        //继续下一次循环
        for (int x=0; x<3 ; x++ )
        {
            if (x%2==1)
            {
                continue;
                System.out.println("x="+x);
            }
            
        }
        
         q:for (int x=0,;x<3; x++) {
                w:for (int y=0; y<4 ; y++ )
                {
                    System.out.println("x="+ x);
                    continue q;
                }
            }
        /*
        记住:
        1,break和continue语句作用范围。
        2,break和continue单独存在时,下面不能有任何语句,因为都执行不到。
        */
    }

} 
原文地址:https://www.cnblogs.com/wuyong0818/p/5034804.html