for 迴圈語句基本用法及示例
阿新 • • 發佈:2019-01-04
/*
for 迴圈語句:迴圈結構
for(初始化表示式;迴圈條件表示式;玄幻後的操作表示式)
{
執行語句;(迴圈體)
}
*/
class ifDemo
{
public static void main(String[] args)
{
for(int x =1;x<3;x++)
{
System.out.println("x=" + x);
}
}
}
// for 迴圈定義
for(int a =0,b=0; a<3;a++,b--)
{
System.out.println("a=" + a + ";b=" + b);
}
//for 迴圈巢狀語句
輸出:
/*
*****
****
***
**
*
*/
class ifDemo
{
public static void main(String[] args)
{
for(int i=1;i<6;i++)
{
for(int x=i;x<=5;x++)
{
System.out.print("*");
}
System.out.println();
}
}
}