KatsolAgency

Programming languages and web development including java , html, css ,java script,Tutorials Android phone tricks and Tech news around the world.

Saturday, July 29, 2017

For loop in Java with example

For loop in Java with example

forloop by katsolagency
By Iorkua Daniel | For loop in Java with example


In Java we have three types of basic loops: for, while & do-while. In this article we are going to discuss about for loop. We will cover following topics in this article:

  1.  What is a for loop?
  2. syntax of for loop
  3. example of for loop
  4. for loop flow diagram
  5. infinite


What is For loop?

It executes a block of statements repeatedly until the specified condition returns false.
Syntax of for loop:

The syntax of for loop looks like the code below;
(initialization; condition; increment/decrement) {
    statement(s) //block of statements
}

Note: Mind the semicolon (;) after initialization and condition in the above syntax.
Initialization expression executes only once during the beginning of loop
Condition(Boolean Expression) gets evaluated each time the 
loop iterates. Loop executes the block of statement repeatedly until this condition returns false.
Increment/Decrement It executes after each iteration of loop.

LOLz  Confused⧭⧭??
There is not get confused of , 
Don’t worry let see an example to understand it better.
For loop example:
class ForLoopExample {
    public static void main(String args[]){
         for(int i=10; i>1; i--){
              System.out.println("The value of i is: "+i);
         }
    }
}

This is how  here the output of this program will look:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2

Explanation of  the above program:
int i=1 is initialization expression
i>1 is condition(Boolean expression)
i– Decrement operation
Flow diagram of for loop

Infinite for loop:

 The importance of Boolean expression and increment/decrement operation co-ordination:

class ForLoopExample2 {
    public static void main(String args[]){
         for(int i=1; i>=1; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}
The above program is almost the same with the first one, but here we  are going to Increment   i++ 
i-- as of the first one
This is an infinite loop as the condition would never return false.
The initialization step is setting up the value of variable i to 1,
since we are incrementing the value of i, it would always be greater
 than 1 (the Boolean expression: i>1) so it would never return false.
  This would eventually lead to the infinite loop condition. Thus it
  is important to see the co-ordination among Boolean expression and
  increment/decrement to determine whether the loop would terminate at
   some point of time or not.

Here is another example of infinite for loop:

// infinite loop
for ( ; ; ) {
    // statement(s)

}

For loop example to iterate an array:
Here we are iterating and displaying array elements using the for loop.

class ForLoopExample3 {
    public static void main(String args[]){
         int arr[4]={2,11,45,9};
         //i starts with 0 as array index starts with 0 too
         for(int i=0; i<4; i++){
              System.out.println(arr[i]);
         }
    }
}


Output:
2
11
45
9

    Choose :
  • OR
  • To comment
No comments:
Write comments