We can use for loop when we know how many number of
times it should repeat the iterations and also suppose if we want to print the
value from 1 to 100 we can use for loop. Eg:
for(int i=0;i<100;i++){
System.out.println("The value of i:"+i);
}
Here for loop starts with keyword "for"
which is in lowercase followed by 3 statements. Each Statements are separated
by semicolon. The following statements are:
1)Loop Initializer
2)Condition
3)Post Iterative statement
1)Loop Initializer:
This is the first statement where we are using
variable i which is of type int and we have assigned the value 0 . This
statement can be used to initialize the variable. This statement can be
executed only once when the for loop starts.
2)Condition
This is second statement is evaluated to either
true or false. Here this statements compares the value of i. If the value
of i is less than 100 then this statement will be executed. If value of i is
not less than i then statement wouldn't be executed anymore and it will jumps
back to previous statements.
3)Post Iterative Statements
This is the third statement. This statement is
executed after each iteration of for loop. This statements can increment or
decrement the variable value.
Very Informative
ReplyDelete