| C Tutorial | For loop | |||
|
If Statement Ternary Operator Switch Statement While Loop Do-While Loop For Loop Break Continue
|
The for loop in C is the most general looping construct. The loop header contains three parts: an initialization, a continuation condition, and an action.
for (<initialization>; <continuation>; <action>) { <statement> }
The initialization is executed once before the body of the loop is entered. The loop continues to run as long as the continuation condition remains true (like a while). After every execution of the loop, the action is executed. The following example executes 10 times by counting 0..9. Many loops look very much like the following...
for (i = 0; i < 10; i++) { <statement> }
C programs often have series of the form 0..(some_number-1). It's idiomatic in C for the above type loop to start at 0 and use < in the test so the series runs up to but not equal to the upper bound. In other languages you might start at 1 and use <= in the test.
Each of the three parts of the for loop can be made up of multiple expressions separated by commas. Expressions separated by commas are executed in order, left to right, and represent the value of the last expression. (See the string-reverse example below for a demonstration of a complex for loop.)
Want To Know more with Video ???
|
|