For loop

We use for loop, when we know how much it should be repeat the iteration. It is very useful at that time when we fully agree about its limit. Because sometimes we want to repeat an iteration to given conditions, as we want to write a line just 1000 times. So the for loop is very useful for it to write it from 1 till 1000. Let’s check it in listing 2 for details how we can use it, in java Programming.
Listing 2: Use of For loop in Java Programming

 public class ForLoop{
  public static void main(String args[]){
   //printing in ascending order
   //from 1 to 10
   //using increment operator
   for(int i=1; i<=10; i++){
    System.out.print(i+" ");
   }
   System.out.println();
   //printing in descending order
   //from 10 to 1
   //using decrement operator
   for(int i=10; i>=1; i--){
    System.out.print(i+" ");
   }
  }
 }
In this listing 2 example there is a simple program. I gave the class name as ForLoop and in the main method I used for loop to times. In the first loop, it is increasing the iteration from 1 to 10 and in the second loop it is decreasing the iteration from 10 to 1. As I told you in the syntax we can use increment and decrement operators in for loop.

Comments