‘for’ loop in C
for loop is one of the famous and most widely used looping statements in ‘C’.
Syntax of the for loop
for is a ‘C’ reserved keyword.
Once you write the for loop you have to create parenthesis, and inside the parenthesis, you write three expressions.
All three expressions are not compulsory, but you can write 3 expressions and those expressions are separated by semicolons. Two semicolons create three blocks for you inside the parenthesis. So, these two semicolons are compulsory, but whether to leave that block empty or filled, that depends upon your logic.
- A for statement has 3 blocks created by 2 semicolons.
- Writing 2 semicolons are mandatory.
- But, you can leave any block empty.
- Even you can leave all blocks empty, which creates an infinite loop.
{ } this is a body of the for loop where you write your statements.
- Block 1 is executed only once, so you should remember this.
- Block 2 (I mean the expression inside block 2) is used for whether to continue with the looping or to break the looping, which means the expression in block 2 is evaluated as true or false. So, this block decides whether to break the loop or continue with the loop.
- Block 3 doesn’t decide anything. In block 3 you can put any expression. So, those are the significance of these blocks.
Now the question is when the expression of block 1 executes, when the expression of block 2 executes, and when the expression of block 3 executes. So, these are executed in a specific order. Let’s understand that.
When a for loop is started for the first time block 1 is executed one time. And after that, block 2 is evaluated. If the evaluation result is true, then only the body of the loop is executed. Suppose, if the evaluation is false, the loop breaks.
So, first block 1 is executed and after that block 2 is evaluated. The block 2 evaluation is true, the loop executes one time and after that block 3 is executed. That means the expression in block 3 is executed. And after that again block 2 is compared. If it is true, again loop is executed, and again block 3 is executed, like that.
It may be a little confusing, but there is a flowchart to understand this better.
Flowchart of the for loop
Flowchart of the for loop is shown in Figure 2.
First, execute block 1 only one time, then check the result of block 2. If it is false, the loop is done, that is loop breaks.
Suppose, if block 2 is true, then execute the body and then execute block 3. And again come back to check the result of block 2. If it is true, again execute the body, after executing the body you should remember that execution of block 3 happens.
This feature of the for loop we can use for iterations. Iterations over a variable, or iterations over an array, so iteration over a pointer array, etc.
We usually use block 1 for initialization, because that is executed only once.
After that the evaluation expression we keep in block 2. For example, i<10 that’s an evaluation. So, that evaluation expression we keep in block 2. And in block 3 we usually increment the iterator. Here, in this case, ‘i’ is an iterator. We use block 3 to increment or decrement the iterator.
Get the Microcontroller Embedded C programming Full Course on Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1