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

Loops Practice PDF

This Java code defines an EmptyBoard class with a main method that can call various loop methods for practice. It includes methods to print a star pattern, multiplication tables from 1 to 3, and other nested for loops that demonstrate logical errors like an infinite loop.

Uploaded by

John Pac
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)
70 views2 pages

Loops Practice PDF

This Java code defines an EmptyBoard class with a main method that can call various loop methods for practice. It includes methods to print a star pattern, multiplication tables from 1 to 3, and other nested for loops that demonstrate logical errors like an infinite loop.

Uploaded by

John Pac
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
You are on page 1/ 2

EmptyBoard.

java

1
2 public class EmptyBoard {
3
4 public static void main(String[] args) {
5 // Starz();
6 // Multiply();
7 // loopsample6();
8
9 }
10
11
12 // Loop -- Practice #2
13 // Write a program that prints multiplication of numbers between 1 to 3
14
15 public static void Multiply() {
16 for (int i = 1; i<=3; i++) {
17 for (int j = 1; j <= 3; j++) {
18 System.out.println(i + " x " + j + " = " + (i*j) );
19 }}
20 }
21
22 // Loop -- Practice #1
23 public static void Starz() {
24 for (int i = 1; i <= 6; i++) {
25 for (int j = 1; j <= 3; j++) {
26 System.out.print("*");
27 System.out.print("\\ /");
28 System.out.print("/ \\");
29 }
30 System.out.println(); // to end the line
31 }}
32 // Loop -- Practice #3
33 public static void loopsample3() {
34 for (int i = 1; i <= 10; i++) {
35 // every time you re-enter the nested loop below after exiting it
(to go back to test-case i above) j is initialized back to 1
36 for (int j = 1; j <= i/5; j++) {
37 System.out.println("i= " + i + " j= " + j);
38 }
39 }
40 }
41
42 // Loop -- Practice #4
43 public static void loopsample4() {
44 for (int i = 1; i <= 3; i++) {
45 System.out.println("outer loop i=" + i);
46 for (int j = i; j <= 3; j++) {
47 System.out.println("\ti= " + i + " j= " + j);

Page 1
EmptyBoard.java

48 }
49 }}
50 // Loop -- Practice #5
51 public static void loopsample5() {
52 for (int i = 1; i <= 5; i++) {
53 for (int j = 1; j <= i; j++) {
54 System.out.print("*");
55 }
56 System.out.println();
57 }
58 }
59 // Loop -- Practice #6
60 public static void loopsample6() {
61 for (int i = 1; i <= 5; i++) {
62 // with i <=10 in the loop below, the i value NEVER gets updated
--> infinite loop -- also this is a Logical error and NOT a syntax error.
So the code will run
63 for (int j = 1; i <= 10; j++) {
64 System.out.print("*");
65 }
66 System.out.println();
67 }}
68
69
70 }
71
72

Page 2

You might also like