- Initial value – It will be used to initialize the value that will act as a counter.
- Test value – It is the deciding moment for the ’for loop’. Because it will decide whether to execute the loop or not. If the condition test will fail, then it will not execute any more.
- Increment – The initial value will be incremented after every execution.
- Statements – In the above example, the statements resemble the code that will be executed for the specified number of times.
Now we will see the ease of using for loop. We have just used ‘sharp’ variable that will hold the initial value. Now if the value of sharp is less than 6, then for loop will increment and execute the code. It will repeat the process, until the sharp value will not become 6.
The output will be:
The SharpLesson says 'Hi' 1 times The SharpLesson says 'Hi' 2 times The SharpLesson says 'Hi' 3 times The SharpLesson says 'Hi' 4 times The SharpLesson says 'Hi' 5 times What happened?: The initial value set sharp equal to 1. As long as sharp is less than 6, the loop will continue to run. sharp will increase by 1 each time the loop runs.
We have already explained the example in its result. It means we need not to write any further explanation of the above shown example.