Loops

Learn about three types of loops: the for loop, the while loop, and the do-while loop.

One of the most useful properties of programmable computers is that you can ask them to repeat a calculation or operation many many times, and they will not (usually) complain. The looping constructs in C allow us to repeatedly execute a block of code many times, without having to manually re-type or re-list the code by hand.

As a simple example, let’s say we want to compute the cumulative sum of integers between 1 and 100. We could write code like the following:

int cumsum = 0;
cumsum = cumsum + 1;
cumsum = cumsum + 2;
cumsum = cumsum + 3;
...
cumsum = cumsum + 100;

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy