0% found this document useful (0 votes)
2 views2 pages

Java For-Loop Code Examples

The document contains a series of 20 for-loop code snippets demonstrating various iterations and operations in Java. Each snippet showcases different loop constructs, including nested loops, conditional statements, and break statements. The examples range from simple iterations to more complex logic involving multiple variables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java For-Loop Code Examples

The document contains a series of 20 for-loop code snippets demonstrating various iterations and operations in Java. Each snippet showcases different loop constructs, including nested loops, conditional statements, and break statements. The examples range from simple iterations to more complex logic involving multiple variables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

For-loop Code Snippets

1)
for (int i = 0; i < 5; i++, [Link]("X")) { [Link](i);
}

2)
for (int i = 1, j = 5; i <= j; i++, j--) { [Link](i * j);
}

3)
for (int i = 10; i > 0 && i % 3 != 0; i--) { [Link](i);
}

4)
for (int x = 0, y = 20; x < y; x += 3, y -= 4) { [Link](x + " " + y);
}

5)
for (int i = 1; i <= 3; i++)
for (int j = 3; j >= 1; j--) [Link](i + j);

6)
for (int i = 0; i < 5; i++); [Link]("Done");

7)
for (int i = 1; i <= 5; i++) if (i % 2 == 0)
[Link](i * 2);

8)
for (int i = 1; i <= 10; i += 3) { [Link](i - 1);
}

9)
for (int i = 5, j = 1; j < i; j += 2, i--) { [Link](i - j);
}

10)
for (int i = 1; i <= 3; i++) {
for (int j = i; j <= 3; j++) { [Link](i * j);
}
}

11)
for (int i = 1; i < 10; i++) { if (i % 4 == 0) break;
[Link](i);
}

12)
for (int i = 0, j = 0; i + j < 5; i++, j += 2) { [Link](i + j);
}

13)
for (long i = 0; i < 3; i++) {
for (long k = 0; k < 1000000; k++); [Link](i);
}
14)
int x = 1;
for (; x <= 5; x++) { [Link](x * x);
}

15)
for (int i = 3; i > 0; i--) {
for (int j = 1; j < i; j++) { [Link](i + j);
}
}

16)
for (int i = 2, j = 8; i <= j && j > 0; i += 2, j -= 3) { [Link](i * 10 + j);
}

17)
for (int i = 1; i <= 4; [Link](i), i++);

18)
int i;
for (i = 1; i * 2 < 15; i++); [Link](i);

19)
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) { [Link](i + j);
}
}

20)
for (int i = 1; ; i++) { if (i == 4) break;
[Link](i);
}

You might also like