![]() |
| Loops in Java Programming | Beginner to Advanced Guide |
📑 Table of Contents ▼
Loops Definition in Java
Java Loops: Loop is very important part of every programming languages like C, C++, Java, Python and more computer programming languages. Loop is part of control flow statements. To run the particular block of code continuously until a required condition is fulfil is called looping operations. Loop can be used when there is a need to execute a part of program execute multiple times.
- Loop is a control of variable value is called loop counter.
- Loop control variable must be initialized in other word starting value is must.
- The increment/ decrement of the control variable which is modified each time the iteration of loop.
- Loop condition is true than execute continue but condition is false than loop will be break.
Types of Loops in Java Programming
- While Loop in Java
- Do While Loop in Java
- For Loop in Java
- Foreach Loop in Java
While Loop Definition in Java
While Loop: A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. While loop before value initialization starting value and the given condition. If you are not follow while loop syntax your loop statement are infinite executed.
Syntax of While Loop in Java
{ body Statements; increment/decrement; } |
Explain While Loop Syntax
while(n<=10) { System.out.println("Number is:"+n); i++; } |
Example of While Loop in Java
{ int i=1; while(i<=5) { System.out.println("Your Number is:"+i); i++; } } } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Print the Table of Any Number Using While Loop
public class while_loops{ { int n; int i=1; Scanner s=new Scanner(System.in); System.out.print("Enter Aby Number:"); n=s.nextInt(); while(i<=10) { System.out.println("Table is:"+n+"X"+i+"="+n*i); i++; } } } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
Do While Loop Definition in Java
Do-While Loop: A do-while loop is used to execute a block of code as long as the specified condition is true. The do-while loop in java is used to repeatedly execute a block of code execute at least one time and then continue executing it as long as a specified condition remains true.
The difference between while loop and do-while loop is that in case of while loop if the condition is false than while body will be not executed but do-while loop either condition true or false than do-while loop body at least one time executed.
Do While Loop Syntax in Java
{ body Statements; increment/decrement; while(condition); } |
Explain Do-While Loop Syntax
do { System.out.println("Your Number is:"+i); i++; while(i<=10); } |
Example of Do While Loop in Java
{ int i=1; do { System.out.println("Your Number is:"+i); i++; } while(i>=5); } } ============OUTPUT============ Your Number is:1 Note: Here condition is false but do-while loop at least one time execute. |
Perfect Do While Loop Example in Java
{ int i=1; do { System.out.println("Your Number is:"+i); i++; } while(i<=5); } } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Print the Table of Any Number Using Do While Loop
public class do_while_loops{ { int n; int i=1; Scanner s=new Scanner(System.in); System.out.print("Enter Aby Number:"); n=s.nextInt(); do { System.out.println("Table is:"+n+"X"+i+"="+n*i); i++; } while(i<=10); } } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
For Loop Definition in Java
For Loop: A for loop is used to execute a block of code as long as the specified condition is true. A for loop in java is used to execute a block of code a specific number of times. For loop there are three main part in c like initialization, condition and increment /decrement. initialization part executes only one at a time.
Syntax of For Loop in Java
{ body Statements; } |
Explain For Loop Syntax
{ System.out.println("Your Number is:"+i); } |
Example of For Loop in Java
{ for(int i=1;i<=5;i++) { System.out.println("Your Number is:"+i); } } } ============OUTPUT============ Your Number is:1 Your Number is:2 Your Number is:3 Your Number is:4 Your Number is:5 |
Print Table of Any Number in Java
public class for_loops{ { int n; System.out.print("Enter Aby Number:"); n=s.nextInt(); for(int i=1;i<=10;i++) { System.out.println("Table is:"+n+"X"+i+"="+n*i); } } } ============OUTPUT============ Enter Any number:6 Table is:6 X 1=6 Table is:6 X 2=12 Table is:6 X 3=18 Table is:6 X 4=24 Table is:6 X 5=30 Table is:6 X 6=36 Table is:6 X 7=42 Table is:6 X 8=48 Table is:6 X 9=54 Table is:6 X 10=60 |
Define For Each Loop in Java
Foreach Loop: A Foreach loop is a special type of loop it can be used mostly with array. It stores each element of array in a variable and executes the body of loop. It starts with for keyword like a normal for loop.
For Each Loop Syntax in Java
{ Statements; } |
Foreach Loop Example in Java
{ int a[]={12,24,56,67,80}; for(int data:a) System.out.println("Foreach Loop Data Element:"+data); } } ============OUTPUT============ Foreach Loop Data Element:12 Foreach Loop Data Element:24 Foreach Loop Data Element:56 Foreach Loop Data Element:67 Foreach Loop Data Element:80 |
Define Foreach Loop with for Loop
{ int a[]={12,24,56,67,80}; for(int i=0;i<a.length;i++) System.out.println("For Loop Data:"+a[i]); } } ============OUTPUT============ For Loop Data Element:12 For Loop Data Element:24 For Loop Data Element:56 For Loop Data Element:67 For Loop Data Element:80 |
Conclusion of Loops in Java
Loop Conclusion: A loops in a main part of every programming languages because loop concept are perform different kind operation like fetch data, print multiple information and more operations. I can explain every loop in this article if your any question or problems than you can contact me by email. I can every solution loop related in next coming soon article.

