1. 程式人生 > >[Java in NetBeans] Lesson 10. For Loops

[Java in NetBeans] Lesson 10. For Loops

這個課程的參考視訊和圖片來自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. 

  • 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