🌐
W3Schools
w3schools.com › c › c_for_loop_nested.php
C Nested Loops
This example uses nested loops to print a simple multiplication table (1 to 3): int i, j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { printf("%d ", i * j); } printf("\n"); } ... Nested loops are useful when working with tables, matrices, ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit4-Iteration › topic-4-4-nested-loops.html
4.4. Nested For Loops — CSAwesome v1
A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, ...
Discussions

python - What is a nested loop and how do I use it in example below? - Stack Overflow
A nested loop is a loop within a loop. For example: 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
Is nesting loops considered a bad practive?
Let’s take a bit of a diversion and talk about the basics of Big O notation. It’s a way of measuring how long your code will take to run. Here are a few of the key examples: O(n) - linear time - Loops are the classic example. For each extra input you have to run the loop one more time. O(n) solutions are typically pretty good, but if you have a ton of inputs they will start to slow down. Also, beware linear time solutions when you could have used something constant time (e.g. using arr.find(...) instead of obj[...]). O(logn) - log time - A binary search is the classic example. Every time you double the inputs you only add one more iteration. This is the holy grail of algorithms. Phds get paid big bucks to come up with logarithmic solutions to problems that used to be linear. O(n^2) - quadratic (or exponential) time - I usually lump quadratic (n squared) together with exponential (n to any power) because they are just different flavors of bad. A nested loop is the classic example, however, just because you have a loop in a loop does not mean you have O(n^2). Iterations have to double with each input. For example say you have an array of numbers and want to check if any is equal to any of the others. The naive O(n^2) solution would be to use a nested loop through the entire array for each element in the array. With 1000 numbers, you would need to run 1000^2 or one million iterations. At a certain point you will just crash your web browser. Okay, there is plenty more to learn about Big O and algorithms in general, but I wanted to give you a quick background so we can talk about how it impacts your problem. You have been told “nested loops are bad”, but what people really mean is “O(n^2) solutions are bad”. In your case, you have a 2d (or more) array. But if when you add a new input it's just one new element to one sub-array (as opposed to one new element to every sub-array), then what you are talking about is linear time. The nested loops are just an artifact of how you organized your data. One new input adds one more iteration, so we are talking O(n). (As others have said though, it might still be worth simplifying your data structure if it makes your code more clear and readable) More on reddit.com
🌐 r/learnjavascript
16
8
March 21, 2019
Are nested loops in this situation bad?
Nested loops are fine. Similarly with if statements, you probably want to avoid unecessary nested loops as it might make the code hard to read but in the case that you're simply iterating something, I think its still quite readable. More on reddit.com
🌐 r/cpp_questions
24
4
December 30, 2022
🌐
Reddit
reddit.com › r/learnprogramming › can someone please explain nested loops / loops in general, like i'm five? (c++)
r/learnprogramming on Reddit: Can someone please explain nested loops / loops in general, like I'm five? (C++)
March 20, 2014 -

I'm in a beginner class at my high school, and I'm trying to learn programming. I find loops sort of easy to understand, but we're supposed to create a multiplication table using a nested loop and I just can't get it. Help! Thank you!

Top answer
1 of 5
7
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
2 of 5
2
A nested loop will perform the inner loop in its entirety for each iteration of the outer loop. So if you had an inner loop of 10 with an outer loop of 3, it's going to go 1-10, 3 times.
Learning C: For Loops Aug 10, 2024
r/C_Programming
last yr.
How to implement a nested iterator? Oct 18, 2023
r/cpp_questions
2y ago
Loops in loops... in loops... in loops... Feb 29, 2020
r/learnprogramming
6y ago
nested loops in python. Nov 1, 2022
r/learnpython
3y ago
More results from reddit.com
🌐
Mimo
mimo.org › glossary › programming-concepts › nested-loops
Nested Loops: Definition, Purpose, and Examples
Nested loops can combine different types of loops (such as for, while, or forEach). They can iterate over arrays, dictionaries, matrices, query rows, or even UI structures. The key is that each loop has its own scope and variables, and they interact in a predictable sequence.
🌐
Programiz
programiz.com › java-programming › nested-loop
Nested Loop in Java (With Examples)
Created with over a decade of experience and thousands of feedback. ... If a loop exists inside the body of another loop, it's called a nested loop. Here's an example of the nested for loop.
🌐
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 Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... It is also possible to place a loop inside another loop.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › nested-loops-in-c-with-examples
Nested Loops in C - GeeksforGeeks
For a nested loop, the inner loop performs all of its iterations for each iteration of the outer loop. If the outer loop is running from i = 1 to 5 and the inner loop is running from j = 0 to 3.
Published   November 7, 2025
🌐
Rebus Community
press.rebus.community › programmingfundamentals › chapter › nested-for-loops
Nested For Loops – Programming Fundamentals
December 15, 2018 - Nested for loops places one for loop inside another for loop. The inner loop is repeated for each iteration of the outer loop.
Find elsewhere
🌐
ScholarHat
scholarhat.com › home
Nested Loops in C - Types of Expressions in C ( With Examples )
The efficient handling of complicated data structures and multi-level iterative issues is made possible by nested loops. They are adaptable to different programming tasks. Example: Print a multiplication table using stacked loops in which the inner loop iterates through the columns and the outer loop iterates through the rows.
Published   July 31, 2025
🌐
Programiz
programiz.com › cpp-programming › nested-loops
C++ Nested Loop (With Examples)
This is how we can use nested loops. // C++ program to display 7 days of 3 weeks #include <iostream> using namespace std; int main() { int weeks = 3, days_in_week = 7; for (int i = 1; i <= weeks; ++i) { cout << "Week: " << i << endl; for (int j = 1; j <= days_in_week; ++j) { cout << " Day:" << j << endl; } } return 0; } ... Week: 1 Day:1 Day:2 Day:3 ... .. ... Week: 2 Day:1 Day:2 Day:3 ... ... .. We can create nested loops with while and do...while in a similar way.
🌐
WsCube Tech
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
November 5, 2025 - Understand how Python nested loops work, their syntax, and practical examples to enhance your programming skills and solve complex problems efficiently.
🌐
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... A nested loop is a loop inside a loop.
🌐
The Linux Documentation Project
tldp.org › LDP › abs › html › nestedloops.html
Nested Loops
A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again.
🌐
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - A nested loop is a part of a control flow statement that helps you to understand the basics of Python. In Python, the for loop is used to iterate over a sequence such as a list, string, tuple, other iterable objects such as range.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › nested-loops-in-programming
Nested Loops in Programming - GeeksforGeeks
April 30, 2024 - Nested loops in programming are like loops within loops. They're used when you need to do something repeatedly inside another task that's also being repeated. For example, if you have a list of students, and each student has a list of grades, ...
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
February 22, 2025 - This code example shows one way to generate pairwise combinations while filtering out self-matchups. This pattern comes in handy whenever you’re comparing elements in a group. As you’ve seen, nested loops make it easy to create pairwise combinations, whether you’re including or filtering out matches. This isn’t only useful in games, but also in real-world scenarios like comparing data entries. With that under your belt, you’ll now take a look at an example that uses while loops in nested situations.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › nested-loops
Nested Loops in C Programming (With Examples)
March 12, 2026 - Learn in this tutorial about nested loops in C with examples. Understand how loops inside loops work to solve complex problems efficiently. Read now!
🌐
W3Schools
w3schools.com › cpp › cpp_for_loop_nested.asp
C++ Nested Loops
This example uses nested loops to print a simple multiplication table (1 to 3): for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { cout << i * j << " "; } cout << "\n"; } ... Nested loops are useful when working with tables, matrices, ...
🌐
Medium
medium.com › @nathjanmjay › title-exploring-different-types-of-nested-loops-in-programming-a2e479bb28f4
Title: Exploring Different Types of Nested Loops in Programming | by Nath Janm jay | Medium
February 29, 2024 - In this example, the outer loop runs three times, and for each iteration of the outer loop, the inner loop runs twice. So, the output will be: ... Simple nested loops are the most basic form of nested loops, where one loop is contained within another.
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.