In java or any other programming language use three types of looping methods
1. While loop
2. Do while loop
3. For loop
While loop
- This loop is used for iteration purposes in programming.
- First there should be initialization and then check the condition, if condition is true it can go inside the while loop and execute.
- When condition is false it skips the loop.
Initialization;
While (condition)
{
Body of loop
}
Try these example for understand about while loops.
public class Mango{ public static void main(String args[]){ int x =1; while(x<6){ System.out.println(x); ++x; } } }
Illegal while conditions
There should be a condition in while loop
Ex: while (x==5), while (false), while (x!=3)…etc.
int x=1; While (x) { Body of loop }
int x; While (x=10) { Body of loop }
Legal while conditions
int x; While (x==10) { Body of loop }
int x=10; While (true) { Body of loop }
Do while loop
- This is like a while loop, but different is this is execute at least one time, and then check the condition.
Initialization; do{ Body of loop } While (condition);
Try these example for understand about while loops.
public class Mango{ public static void main(String args[]){ int x =1; do{ System.out.println(x); ++x; }while(x<6); } }
public class Apple{ public static void main(String args[]){ int x=1; while(x<15){ int y=1; while(y<12){ int res = x*y; System.out.println(x+ " * " +y+" = "+ res); y++; } System.out.println("------------------------"); ++x; } } }
For loop
for (initialization; test condition; increment) { Body of the loop }
- This is the structure of for loop, but same thing can be done in several ways. (Look at the examples)
- First you have to declare the variable that you are going to use. This should be the initial state of variable.
- Then semicolon is compulsory.
- After this, condition is there, if condition is true, the body of the loop is executed.
- Then it go to increment process and same thing happen until condition false.
Try this codes to understand about for loop.
public class Apple{ public static void main(String args[]){ for(int x=0; x<5; x++){ for(int y=0; y<=x; y++){ System.out.print("*"); } System.out.print("\n"); } } }
public class Apple{ public static void main(String args[]){ int mark[] = {10,20,30,40,50,60,};//array in java for(int x=0; x<mark.length; x++){ System.out.println(mark[x]); } } }
public class Apple{ public static void main(String args[]){ for(;;){ System.out.println("This is endless for loop"); } } }
public class Apple{ public static void main(String args[]){ int x= 10; for(;x<15;){ System.out.println("This is a valid for loop"); x++; } } }
Continue and break
- Break keyword is used to stop entire loop.
- Continue means it skips the current iteration and execute the program.
public class Apple{ public static void main(String args[]){ for(int x=0; x<=20; x++){ if(x==5){ continue;//this is used to skip a part } if(x==15){ break;//this is used to break the loop } System.out.println(x); } } }
Labeled for loop
- You can label for loops as you want.
- If there are nested loops it is better way to identify each loop separately.
- Before “for” keyword you can type the name of loop and add colon after it.
In this example I have labeled outer and inner loops.
public class Apple{ public static void main(String args[]){ outer: for(int x=0; x<10; x++){ System.out.println(x); inner: for(int y=0; y<10; y++){ System.out.println(y); if(y==5){ continue outer; } } } } }
Enhanced for loop
- This is available after java 5.
- Syntax is there, first you have to use array to declare data.
int x[] = {elements};
for (declaration : expression);
Try these codes to understand about enhanced for loop.
public class Apple{ public static void main(String args[]){ int x[] = {1,2,3,4,5,6}; //this is normal for loop for(int i=0; i<x.length; i++){ System.out.println("Normal for loop: " + x[i]); } //this is enhanced for loop for(int j :x){ System.out.println("Enhanced for loop: " + j); } } }
Loops in Java with examples
Reviewed by Ravi Yasas
on
11:13 PM
Rating:

No comments: