Loading topic
for and while loops and that you know the idea from the previous chapter: a step is one constant-time basic operation, such as an assignment, a comparison, or an addition. Here we apply that idea to the simplest structure that makes running time grow with3 min readfor and while loops, know how range works, and are comfortable with the ceiling and floor functions. The previous chapter counted a plain range(n) loop. Real loops often start somewhere other than zero, skip values, or count downward. You can still fi3 min readfor loops and count how many times a single loop runs from its start, stop, and step values. It also assumes you know that the costs of blocks running one after another add together. Here you learn what happens when one loop sits inside another.3 min readfor loops, count the iterations of a single range, multiply the costs of independent nested loops, and use the arithmetic series formula 1 + 2 + ... + k = k(k+1)/2 from the previous chapter.3 min readwhile loops, count the iterations of a simple loop from its bounds, and work with logarithms such as log₂ 8 = 3.3 min readfloor(log2 n) + 1 iterations, and sum a geometric series. Here we combine those skills. A linear loop and a logarithmic loop can nest in three common ways, and they do not all 3 min read3n + 2 or `3 min readn. A loop that can return or break early is different. Its count depends on what the d3 min read