[Java in NetBeans] Lesson 11. While Loops

时间:2021-01-31 19:31:11

这个课程的参考视频和图片来自youtube

主要学到的知识点有:(the same use in C/C++)

1. while loop

  • while(i < max){} will keep executing if i < max is true, otherwise will jump out from the while loop. Possible execute 0 times.
max = 5;
i = 0;
while(i < 5){
System.out.printf("%d ", i);
i++;}
// the result will be like this
0, 1, 2, 3, 4
  • do {} while (i < max) is similar like before, only difference is that the loop body will be execute at least once.
  • while(true) { if (i < max) {break;}}   Whenever see break; will jump out from the while loop.
max = 5;
i = 0;
while(true){
System.out.printf("%d ", i);
i++;
if (i >= max){
break;}}
// the result will be like this
0, 1, 2, 3, 4