0% found this document useful (0 votes)
16 views

Basic and Advanced Loops in Java

The document provides examples of basic and advanced loops in Java, including for loops, while loops, do-while loops, and enhanced for-each loops. It also mentions infinite loops and demonstrates nested loops. Each loop type is illustrated with code snippets showing their functionality.

Uploaded by

vineeth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Basic and Advanced Loops in Java

The document provides examples of basic and advanced loops in Java, including for loops, while loops, do-while loops, and enhanced for-each loops. It also mentions infinite loops and demonstrates nested loops. Each loop type is illustrated with code snippets showing their functionality.

Uploaded by

vineeth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

* Basic and Advanced Loops in Java

public class LoopExamples {

public static void main(String[] args) {

// 1. Basic Loops

// a) For Loop

for (int i = 0; i < 5; i++) {

System.out.println("For Loop: " + i);

// b) While Loop

int j = 0;

while (j < 5) {

System.out.println("While Loop: " + j);

j++;

// c) Do-While Loop

int k = 0;

do {

System.out.println("Do-While Loop: " + k);

k++;

} while (k < 5);

// 2. Advanced Loops

// a) Enhanced For Loop (For-Each Loop)

int[] numbers = {1, 2, 3, 4, 5};

for (int num : numbers) {

System.out.println("Enhanced For Loop: " + num);


}

// b) Infinite Loop (Use with caution)

/*

while (true) {

System.out.println("Infinite Loop");

*/

// c) Nested Loops

for (int a = 1; a <= 3; a++) {

for (int b = 1; b <= 3; b++) {

System.out.println("Nested Loop: " + a + "," + b);

You might also like