[Java in NetBeans] Lesson 11. While Loops
阿新 • • 發佈:2018-12-20
這個課程的參考視訊和圖片來自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