- Whenever a set of statements has to be executed multiple times,we will use for statement.
SYNTAX WOULD BE-
for(initialization;condition;updation)
{
//statement block
}
Here,
intialization-Contains declaring and/or initialization of one or more variables,that happens once.
condition-Must be some Boolean expression,that will be checked immediately after initialization and each time when there is an updation of variables.
updation-Contains increment/decrement of variables,that will be executed after executing statement block.
WHY FOR LOOP
A program is execellent if we have less codes to run big program.
So,For some program which has some codes of iteration we need for loop statements.
It is used to reducing coding length.
THINGS I LEARNED
I learned to make some program useing forloop statements.
Here is one of the simple example of forloop which I learned:
class For Loop
{
public static void main(String args[])
{
int x;
for(x=0;,x<5;x=x+1)
System.out.println("This is x: "+x);
}
}
Output will be:
This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4
like this it helps to minimize the lines we use and creates a excellent program.
CONCLUSION
If the number of iteration is fixed, it is recommended to use for loop.
Variables declared in the initialization block should be of the same type. Each variable in the initialization must be of the same datatype.
Variables declared inside the initialization block are accessible only within the loop.
Thats all, use a less number lined program which contains more information in it to be a good programmer.