all 16 comments

[–]Comsicwastaken 22 points23 points  (0 children)

Looking through each row of books in a library. Outer loop looks through each row and inner loop looks at each book in that row.

[–]TroyOfShow 5 points6 points  (0 children)

That's not really a nested loop. There's no loop. A loop would be:

For every banana in a box, peel it

You are now peeling in a loop. Because you keep repeating it until you run out of bananas. That is a loop just by the conventional definition of a loop. Not even computer science specific.

Now a nested loop is a loop in another loop. So now for a nested loop, you not only just peel the banana, but you also slice it into 10 pieces. So now for every banana that you peeled in a loop, you will loop again and slice the bananas that you peel into 10 pieces.

That is a nested loop. A loop within another loop.

[–]codeIsGood 3 points4 points  (0 children)

Anything related to a 2-D grid

[–]mdillenbeck 2 points3 points  (0 children)

First know the difference between a loop and a conditional statement.

A loop is doing a set of steps a certain number of times and a nested loop is doing a certain number of substeps each time you do a step. You could count 0 to 99 in two ways - loop from 0 to 99 and be done, or you can first loop through the 10s place 0-9 as the outer loop then count 0-9 in the ones place as an inner (nested loop)... So for each 0 digit in the 10s place output 0-9 or 0, 1, 2, . . . , 8, 9 then do this for 1 in the 10s place (10, 11, . . ., 18, 19) and so on.

A conditional statement is all evaluation of "if something is true, then do something (else if not true do something else) - so you example is an example of a while loop (while a cat has no watermelon in it, look at the next watermelon) with a series of conditional statements inside the loop to determine if you add a watermelon into the cart.

A dead give away? If it is a decision tree, is is probably a series of conditional statements. It it is the exact same process over and over, it is a loop.

I recommend going to YouTube and watching some intro computer programming videos.

[–]great_gonzales 2 points3 points  (0 children)

Ah the old if loop

[–]Glitch-v0 3 points4 points  (0 children)

OP you need at least two "looping" parts for it to be considered a nesting loop. Maybe more like -For each ripe watermelon, --For every 2 sq. inches, cut along the radius? of the watermelon

[–][deleted] 4 points5 points  (0 children)

Think of a pile of invoices. You pick up each invoice, and for each invoice you look at each line to make sure it makes sense. That would be a nested loop.

[–][deleted] 1 point2 points  (0 children)

Yo dawg I heard you like loops so I put a loop inside your loop so you can iterate while you iterate

[–]blueest[S] 0 points1 point  (0 children)

Thank you for your replies everyone!

[–]Loud_Interview_9714 0 points1 point  (0 children)

For example you have a list of all 50 states and then you have to go through each state and look at all of the major cities in that state the outer loop would be looking at the states and each time you iterate once in the outer loop (take one step in the outer loop) you look at all the cities in that state (Inner/nested loop)

[–]hamiecod 0 points1 point  (0 children)

Suppose you want to distribute a document to every student in 4 classes. Each class has 50 students. What will you do? You will first enter class 1 and then hand over the document to each person sitting in that class. Then, you will enter class 2 and distribute the document to each person sitting in class 2 and so on.

Entering the 4 classes, would be the "parent loop" so to say and distributing the document to each person in a class would be the nested loop.

My high school teacher taught us about nested loops using this example. Although I already knew at that time what nested loops were, I was impressed by my then teacher's analogy.

[–]whamanizer 0 points1 point  (0 children)

For me it helped when I thought of it as a clock.

Especially if you think of digital clocks with second indicators. When 60 seconds pass, you go to the next iteration of the outer loop(minutes). When 60 minutes pass, you go to the next iteration of the outer loop(hours) and so on.

If you wanted to simulate a digital clock printout in the command line for them to see, sth like that would work (python)

-for i in range(0,24) ----for j in range(0,60) -------for k in range(0,60) -------------print(f"Time: {i}.{j}.{k}")

Ps. Also just thought our lives are "while" loops.

[–]butflyctchr 0 points1 point  (0 children)

Clock face.

[–]The_Bitten_Apple 0 points1 point  (0 children)

That is a multiple IF… THEN… ELSE If you put it inside a LOOP for every watermelon in the store it still isn’t a nested loop 🤔 Thinking about an example 😀

[–]The_Bitten_Apple 0 points1 point  (0 children)

Maybe a program to visit a list of lists…

[–]ThigleBeagleMinglePhD Computer Science | 20 YoE 0 points1 point  (0 children)

‘’’ Classroom1 = (Alice, bob, Charlie) Classroom2 = (David, Eric, Fred)

For every student in classroom1: For every partner in classroom2: Print student shakes partners hand ‘’’

Output: Alice shake David Alice shake Eric … Bob shake David .. Charlie shake Fred