0% found this document useful (0 votes)
34 views3 pages

المستند

The document discusses Java code examples using for loops that were covered in a CSE 142 lecture. It includes methods that print triangle patterns, numbers, squares, a countdown, and patterns combining dots and numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

المستند

The document discusses Java code examples using for loops that were covered in a CSE 142 lecture. It includes methods that print triangle patterns, numbers, squares, a countdown, and patterns combining dots and numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 142 Lecture 3

// Static methods, Data Types, Expressions, Variables

// Some examples using for loops that we looked at in class.


public class LoopExamples {
public static void main(String[] args) {
triangle();
System.out.println();
numTriangle();
System.out.println();
squares();
System.out.println();
blastoff();
System.out.println();
dots();
System.out.println();
dotsNums();
}

// Prints the following output


// *
// **
// ***
// ****
// *****
public static void triangle() {
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= line; j++) {
System.out.print("*");
}
System.out.println();
}
}

// Prints the following output


// 1
// 22
// 333
// 4444
// 55555
public static void numTriangle() {
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= line; j++) {
System.out.print(line);
}
System.out.println();
}
}

// Print the squares of 1 through 6 formatted like


// 1 squared = 1
public static void squares() {
for (int i = 1; i <= 6; i++) {
System.out.println(i + " squared = " + i * i);
}
}

// Prints the following lines.


// T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff!
// The end.
public static void blastoff() {
// We discussed that these expressions would also
work for our loop test
// i > 0
// i != 0
System.out.print("T-minus ");
for (int i = 10 ; i >= 1; i--) {
System.out.print(i + ", ");
}
System.out.println("blastoff!");
System.out.println("The end.");
}

// Prints the following output.


// ....1
// ...2
// ..3
// .4
// 5
public static void dots() {
// line dots line * -1 line * -1 + 5
// 1 4 -1
4
// 2 3 -2
3
// 3 2 -3
2
// 4 1 -4
1
// 5 0 -5
0
for (int line = 1; line <= 5; line++) {
for (int dot = 1; dot <= line * -1 + 5; dot++)
{
System.out.print(".");
}
System.out.println(line);
}
}

// Prints the following output.


// ....1
// ...22
// ..333
// .4444
// 55555
public static void dotsNums() {
for (int line = 1; line <= 5; line++) {
for (int dot = 1; dot <= line * -1 + 5; dot++)
{
System.out.print(".");
}
for (int num = 1; num <= line; num++) {
System.out.print(line);
}
System.out.println();
}
}

// Loops infinitely because we update j but test i


public static void infiniteLoop1() {
for (int i = 1; i <= 5; i++) {
for (int j = 1;
i <= 10;

j++) {
System.out.print("*");
}
System.out.println();
}
}

// Loops infinitely because we update i but test j


public static void infiniteLoop2() {
for (int i = 1; i <= 5; i++) {
for (int j = 1;
j <= 10;

i++) {
System.out.print("*");
}
System.out.println();
}
}
{

You might also like