In addition to other nice contributions, I am a fan of for-each loop and usually do it on one line.
for(int i : absencesArr) absenceTotal += i; System.out.printf("There were %d absences that day.", absenceTotal);
But in some situations, when I want to control my object size / length / counter, I will use for loop , as in the following example:
for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i]; System.out.printf("There were %d absences that day.", absenceTotal);
And if I need to have more than one line of codes inside a for loop or for-each loop , then I put them all in curly braces { more than one line of code } .
source share