The Escuela

← Analysis of Algorithms: all 3 parts

Computer science · Free practice

Analysis of Algorithms, Part 1: Counting Instructions in a Loop

Watch the lesson by Dr. J. Ubaldo Quevedo, then practice as many times as you want: every New button gives you a new loop or sum.

1. Watch

  • Count each assignment as 1. A for loop's initialization runs once, its test runs once more than the body, and its increment runs once per pass.
  • For for (int i = 0; i < n; i++) the body runs n times: 1 + (n + 1) + n for the loop itself.
  • int sum = 0; for (…) sum += i; executes 1 + 1 + (n + 1) + n + n = 3 + 3n instructions.

2. Try it yourself

Practice as many times as you want. Every New button builds a new loop or sum. It's free, and you don't need an account.

Correct so far: 0