Java For Loop

Java For loop is used to repeatedly execute a particular program code several times.The number of iteration must be fixed. 3 Type of Java ...

Java For loop is used to repeatedly execute a particular program code several times.The number of iteration must be fixed.

3 Type of Java For Loop

  • Simple for loop
  • For -each or Enhanced for loop
  • Labeled for loop

Simple Java For Loop

Java for loop is just same as C/C++.

Syntax:
[for(initialization;condition;increment/decrement){
// execute this code  
}]

Example:
[
public class JavaForLoop {

    /**     * @param args the command line arguments     */    public static void main(String[] args) {
        // TODO code application logic here        for(int i=0;i<=10;i++)
        {

            System.out.println("The number is "+i);
        }



    }

}
]

Java For -each or Enhanced for loop

Java for-each loop is mainly used to iterate through arrays or other Java collections.It works with the used of an index and its able to return values.
Syntax:
[
for(Type variable:array){
//code to be executed          }  
]

Example:
[
public class ForeachLoop {

    public static void main(String args[])
    {
        int score[]={22,11,33,44,55};//initialize array        for(int s:score)
        {
            System.out.println("Score is "+s);
        }

    }

}
]

Java Labeled for loop

As the name suggests, each for loop has name, i.e., labeled. Using break keyword, we are able to break away from the inner loop and continue with outer loop.

Syntax:
[label:
        for(initialization;condition;increment/decrement){
//code to be executed  
}  ]

Example:
[
public class LabeledForLoop {

    public static void main(String args[])
    {
        kk:


        for(int i=1;i<=3;i++){
            dd:
            for(int j=1;j<=3;j++){
                if(i==2&&j==2){
                    break dd;
                }
                System.out.println(i+" "+j);
            }
        }
    }
}  
]

Java infinitive loop

Adding two semicolons in the condition part creates infinitive loop.

Example:

Run below code
[
public class InfinitiveLoop {

    public static void main(String args[])
    {
        for(;;)
        {
            System.out.println("Infinitive loop Example");
        }

    }
}
]

See also:

 Java Switch Statement

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java For Loop
Java For Loop
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-for-loop.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-for-loop.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy