🌐
W3Schools
w3schools.com › cpp › cpp_for_loop_nested.asp
C++ Nested Loops
C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Code Challenges C++ Syllabus C++ Study Plan C++ Certificate ... It is also possible to place a loop inside another loop. This is called a nested loop.
🌐
Reddit
reddit.com › r/computerscience › explaining "nested loops" to someone without a computer's background
r/computerscience on Reddit: Explaining "Nested Loops" to Someone Without a Computer's Background
May 29, 2022 -

Does anyone know of a good example to explain Nested Loops to someone without a Computer's Background? I was thinking of an example where someone makes a checklist/decision tree for picking an ideal watermelon at a grocery store.

For example:

- Make sure the watermelon weighs more than 1 KG

- If YES, Make sure the watermelon is ripe

- If YES, Make sure the watermelon has no blemishes and dents

- If YES, Make sure the watermelon costs less than $10

- If YES, then buy.

Is this a good example of a Nested Loop - can someone please comment on this?

Thanks!

Discussions

python - What is a nested loop and how do I use it in example below? - Stack Overflow
I was given the following problem for my beginning python coding class. I have reached out to the professor, but he hasn't gotten back to me and this is due in 3.5 hours. If someone has a tutorial/ More on stackoverflow.com
🌐 stackoverflow.com
Can someone please explain nested loops / loops in general, like I'm five? (C++)
Loops like you're 5: A loop is a way to tell the computer to do something specific repeatedly. For instance, if you were a (human) computer and I told you to walk to the end of the street and back until you have done so three times; this is a loop. It's a specific instruction done repeatedly. Loops don't always have an end. If I told you (as a computer) to walk to the end of the street and back until I tell you to stop; if suddenly I decide to move to France while you're walking back and forth, this is an example of a potential infinite loop. I may come back to visit and tell you to stop. You may keep going until you keel over. Nested loops like you're 5: If a loop is a set of specific instructions done repeatedly, what happens if you put a loop in a loop? Well, one of those loops has to finish entirely before the other can continue again. If I told you to walk down to the end of the street 3 times and back, but when you get halfway back, stop and touch your nose 5 times before you start again; this is a nested loop. You may say to yourself "but wait, I can touch my nose while I'm walking, why do I need to stop?" and you would be right, but what we need to realize about computers is that because of how they work, they can only do one thing at a time. (see below before any objections) Computers work in steps. Do A, then do B, then do C. Loops allow us to say "but do X over and over again." You may then say "but how come I can browse reddit, listen to music, and download stuff at the same time on my computer?" and this moves into the concepts of concurrency, or in simpler terms, making the computer do lots of different things at the same time. Things like this exist because we may want a loop to be indefinite, or to end when we tell it to end. Lots of things in the computer world depend on infinite loops, including browsing Reddit (for server side web services, some web browsing features) and video games (rendering and game logic loops). This is where concurrency and asynchronous operations come into play, but this is beginning to get out of the scope of the question, so I'll stop it here (unless you're interested). tl;dr More on reddit.com
🌐 r/learnprogramming
17
4
March 20, 2014
How to be decent at nested for loops?

Practice more.

Draw the patterns on paper first.

More on reddit.com
🌐 r/learnprogramming
2
1
January 11, 2022
Need help understanding nested for loops?

What exactly don't you understand?

You should walk through the algorithm manually. Write down what you think the output would look like, then actually run that little program. If they are different, use a debugger and step through the code. You can then follow and see what each value is and how the variables change as the program runs. That way you can also understand the exact time that i and j increment, which is important. there is an order of operations, i'm not sure what it is for c++ for sure so I would just run it or look at documentation to see exactly when variables change.

More on reddit.com
🌐 r/learnprogramming
5
2
March 20, 2013
🌐
Programiz
programiz.com › java-programming › nested-loop
Nested Loop in Java (With Examples)
In the above example, the outer loop iterates 3 times and prints 3 weeks. And, the inner loop iterates 7 times and prints the 7 days. We can also create nested loops with while and do...while in a similar way.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › nested-loops-in-c-with-examples
Nested Loops in C - GeeksforGeeks
If the outer loop is running from i = 1 to 5 and the inner loop is running from j = 0 to 3. Then, for each value of the outer loop variable (i), the inner loop will run from j = 0 to 3. Nested for loop refers to any type of loop that is defined ...
Published   November 7, 2025
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
May 21, 2025 - Next, you’ll take a look at how you can use nested loops to create pairwise combinations. ... When building programs, you may have to work with datasets that require you to make comparisons or work with data in pairs. In the example below, you’ll find a program that generates all possible matchups between players in a friendly game of space ball:
🌐
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - In this example, we are printing a multiplication table of the first ten numbers. The outer for loop uses the range() function to iterate over the first ten numbers · The inner for loop will execute ten times for each outer number · In the ...
🌐
Oregonstate
eecs.engineering.oregonstate.edu › ecampus-video › CS161 › template › chapter_5 › nested.html
Chapter 5: Nested loops, Which loop to use?
Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop. For example, you read a file line by line and for each line you must count how many times the word “the” is found.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit4-Iteration › topic-4-4-nested-loops.html
4.4. Nested For Loops — CSAwesome v1
Add an if/else statement that changes ... have no remainder when divided by 2. Be creative and design a unique snowflake! Use nested for-loops to have the turtle draw a snowflake of polygons....
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_for_loop_nested.asp
Java Nested Loops
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... It is also possible to place a loop inside another loop. This is called a nested loop.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_nested_loops.htm
Nested Loops in C
That means you can put a while loop inside a for loop, a for loop inside a do-while loop, or any other combination. The general behaviour of nested loops is that, for each iteration of the outer loop, the inner loop completes all the iterations.
🌐
Programiz
programiz.com › cpp-programming › nested-loops
C++ Nested Loop (With Examples)
To achieve this, we can create a loop to iterate three times (3 weeks). And inside the loop, we can create another loop to iterate 7 times (7 days). This is how we can use nested loops.
🌐
The Linux Documentation Project
tldp.org › LDP › abs › html › nestedloops.html
Nested Loops
November 5, 2025 - Nested Loop · See Example 27-11 for an illustration of nested while loops, and Example 27-13 to see a while loop nested inside an until loop.
Top answer
1 of 3
1

Solution

Let's break the problem down into steps.

  1. First, we need to get an integer from the input, and make sure that the integer is either 3, 10, or any number in between. That's what 3 to 10 inclusive means.

  2. We then need to ask a user for a single character that will be used to print a box.

  3. After we've gotten the integer, and the character, we need to display a box made up of the character whose width and height are determined by the integer.

For step one, we need to get input from the user. This can be done using the builtin function input(). It takes an optionally prompt as an argument, and returns the input as a string. So let's write that down:

integer = input()

Great. Now we need to convert the input to an integer, so it can be used in the rest of our program. This can be accomplished using the built-in function int(). We'll pass in your string, and it will convert it to an integer:

integer = int(input('Enter a number: '))

Step two is to get a character from the user. That's easy. We'll use input() once again to get a single character from the user. but since the input need to be a character, we shouldn't convert this value:

character = input('Enter a character: ')

Alright. This is what we have so far:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

The last step is to print a box made of the value of character that has a width of integer and a height of integer. Now all we need to do is to print a row with integer number of characters integer number of times. This is where the nested for loop comes in.

We know each row must be the width of integer. So we need to print character that many times. We can use the range() built-in function to do this. We pass in integer as an argument, and it will generate all of the numbers from 0 to integer:

for _ in range(integer):
    print(character, end="")

The the reason we used the underscore in our for loop instead of an actual readable variable name, is because we don't need to use the values that the for loops would give us. We only care about repeating our code.

The end="" part is telling the print() builtin function not to print a newline each time it is called, because we want all the character to be in one row. The print() before the for loop is to add some space between our prompts and the program output. Let's see what our program outputs so far:

Enter a number: 5
Enter a character: @

@@@@@
>>> 

Great. The last thing to do is to print each row integer number of times. Since we know that executing the for loop above prints one row, we just need to execute it integer number of times to print integer rows. So we'll wrap that for loop inside of another for loop and use range() to execute the nested for loop integer number of times. We'll use print() once again, to add a newline between each row:

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

And that's it! Were done. Let's test our program:

Enter a number: 7
Enter a character: #

#######
#######
#######
#######
#######
#######
#######
>>> 

As you can see, we're getting our expected output. Here is the final program:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Error handling

You may also need to implement error handling. This means making your program recover from the user entering invalid input. In this case, the only time invalid input could really be enetered is when we are asking the user for an integer. We need to:

  1. make sure the user enetered a valid number. And
  2. make sure that number is either 3, 10 or any number in between.

To do this, we'll need a loop. This is so we can continue to ask the user for valid input, until they do so. We can use a while True loop. This means we'll keep looping until we break from the loop.

while True:

Now we need to make sure the user entered a valid number. We can use a try/except block. The way a try/except block works is that we put a block of code in the try portation that may raise an error. if no error is raised, we continue in the program normally. If an error is raised however, Python will jump to the except block and the code in there will be executed.

In our case we'll try to convert the users input to an integer. If this fails a ValueError will be raised. So we need to catch that error message and print it to the user:

while True:
    try:
        integer = int(input())
    except ValueError:
        print('Please enter a number')

All that's left is to test if the integer entered is in the correct range. If so, we'll break from our loop and continue with the program. If not, we'll display an error message and try again. Here is how that would look:

while True:
    try:
        integer = int(input('Enter a number: '))
        if integer >= 3 and integer <= 10:
            break
        print('Please enter a number between 3 and 10 inclusive')
    except ValueError:
        print('Please enter a number')

character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Here is how the above would look in the full program:

And here is a demo:

Enter a number: -23
Please enter a number between 3 and 10 inclusive
Enter a number: a
Please enter a number
Enter a number: 11
Please enter a number between 3 and 10 inclusive
Enter a number: 10
Enter a character: #

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
>>> 

For more resources about error handling in a program, see the question: Asking the user for input until they give a valid response.

2 of 3
0

A nested loop is a loop within a loop.

But how does it work?

The outer loop statement gets executed, which triggers the inner statement. The inner loop then executes till its completed..

So the inner statement will keep on going until it has satisfied the statement you have set it to.

Then, the outer loop triggers the inner loop again...and it will keep on triggering the inner loop forever until the outer loop's statement is satisfied.

🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-3-nested-loops
5.3 Nested loops - Introduction to Python Programming | OpenStax
March 13, 2024 - In this example, the outer loop ... minute portion between 0 and 59. hour = 8 minute = 0 while hour <= 9: while minute <= 59: print(hour, ":", minute) minute += 30 hour += 1 minute = 0 ... A nested for loop can be implemented ...
🌐
Rebus Community
press.rebus.community › programmingfundamentals › chapter › nested-for-loops
Nested For Loops – Programming Fundamentals
December 15, 2018 - As you can see we simply included ... (nested another if then else) for the “false action”. In our example, we nested if then else control structures. Nesting could have an if then else within a while loop....
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › nested-loops-in-c-with-examples-2
Nested Loops in C++ - GeeksforGeeks
A nested do-while loop means using one do-while loop inside another do-while loop, where the inner loop runs completely for every single iteration of the outer loop. Example Program to Print a Star Triangle Using Nested do-while Loops
Published   January 14, 2026
🌐
Codejumper
codejumper.com › downloads › htmls › lessons › en › Lesson14 › Lesson_14.html
Code Jumper Curriculum, Lesson 14: Nested Loops
After this lesson, students will understand the concept of a loop and how computer programs use nested loops. In this lesson, students will identify real-world examples of nested loops and choose one of the examples to record in their Computer Science Journals for future reference.
🌐
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A nested loop is a loop inside a loop.
🌐
Medium
medium.com › swlh › nested-loops-ee1dbb9fc8ab
Loop de Loop: When and How to Use Nested Loops | by Jenna Dean | The Startup | Medium
November 22, 2019 - Given the three different items that need to be labelled vary in size, you need to make the labels three different sizes. Let’s break it down. (And yes it is a very small wedding.) We will be using JavaScript to demonstrate today, but nested loops can be implemented in most of the major programming languages!
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-nested-loops
Python Nested Loops - GeeksforGeeks
... 2 * 1 = 2 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 3 * 1 = 3 3 * 2 = 6 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 · Explanation: When i == j, the iteration ...
Published   3 days ago