Skip to main content

Section 13.1 The while Loop

Remember our program, using a for-loop, to compute the factorial of a number? Essentially, it went like this:
N = 5; 
fac = 1; 
for (i=1; i<=N; i++) { 
  fac = fac * i; 
}
C has three different types of loops. Sometimes we want to loop through a section of code, but we do not know how many times we want it to execute. Maybe we want it to repeat the code while a certain condition is true, or maybe while that condition is not met. In these sorts of cases, a while-loop will come in handy!

If you cannot see this codecast, please click here.

Aside: Video Description.