Flow of For Loop in Java Programming
- For Loop is one of the looping statement in java programming.
- For Loop is used to execute set of statements repeatedly until the condition is true.
- For Loop checks the contrition and executes the set of the statements , It is loop control statement in java.
- For Loop contain the following statements such as “Initialization” , “Condition” and “Increment/Decrement” statement.
Syntax For Loop : Java Programming
for (initialization ; condition ; increment) { Statement1 Statement2 . . StatementN }
Live Example : For Loop Statement
class ForExample { public static void main(String[] args){ for(int i=1; i<10; i++){ System.out.println("Count is : " + i); } } }
Output :
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9
Explanation :
1. Initialization
- Consider above program , in this program we are able to see that for loop statement contain 3 statements first one is “Initialization”.
- ‘i’ is loop control variable used to control the complete loop.
- Its value is initialized with 1.
- Initialization statement gets executed once.
2. Condition
- Inside first step value of i is initialized .
- After initialization the value of control variable is checked against the condition present inside “Condition”.
- i < 11 is true condition so that body part of the loop gets executed and it print the Count.
- As soon as it finishes the execution of the body part it will jump to the “Increment/Decrement” statement.
3. Increment / Decrement
- It will alter the value of Control variable by incrementing it or by decrementing it.
- After alteration it again executes “Condition” of the loop.
- Again if the condition evaluates to be true then it will again executes the body part as long as condition remains true.
Keep in mind :
- Initialization expression initializes the loop
- Initialization expression executed once when the loop begins.
- When the Condition expression evaluates to false, the loop terminates.
- The Increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
Consider Following Program :
class Sample { public static void main(String[] args){ for(int i=1; i ; i++){ // need to use valid condition System.out.println("Count is : " + i); } } }
In Java Integer is not an True Expression :
- It will throw error since In Java we cannot consider Positive integer as “True”.
- We must use comparison operator in order to make expression True.
- We can Use boolean Operators instead.
C:Priteshjava>javac Sample.java Sample.java:3: incompatible types found : int required: boolean for(int i=1; i ; i++){ // need to use valid condition ^ 1 error
Legal and Illegal Ways of For Loop :
Example 1 : Compile Error
public static void main(String[] args){ int exp; for(int i=1; exp ; i++){ // need to use valid condition System.out.println("Count is : " + i); } }
Example 2 : Error Free
public static void main(String[] args){ boolean exp; for(int i=1; exp ; i++){ System.out.println("Count is : " + i); } }
Comments
Post a Comment