Informal Run-Time Analysis

A loop's statements don't all run the same number of times. This segment prints a music student's practice schedule over six days.
for (int day = 1; day <= 6; day++)
{
System.out.println("Warm up");
for (int song = 1; song <= 3; song++)
{
System.out.println("Play");
}
if (day % 2 == 0)
{
System.out.println("Review");
}
}
System.out.println("Done");
One count per statement
Each statement gets its own count, read from the braces around it.
StatementRunsWhy
System.out.println("Warm up")6in the outer body
System.out.println("Play")183 songs on each of 6 days
System.out.println("Review")3days 2, 4, and 6
System.out.println("Done")1after the last day
The condition day % 2 == 0 is checked every day, 6 times, though Review prints only 3 times. The outer condition day <= 6 is checked 7 times, since the seventh check, at 7, is the false one. The inner condition is checked 4 times a day for 6 days, which makes 24.
When the inner loop changes
With the inner header changed to song <= day + 1, the student plays two songs on day 1, three on day 2, and so on. The inner loop's count now changes from day to day. So "Play" runs 2 + 3 + 4 + 5 + 6 + 7 times, which is 27.
Comparing growth
Counts also show how the work grows when the input grows. Over 12 days instead of 6, the three-songs-a-day version prints Play 36 times, twice as often as before. The one-more-each-day version prints it 2 + 3 + ... + 13 times, which is 90, more than three times its 27. Doubling the days doubled the work in the first version. In the second, the work more than tripled, because the number of days and the songs per day both grew.