Nested For & Text Patterns
Use nested for loops to print rectangles, right triangles, and small tables. Control rows (outer) and columns (inner).
Nested For & Text Patterns is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Nested Concept
Nested loops: a loop inside another loop. Often, the outer loop controls rows and the inner loop controls columns.
Rectangle 3x4
Rectangle 3x4
public class Main {
public static void main(String[] args) {
// Outer loop controls the number of rows (3 rows total)
for (int r = 1; r <= 3; r = r + 1) {
// Start with an empty string for each row
String line = "";
// Inner loop controls the number of columns (4 stars per row)
for (int c = 1; c <= 4; c = c + 1) {
// Add one star to the line in each iteration
line = line + "*";
}
// Print the completed line of stars
System.out.println(line);
}
}
}
Right Triangle
Right triangle
public class Main {
public static void main(String[] args) {
// Outer loop controls the number of rows (from 1 up to 4)
for (int r = 1; r <= 4; r = r + 1) {
// Start with an empty string for each row
String line = "";
// Inner loop runs 'r' times, so stars increase with each row
for (int c = 1; c <= r; c = c + 1) {
// Add one star to the current line
line = line + "*";
}
// Print the completed line of stars
System.out.println(line);
}
}
}
Right-Aligned
Right-aligned triangle: add spaces before stars.
public class Main {
public static void main(String[] args) {
int height = 4; // total number of rows for the triangle
// Outer loop: controls the row number
for (int r = 1; r <= height; r = r + 1) {
String line = "";
// First inner loop: add spaces so stars are right-aligned
for (int s = 1; s <= (height - r); s = s + 1) {
line = line + " ";
}
// Second inner loop: add stars according to current row
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
// Print the final line (spaces + stars)
System.out.println(line);
}
}
}
1..3 Table
Table 1..3
public class Main {
public static void main(String[] args) {
// Outer loop controls the rows (1 to 3)
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
// Inner loop controls the columns (1 to 3)
for (int c = 1; c <= 3; c = c + 1) {
// Multiply row number (r) by column number (c)
// Add the result plus a space to the line
line = line + (r * c) + " ";
}
// Print the row after building it
System.out.println(line);
}
}
}
Patterns Demo
Run it: Three patterns printed without any console input.
public class Main {
public static void main(String[] args) {
// Rectangle 3x4
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
for (int c = 1; c <= 4; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
// Right triangle
for (int r = 1; r <= 4; r = r + 1) {
String line = "";
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
// Right-aligned triangle
int height = 4;
for (int r = 1; r <= height; r = r + 1) {
String line = "";
for (int s = 1; s <= (height - r); s = s + 1) {
line = line + " ";
}
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
}
}
3x4 Rectangle
Quick check: Which snippet prints a 3x4 rectangle of * (3 rows, 4 columns)?
Recap & Next
Recap: You used nested loops for rectangles, triangles, alignment with spaces, and tiny tables.
Next: Practice and mini-challenges to build confidence with patterns.
Frequently asked questions
Is the “Nested For & Text Patterns” lesson free?
Yes — the full text of “Nested For & Text Patterns” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nested For & Text Patterns”?
Use nested for loops to print rectangles, right triangles, and small tables. Control rows (outer) and columns (inner). You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Nested For & Text Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- For Loop Basics
- Nested For & Text Patterns
- Pattern Practice & Mini-Challenges