C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Loops in JavaThe Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop. There are three types of for loops in Java.
Java Simple for LoopA simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
Syntax: for(initialization; condition; increment/decrement){ //statement or code to be executed } Flowchart: Example: ForExample.java //Java Program to demonstrate the example of for loop //which prints table of 1 public class ForExample { public static void main(String[] args) { //Code of Java for loop for(int i=1;i<=10;i++){ System.out.println(i); } } } Output: 1 2 3 4 5 6 7 8 9 10 Java Nested for LoopIf we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes. Example: NestedForExample.java public class NestedForExample { public static void main(String[] args) { //loop of i for(int i=1;i<=3;i++){ //loop of j for(int j=1;j<=3;j++){ System.out.println(i+" "+j); }//end of i }//end of j } } Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Pyramid Example 1: PyramidExample.java public class PyramidExample { 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();//new line } } } Output: * * * * * * * * * * * * * * * Pyramid Example 2: PyramidExample2.java public class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){ System.out.print("* "); } System.out.println();//new line } } } Output: * * * * * * * * * * * * * * * * * * * * * Java for-each LoopThe for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation. It works on the basis of elements and not the index. It returns element one by one in the defined variable. Syntax: for(data_type variable : array_name){ //code to be executed } Example: ForEachExample.java //Java For-each loop example which prints the //elements of the array public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } } Output: 12 23 44 56 78 Java Labeled For LoopWe can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop. Note: The break and continue keywords breaks or continues the innermost for loop respectively.Syntax: labelname: for(initialization; condition; increment/decrement){ //code to be executed } Example: LabeledForExample.java //A Java program to demonstrate the use of labeled for loop public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ break aa; } System.out.println(i+" "+j); } } } } Output: 1 1 1 2 1 3 2 1 If you use break bb;, it will break inner loop only which is the default behaviour of any loop. LabeledForExample2.java public class LabeledForExample2 { public static void main(String[] args) { aa: for(int i=1;i<=3;i++){ bb: for(int j=1;j<=3;j++){ if(i==2&&j==2){ break bb; } System.out.println(i+" "+j); } } } } Output: 1 1 1 2 1 3 2 1 3 1 3 2 3 3 Java Infinitive for LoopIf you use two semicolons ;; in the for loop, it will be infinitive for loop. Syntax: for(;;){ //code to be executed } Example: ForExample.java //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println("infinitive loop"); } } } Output: infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c Now, you need to press ctrl+c to exit from the program. Java for Loop vs while Loop vs do-while Loop
Next TopicJava While Loop
|