-->
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, July 17, 2017

For Loop

For Loop
The for loop is used in java to iterate over a piece of code mutiple times until a particular condition is met.If the number of repetition is fixed then it is strongly suggested that you use for loop.

There are two types of for loops:
i)Simple for loop
ii)Enhanced for loop

Simple for loop
Simple for loop in java is as same as it is in C/C++.It has four parts:-initialization,condition,update statement, and loop body

Syntax of for loop:

    for(initialization;condition;update_statement)
{  
    //code to be executed till the condition is satisfied
    
}  

Example:

    public class ForLoop {  
    public static void main(String[] args) {  
        for(int i=1;i<=10;i++){  
            System.out.println("I Love Java");  
        }  
    }  
    }  

Output:
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java
I Love Java

Enhanced for loop
The enhanced for loop is a special feature which is not found in C/C++. It is used to traverse an array or collection.We don't need to increment value of array index or any collection.

    for(Type variable:data source){  
    //code to be executed  
    }  

Example:


    public class EnhancedFor {  
    public static void main(String[] args) {  
        int data[]={34,213,4,41,4};  
        for(int i:data){  
            System.out.println(i);  
        }  
    }  
    }  

No comments:

Post a Comment