0% found this document useful (0 votes)
3 views1 page

Loop

The document contains multiple Java code snippets demonstrating different types of loops: a for loop with a break statement, a while loop, a do-while loop, nested for loops, and a for-each loop. Each snippet illustrates how to iterate through numbers or arrays and print values. The examples highlight the syntax and functionality of each loop type in Java.

Uploaded by

nagabhushansv2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Loop

The document contains multiple Java code snippets demonstrating different types of loops: a for loop with a break statement, a while loop, a do-while loop, nested for loops, and a for-each loop. Each snippet illustrates how to iterate through numbers or arrays and print values. The examples highlight the syntax and functionality of each loop type in Java.

Uploaded by

nagabhushansv2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Main {

public static void main(String[] args) {


for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
}
}
----------------------
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
------------------------
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
--------------
public class Main {
public static void main(String[] args) {
// Outer loop.
for (int i = 1; i <= 2; i++) {
System.out.println("Outer: " + i); // Executes 2 times

// Inner loop
for (int j = 1; j <= 3; j++) {
System.out.println(" Inner: " + j); // Executes 6 times (2 * 3)
}
}
}
}
-----------------
class ForEachExample1{
public static void main(String args[]){
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
--------------

You might also like