For-loop-in-Java .

In this session i have discussed for loop in Java .


The for loop also a entry control loop in Java .

For Loop Syntax :

    for(initialization ;test-condition;increment/decrement){
    
        loop body
    }
    

initialization :

In the for loop first initialize the variable like i=1 etc . This is called the control variable .

test-condition :

The next is test the condition . In which the value of variable is evaluated with specified condition . The condition may be relational expression . The loop will continue if condition is true other wise loop will be terminated .

increment/decrement :

In the segment the value of variable will be increment or decrement according to your need .


Example : Write a java program that print the value 1-10 using for loop .

 class Hello{ 
         
     public static void main(String args[]){  
      
         for(int i=1;i<=10;i++){

              System.out.print(i);
              System.out.print(" ");
         }
     }
 }
   OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a java program that print the table of 2 using for loop .

class Hello{ 
         
     public static void main(String args[]){  
      
            int t;
            for (int i=1;i<=10;i++){
            
                t = i * 2;   
                System.out.print(t);
                System.out.print(" ");
            }
     }      
}    
    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

Example : Write a java program that print the even number using for loop .

class Hello{ 
         
     public static void main(String args[]){  
       
           for (int i=1;i<=10 ; i++){

                 if (i % 2 == 0){
                 
                    System.out.print(i);
                    System.out.print(" ");
                 }
           }
     }
}         
    
    OUTPUT : 2 4 6 8 10
    

Example : Write a program that print the odd number using for loop .

class Hello{ 
         
     public static void main(String args[]){  
           
           for (int i=1;i<=10 ; i++){
           
                 if (i % 2 != 0){
                 
                    System.out.print(i);
                    System.out.print(" ");
                 }
           }
     }
}       
    
    OUTPUT : 1 3 5 7 9
    

Example : Write a java program that print the following triangle using for loop .

class Hello{ 
         
     public static void main(String args[]){  
            
            for (int i=1;i<=5 ; i++){
           
                for (int j = 1; j <= i;j++ ){
                
                    System.out.print("*");
                }
                System.out.println();
            }
     }
}         
    
     OUTPUT :   *
                * *
                * * *
                * * * *
                * * * * *
    

Example : Write a java program that print the following number triangle using for loop .

class Hello{ 
         
     public static void main(String args[]){  
       
            for (int i=1;i<=5 ; i++){
            
                for (int j = 1; j <= i;j++ ){
                
                    System.out.print(j);
                }
                System.out.println();
            }
     }
}         
    
     OUTPUT :   1
                1 2
                1 2 3
                1 2 3 4
                1 2 3 4 5
    

Example : Write a java program that print the following number triangle using for loop .

 class Hello{ 
         
     public static void main(String args[]){  
       
           for (int i=1;i<=5 ; i++){
            
                for (int j = 1; j <= i;j++ ){
                
                    System.out.print(i);
                }
                System.out.println();
            }
     }
 }        
    
     OUTPUT :   1
                2 2
                3 3 3
                4 4 4 4
                5 5 5 5 5
    

Example : Write a java program that print the following triangle using for loop .

 class Hello{ 
         
     public static void main(String args[]){  
       
            int k = 4;
            for (int i=1;i<=5 ; i++){
            
                for (int space = k; space > 0; space--){
                
                    System.out.print(" ");
                }
                    for (int j = 1; j <= i; j++){
                    
                        System.out.print("*");
                    }
                    k--;
                    System.out.println();
            }
     }
 }        
    
     OUTPUT :        *
                   * * 
                 * * * 
               * * * * 
             * * * * *
    

Example : Write a java program that print the following number triangle using for loop .

 class Hello{ 
         
     public static void main(String args[]){  
 
            int k = 4;
            int m = 1;
            for (int i=1;i<=4 ; i++){
            
                for (int space = k; space > 0; space--){
                
                    System.out.print(" ");
                }
                for (int j = 1; j <=i; j++)
                {
                    System.out.print(m);
                    m++;
                }
                 k--;
                 System.out.println();
             }
     }
 }        
    
     OUTPUT :          1
                     2 3
                   4 5 6
                 7 8 9 10
    

Example : Write a java program that print the following number triangle using for loop .

 class Hello{ 
         
     public static void main(String args[]){  
 
            int k = 3;
            int m = 1;
            for (int i=1;i<=4 ; i++){
            
                for (int space = k; space > 0; space--){
                
                  System.out.print(" ");
                }
                for (int j = 1; j <=m; j++){
                
                   System.out.print("*");
                }
                k--;
                m = m + 2;
                System.out.println();
             }
       }
 }       
    
     OUTPUT :        *
                   * * *
                 * * * * *
               * * * * * * *
    

Welcome to AndroidATPoint.

back image                     next image