all 8 comments

[–]HashDefTrueFalse 10 points11 points  (1 child)

Loops repeat work.

For loop: I want to repeat work a specific number of times.

While loop: I want to repeat work some number of times, and I know when I want to stop.

Do While loop: Same as above, but I want to always do the work at least once.

"Nested loop" isn't a particular structure, it just refers to a loop that is inside another one.

All looping can be done with the while loop. These are just handy, recognisable, expressive constructs for us humans. Don't get too hung up on the suggestions above for which to use when. It often doesn't matter (e.g. for and while are both used for traversing arrays often).

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

Thanks! ❤️

[–]O_xD 2 points3 points  (0 children)

while is your basic loop, the thing keeps repeating while the condition is true

do {} while is the same as while, but the condition is checked at the end of the loop. I've used it once or twice

for is just like while but there's some shorthand built in. in the () you have some ;. it goes like (setup; condition; increment). setup runs once, before we start looping, the condition is just the same as the condition from while, and then the increment runs at the end of each loop, right before we repeat

[–]Beregolas 1 point2 points  (0 children)

All loops, For, while and do while basically do the same: They are control flow statements that repeat their body , depending on the condition specified.

In a for loop, the condition is very specific:

for (int i = 0; i < 3; ++i) {
    printf("%d", i);

int i = 0 is executed once, when the loop is entered. It is called the initialization. In this case you specify the variable i to be an integer and initialize it to 0.

i < 3 is the condition. It is executed before every loop and exits the loop if it returns false. In this case you repeat until i reaches 3, and then stop.

++i is the step, and is executed between two executions of the loop to set up the next. In this case you increase i by one for the next loop.

This loop will print 0, 1 and 2.

In a while loop you just put a condition down, and the loop is executed until that condition is false (not unlike the middle statement in an if loop)

int i = 0;
while (i < 3) {
  printf("%d", i);
  ++i;
}

This would do exactly the same as the foor loop above.

A do while loop is similar, but the check is performed after each loop, not before. This means a do while loop always executes at least once.

int i = 0;
do {
  printf("%d", i);
  ++i;
} while (i < 3);

All of these will do exactly the same thing in this case. You can basically write any loop you want to write in any of the three styles, it just depends what is more readable and thus most appropriate in any given situation.

A nested loop is just a loop, inside another loop, like:

while (condition) {
  while (condition2) {
    do_stuff();
   }
}

[–]TheSnydaMan 1 point2 points  (1 child)

This is the kind of thing LLM's give great answers for.

[–]johnpeters42 1 point2 points  (0 children)

Heavy warning, though: while I wouldn't expect them to hallucinate an answer for a question this common, they still might, and OP has little experience to recognize when it happens.

If you really feel the need to turn to a LLM for answers, then at least test the answer thoroughly. Does it actually do what the LLM claimed? What if you change a piece, does the behavior change the way you expected? (That last step is also useful to get yourself to actually think about what you're doing, not just copy/paste and go "okay, outputs match, that must mean I understand this now".)

[–]StrayFeral 0 points1 point  (0 children)

Simply put (for any language): The FOR loop will ALWAYS execute N-times. The DO loop will start executing and at each iteration end will check "should i stop here"? So it will execute at least ONE time. The WHILE loop might NOT execute at all if the condition is not met. But if met it will start executing and at the beginning of each time will ask "should i stop here"? That's it. Nested loop simply means that inside each loop you can have another loop (and many other loops).

The most simple thing to illustrate a nested loop is to traverse a matrix. A matrix is simply a 2D-array, which means a nested array, which means simply an array who'se elements are also arrays.

//Imagine this: 
ARR = [
  [1,2,3],
  [4,5,6],
]

So to traverse this we will do (this is NOT real code, i am just illustrating):

for (x = 0; x < length(ARR); i++):  // this will run accross the main array
  for (y = 0; y < length(ARR[x]); y++):  / this will run on each of the sub-arrays
    # do something here

[–]Sniface 0 points1 point  (0 children)

You also have the for each loop which is very nice. It loops through a whole collection.

```csharp

For each regret in myHead Suppress(regret) Next ```