[Java in NetBeans] Lesson 10. For Loops

时间:2022-03-07 00:47:02

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

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

1. x++, x += 1; similar x--, x -= 1; x *= 2; x /= 2.

  • x++ : Plus 1 after  the line is executed. similar x--
x = 2;
System.out.printf(" x is now : %d", x++);
System.out.printf(" x is : %d", x);

The result of above, is

x is now : 2
x is : 3
  • x += 1      equals   x = x + 1; similar x -= 1; x *= 2; x /= 2.

2. For loop

  • for (int i = 0; i < max; i ++){}   will go through 0  unitl max -1, but intotal is max.

[Java in NetBeans] Lesson 10. For Loops

  • for (int i = 1; i <= max; i ++){}   will go through 1  unitl max, but intotal is max.
  • Similar for (int i = max; i > 0; i --){} will go through max  unitl 1, but intotal is max.
  • for (int each : numArray) {}  will go through each element/object in the array/collections.
int[] numArray = new int[] { 3, 4, 5, 8};
for (int each: numArray){
System.out.printf("%d "); }

the results of above will be like

3 4 5 8