Chapter 9:Loops And Pattern
Table of contents
- Chapter 9: Loops and Pattern Programming
- Introduction:
- 1) Difference between System.out.print vs System.out.println
- 2) Pattern Programming: Printing 5 Stars Vertically
- 3) Loops in Java
- Pattern Printing
- Program to Print a Hollow Square
- Output:
- Explanation of the Code:
- Program to Print the Letter "H"
- Output:
- Explanation of the Code:
- Program to Print the Letter "E"
- Expected Output:
- Explanation of the Code Logic:
- Pattern X
You're right! Let me rework the explanation with a proper introduction, use every line, and provide tables for each loop to illustrate the process. Here's a revised version of Chapter 9: Loops and Pattern Programming:
Chapter 9: Loops and Pattern Programming
Introduction:
In programming, loops allow us to repeat a block of code multiple times until a specified condition is met. This chapter will introduce different types of loops in Java, explain their usage with examples, and also cover pattern programming. We will begin by understanding the difference between System.out.print
and System.out.println
, and then move on to explaining various loop structures with detailed explanations and tables for each.
1) Difference between System.out.print
vs System.out.println
System.out.print
: Prints the content and keeps the cursor on the same line. Any subsequent output will be printed on the same line.Example:
System.out.print("Hello "); System.out.print("World!");
Output:
Hello World!
System.out.println
: Prints the content and moves the cursor to the next line. Each new output will be printed on a new line.Example:
System.out.println("Hello"); System.out.println("World!");
Output:
Hello World!
2) Pattern Programming: Printing 5 Stars Vertically
To print 5 stars, each on a new line, use System.out.println
:
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
Output:
*
*
*
*
*
Table Representation:
Step | Code Execution | Output | Description |
1 | System.out.println("*") | * | Prints * and moves to the next line |
2 | System.out.println("*") | * | Prints * on a new line |
3 | System.out.println("*") | * | Prints * on a new line |
4 | System.out.println("*") | * | Prints * on a new line |
5 | System.out.println("*") | * | Prints * on a new line |
3) Loops in Java
Loops allow us to execute a block of code repeatedly based on a condition. Java provides 4 types of loops:
For Loop
While Loop
Do-While Loop
For-Each Loop (Enhanced for Loop)
i) For Loop
A for loop is used when the number of iterations is known beforehand. It consists of three main parts:
Initialization: Sets the initial value.
Condition: Determines whether to continue the loop.
Update: Modifies the value after each iteration.
Syntax:
for (initialization; condition; update) {
// loop body
}
If the condition is false, the loop exits.
Example: Printing 5 Stars Vertically
for (int i = 0; i < 5; i++) {
System.out.println("*");
}
Table Representation:
Step | Initialization (i = 0 ) | Condition (i < 5 ) | Output | Value of i | Update (i++ ) |
1 | i = 0 | true | * | 1 | i++ (1) |
2 | i = 1 | true | * | 2 | i++ (2) |
3 | i = 2 | true | * | 3 | i++ (3) |
4 | i = 3 | true | * | 4 | i++ (4) |
5 | i = 4 | true | * | 5 | i++ (5) |
6 | i = 5 | false | Exit |
Alternative Example: Print Stars on the Same Line
To print stars on the same line using System.out.print
:
for (int i = 0; i < 5; i++) {
System.out.print("*");
}
// Output: *****
ii) While Loop
The while loop is used when the number of iterations is not known beforehand. It runs as long as the condition is true.
Syntax:
while (condition) {
// loop body
// update
}
Example: Printing 5 Stars Vertically
int i = 0;
while (i < 5) {
System.out.println("*");
i++; // update
}
Table Representation:
Step | Initialization (i = 0 ) | Condition (i < 5 ) | Output | Value of i | Update (i++ ) |
1 | i = 0 | true | * | 1 | i++ (1) |
2 | i = 1 | true | * | 2 | i++ (2) |
3 | i = 2 | true | * | 3 | i++ (3) |
4 | i = 3 | true | * | 4 | i++ (4) |
5 | i = 4 | true | * | 5 | i++ (5) |
6 | i = 5 | false | Exit |
iii) Do-While Loop
The do-while loop executes the loop body at least once, and checks the condition afterward. It guarantees that the code runs once even if the condition is false.
Syntax:
do {
// loop body
// update
} while (condition);
Example: Printing 5 Stars Vertically
int i = 0;
do {
System.out.println("*");
i++; // update
} while (i < 5);
Table Representation:
Step | Initialization (i = 0 ) | Output | Condition (i < 5 ) | Value of i | Update (i++ ) |
1 | i = 0 | * | true | 1 | i++ (1) |
2 | i = 1 | * | true | 2 | i++ (2) |
3 | i = 2 | * | true | 3 | i++ (3) |
4 | i = 3 | * | true | 4 | i++ (4) |
5 | i = 4 | * | true | 5 | i++ (5) |
6 | i = 5 | false | Exit |
iv) Nested Loop
A nested loop is a loop inside another loop. It is often used for printing patterns or iterating over multi-dimensional data.
Syntax:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// inner loop body
}
}
Example: Print a 5x5 Star Pattern
for (int i = 0; i < 5; i++) { // Outer loop
for (int j = 0; j < 5; j++) { // Inner loop
System.out.print("*");
}
System.out.println(); // Move to the next line after inner loop
}
Output:
*****
*****
*****
*****
*****
Table Representation:
Outer Loop (i ) | Inner Loop (j ) | Output |
0 | 0 to 4 | ***** |
1 | 0 to 4 | ***** |
2 | 0 to 4 | ***** |
3 | 0 to 4 | ***** |
4 | 0 to 4 | ***** |
Pattern Printing
Pattern printing is a common exercise in programming that involves using loops to create visually appealing shapes and designs. These patterns help programmers practice their understanding of nested loops and control structures.
Program to Print a Hollow Square
Let's write a Java program to print a hollow square based on a given size, n
. In this case, we will set n = 4
, which means the hollow square will be 4 rows and 4 columns.
Java Code:
public class HollowSquare {
public static void main(String[] args) {
int n = 4; // Size of the square
for (int i = 0; i < n; i++) { // Loop for each row
for (int j = 0; j < n; j++) { // Loop for each column
// Check if we are at the border of the square
if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
System.out.print("*"); // Print '*' for border
} else {
System.out.print(" "); // Print space for hollow part
}
}
System.out.println(); // Move to the next line after each row
}
}
}
Output:
*******
* *
* *
*******
Explanation of the Code:
Initialization:
int n = 4;
— This sets the size of the square.
Outer Loop (
for (int i = 0; i < n; i++)
):- Iterates through each row of the square. The variable
i
represents the current row index.
- Iterates through each row of the square. The variable
Inner Loop (
for (int j = 0; j < n; j++)
):- Iterates through each column of the square. The variable
j
represents the current column index.
- Iterates through each column of the square. The variable
Conditional Check (
if (i == 0 || j == 0 || i == n - 1 || j == n - 1)
):Checks if the current position is on the border of the square:
i == 0
— Top border.j == 0
— Left border.i == n - 1
— Bottom border.j == n - 1
— Right border.
If true, it prints an asterisk (
*
).
Else Statement:
- If the current position is not on the border, it prints a space ( ) to create the hollow effect.
Moving to the Next Line:
- After each row,
System.out.println();
is called to move the cursor to the next line, ensuring that the next row of the square starts on a new line.
- After each row,
Program to Print the Letter "H"
Java Code:
package Patterns;
public class Print_H {
public static void main(String[] args) {
int n = 5; // Height of the letter H
for (int i = 0; i < n; i++) { // Loop for each row
for (int j = 0; j < n; j++) { // Loop for each column
// Check if we are at the borders or the middle row
if (j == 0 || j == n - 1 || i == n / 2) {
System.out.print("*"); // Print '*' for borders and middle line
} else {
System.out.print(" "); // Print space for the hollow part
}
}
System.out.println(); // Move to the next line after each row
}
}
}
Output:
* *
* *
*****
* *
* *
Explanation of the Code:
Outer Loop (
for (int i = 0; i < n; i++)
):- Iterates through each row of the pattern. The variable
i
represents the current row index.
- Iterates through each row of the pattern. The variable
Inner Loop (
for (int j = 0; j < n; j++)
):- Iterates through each column of the pattern. The variable
j
represents the current column index.
- Iterates through each column of the pattern. The variable
Conditional Check (
if (j == 0 || j == n - 1 || i == n / 2)
):Checks if the current position is on the left border (
j == 0
), the right border (j == n - 1
), or the middle row (i == n / 2
):j == 0
— Left vertical line of "H".j == n - 1
— Right vertical line of "H".i == n / 2
— Horizontal line of "H" in the middle.
If true, it prints an asterisk (
*
).
Program to Print the Letter "E"
Java Code:
package Patterns;
public class Print_E {
public static void main(String[] args) {
int n = 9; // Height of the letter E
for (int i = 0; i < n; i++) { // Loop for each row
for (int j = 0; j < n; j++) { // Loop for each column
// Check if we are at the top, middle, bottom, or left side of the letter E
if (i == 0 || i == (n - 1) / 2 || i == n - 1 || j == 0) {
System.out.print("*"); // Print '*' for the borders and middle line
} else {
System.out.print(" "); // Print space for the hollow part
}
}
System.out.println(); // Move to the next line after each row
}
}
}
Expected Output:
*********
*
*
*********
*
*
*********
Explanation of the Code Logic:
Outer Loop (
for (int i = 0; i < n; i++)
):- This loop iterates over each row (0 to 8, making a total of 9 iterations).
Inner Loop (
for (int j = 0; j < n; j++)
):- This loop iterates over each column (0 to 8) within the current row.
Conditional Statements:
if (i == 0 || i == (n - 1) / 2 || i == n - 1 || j == 0)
:The conditions check if:
i == 0
: We're in the first row (top border).i == (n - 1) / 2
: We're in the middle row (fifth row).i == n - 1
: We're in the last row (bottom border).j == 0
: We're in the first column (left vertical line).
If any of these conditions are true, it prints an asterisk (
*
).
Pattern X
Full Code
package Pattern;
public class Print_X {
public static void main(String[] args) {
int n = 9; // Size of the pattern
for (int i = 0; i < n; i++) { // Loop for rows
for (int j = 0; j < n; j++) { // Loop for columns
if (i == j || i + j == n - 1) { // Check if on diagonals
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to the next line after finishing a row
}
}
}
Expected Output
When you run this program, it will produce the following output:
* *
* *
* *
* *
*
* *
* *
* *
* *
Code Explanation:
Outer Loop: The outer loop (
for(int i=0;i<n;i++)
) iterates through the rows.Inner Loop: The inner loop (
for(int j=0;j<n;j++)
) iterates through the columns in each row.Conditions:
if(i==j || i+j==n-1)
: This condition checks if the current position corresponds to one of the diagonals. If true, it prints*
.else
: If the condition is false, it prints a space ( ).