0% found this document useful (0 votes)
3 views6 pages

4.4. Nested Loops — Snefru_ Learning Programming With C

The document explains nested loops in C programming, illustrating how to print patterns using two loops: one for lines and another for stars. It provides exercises to print a simple star pattern and a right-aligned star pattern based on user input, detailing the steps to decompose the problem and the corresponding code. Additionally, it addresses common mistakes and debugging tips for achieving the desired output.

Uploaded by

FATURRAHMAN 98
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)
3 views6 pages

4.4. Nested Loops — Snefru_ Learning Programming With C

The document explains nested loops in C programming, illustrating how to print patterns using two loops: one for lines and another for stars. It provides exercises to print a simple star pattern and a right-aligned star pattern based on user input, detailing the steps to decompose the problem and the corresponding code. Additionally, it addresses common mistakes and debugging tips for achieving the desired output.

Uploaded by

FATURRAHMAN 98
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/ 6

6/16/25, 8:57 PM 4.4.

Nested loops — Snefru: Learning Programming with C

Nested loops
Contents
4.4.1. Print a 2 dimensional pattern

4.4.2. Let’s tweak the pattern a little!

Just like how if-statements can be nested, loops can be nested too. Nested loops would be
necessary if we decompose a problem into multiple parts, and each part has its own repetition.

For example, in this next exercise, we print multiple lines where each line has different repetition
pattern.

4.4.1. Print a 2 dimensional pattern


Exercise

Write a C program that prints the following pattern:

*
**
***
****
*****

Step 1: Toy example. The expected output is shown above. But a more simplified version of the
output is printing only three line as follows.

*
**
***

Step 2: Think of a solution! There are two sources of repetition. The first source of repetition is
the number of lines, i.e., we are printing three lines in the toy example. The second source of
repetition is the number of stars in each line. In the first line, we are printing 1 *, in the second
line, we are printing 2 *s and so on.

We can use a loop to repeat the action of printing lines. For each line, we can use another loop to
repeat the action of printing stars. In every line, we can print out the number of stars that is
equal to the line number.

Step 3: Decompose into steps. We can decompose the problem into two parts. The first part is
in each line, we print out the number of stars that is equal to the line number. The second part is
to print out all the number of lines. In other words, we need a loop that repeats printing stars,
and another loop that repeats printing lines. Printing lines can be decomposed into the following
steps:

1. Initialize a variable line to 1.


2. Print out stars equal to the line number.
3. Increment the variable line by 1.
4. Repeat 2 and 3 until the variable line is greater than number of lines to be printed, e.g. 3.

The second step “2. Print out stars equal to the line number.” can be decomposed into the
following steps:

1. Initialize a variable star to 1.


2. Print out a *.
3. Increment the value of star by 1.
4. Repeat 2 and 3 until the value of star is greater than the value line.

Step 4: Write the code. The code is shown below. Download print-stars-pattern.c if you want
to run the program yourself. Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 1/6
6/16/25, 8:57 PM 4.4. Nested loops — Snefru: Learning Programming with C

C run copy

1 #include <stdio.h>
2
3 int main(void) {
4 for (int line = 1; line <= 3; line += 1) { // loop over lines
5 for (int star = 1; star <= line;
6 star += 1) { // loop over stars in a single line
7 printf("*");
8 }
9 printf("\n");
10 }
11 return 0;
12 }

Input Output (example input)

*
**
***

Step 5: Test the code. Try changing 3 to 5 and see what happens. It should print the following
pattern.

*
**
***
****
*****

Step 6: Debug the code. It is possible that you do not get the expected output. Some common
mistakes are:

1. Misplacing the printf("\n") statement to be in the inner loop. This will print a new line for
every *. If you did so, the output will look like this:
Misplacing printf("\n") in the inner loop.

Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 2/6
6/16/25, 8:57 PM 4.4. Nested loops — Snefru: Learning Programming with C

C run copy

1 #include <stdio.h>
2
3 int main(void) {
4 for (int line = 1; line <= 3; line += 1) { // loop over lines
5 for (int star = 1; star <= line;
6 star += 1) { // loop over stars in a single line
7 printf("*");
8 printf("\n"); // misplaced
9 }
10 }
11 return 0;
12 }

Input Output (example input)

*
*
*
*
*
*

Undesired Output

*
*
*
*
*
*

It should be placed after the inner loop only when one line is printed out.
2. Some people prefer to start line and star from 0, instead of 1. This is perfectly fine, unless
you are printing out the number of stars that is equal to the line number. In that case, you
need to change the condition in the outer loop to line < 3 or line <= 2. There is no need to
change the condition in the inner loop. This is because line is now already reduced by 1
since it starts from 0.
Starting line and star from 0.

Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 3/6
6/16/25, 8:57 PM 4.4. Nested loops — Snefru: Learning Programming with C

C run copy

1 #include <stdio.h>
2
3 int main(void) {
4 for (int line = 0; line < 3; line += 1) { // loop over lines
5 for (int star = 0; star <= line;
6 star += 1) { // loop over stars in a single line
7 printf("*");
8 }
9 printf("\n");
10 }
11 return 0;
12 }

Input Output (example input)

5 *
**
***

4.4.2. Let’s tweak the pattern a little!


Exercise

Write a C program that prints the following pattern with n rows. n would be taken as input from
the user. In the following pattern, n is set to 5.

*
**
***
****
*****

Step 1: Toy example. The expected output is shown above. We can use it as a toy example to
develop our solution.

Step 2: Think of a solution! Again, here we need an outer loop for the number of rows to print,
and an inner loop for the number of spaces and number of stars to print in each row.

In the following figure, we can see a pattern in the number of spaces and number of stars in
each line. The number of spaces is equal to n - row number and the number of stars is equal to
row number.

We will use a loop to print out each row. For each row, we will use another loop to print out the
number of spaces and number of stars. In every row, we can print out the number of spaces that
is equal to n - row number. Then, we can print the number of stars that is equal to row number.

Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 4/6
6/16/25, 8:57 PM 4.4. Nested loops — Snefru: Learning Programming with C

Fig. 4.13 Pattern of stars/asterisks

Step 3: Decompose into steps. We can decompose the problem into two parts. The first part is
to print out the number of rows. The second part is to print out the number of spaces and
number of stars in each line. The second part can be decomposed into the following steps:

1. Initialize a variable row to 1.


2. Print out the number of spaces that is equal to n - row number and stars that is equal to row
number.
3. Increment the variable row by 1.
4. Repeat 2 and 3 until the variable row is greater than n for the toy example.

The second step “2. Print out the number of spaces that is equal to n - row number and stars
that is equal to row number” can be decomposed into the following steps:

1. Initialize a variable column to 1.


2. Print a space if the value of column is less than or equal to n - row number.
3. Otherwise, print out a *.
4. Increment the variable column by 1.
5. Repeat 2 to 4 until the variable column is greater than n.

Step 4: Write the code. The code is shown below. Download reverse-stars-pattern.c if you
want to run the program yourself.

Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 5/6
6/16/25, 8:57 PM 4.4. Nested loops — Snefru: Learning Programming with C

C run copy

1 #include <stdio.h>
2
3 int main(void) {
4 int n = 0;
5 printf("Enter the number of rows:");
6 scanf("%d", &n);
7 for (int row = 1; row <= n; row += 1) {
8 for (int col = 1; col <= n; col += 1) {
9 if (col <= n - row) {
10 printf(" ");
11 } else {
12 printf("*");
13 }
14 }
15 printf("\n");
16 }
17 return 0;
18 }

Input Output (example input)

5 Enter the number of rows:5


*
**
***
****
*****

Step 5: Test the code. Test the code with corner numbers that may break your code. For
example, try changing 5 to 1 and see what happens. It should print only one *. Try changing 5 to
0 and see what happens. It should print nothing.

Step 6: Debug the code. It is possible that you do not get the expected output. Some common
mistakes in printing patterns were discussed in the previous exercise.

1 Question
Quiz
Start Quiz

By Salma Emara et al.


© Copyright 2022–2025.

Snefru Chatbot +
+

https://learningc.org/chapters/chapter04-loops/nested-loops 6/6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy