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, ...
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
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
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
Is it bad to use a lot of nested while loops?
You could try splitting things out into functions to avoid nesting too deep in loops. More on reddit.com
09:44
Learn nested loops in 9 minutes! ➿ - YouTube
Nested Loops Part 1 | Introduction - YouTube
Nested loops in Python are easy
05:18
C nested loops ➰ - YouTube
06:35
Nested Loops - Visually Explained - YouTube
08:44
Learn Java nested loops in 8 minutes! ➿ - YouTube
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.
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.
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.
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
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.
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.
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.
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.
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!
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.
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, ...
GeeksforGeeks
geeksforgeeks.org › python › python-nested-loops
Python Nested Loops - GeeksforGeeks
In Python, there are two types of loops: for loop and while loop. Using these loops, we can create nested loops, which means loops inside a loop. For example, a while loop inside a for loop, or a for loop inside another for loop.
Published March 13, 2026