คำสั่งวนลูป (Loops)

while (condition) {
  // loop-body;
  Statement(s);
}
Trace while Loop
int count = 0;
while (count < 2) {
  System.out.println("Welcome to Java!");
  count++;
}
Trace while Loop, cont.
int count = 0;
while (count < 2) {
  System.out.println("Welcome to Java!");
  count++;
}
Caution
ห้ามใส่ ; หลังประโยค for loop
for (int i=0; i<10; i++);
{
  System.out.println("i is " + i);
}
Caution, cont.
Similarly, the following loop is also wrong:
int i=0;
while (i < 10);
{
  System.out.println("i is " + i);
  i++;
}
In the case of the do loop, the following semicolon is needed to end the loop.
int i=0;
do {
  System.out.println("i is " + i);
  i++;

} while (i<10);

กลับไปหน้าจาวา

 

หน้าหลัก บทเรียน ประวัติ เพื่อน