From the course: C Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Iterating with for

Iterating with for - C Tutorial

From the course: C Essential Training

Iterating with for

- [Narrator] C provides two versions of the for loop. One is inherited from C and the other is unique to C . Let's look at the C for loop first, as this is by far the most common. This is for.cpp from Chapter three of the exercise files and that's the word for, not the number four. F-O-R.cpp. If you remember, with the while loop, we had to initialize the index variable before the loop. We also had to increment the variable at the end of the loop. The for loop is like the while loop with controls for initializing and incrementing an index. So, here we have, around line 12, we have the index being incremented and then a semicolon and then, the condition, just like in the while loop, another semicolon, and then the increment. This is all in one statement called for. So, when I build a run, you see we get the same results that we had with our while loop. So, there's three loop control expressions. The…

Contents