Understanding Loops

I know what “i++” does but why is it necessary to have this statement (in the first image) in order to print the whole list (and if there is already one “i++” at the bottom)?

The first one is a while state so when i does equal 4 it would be stuck in an endless loop.

The second is a for statement which will increment i by one each iteration.

2 Likes

Because the continue keyword skips the rest of the code in the loop, and starts back at the top. So without that i++ you will be stuck at 4 forever.

3 Likes

I think you’re confusing continue with break.
continue will skip the current iteration in the loop, but keep the loop running. break will stop the loop entirely.

So in the first image, when i == 4, It increments i without printing to the console, then skips over to the next iteration of the loop.
If you had a a break instead of a continue there, it would only print out 0-3 in the console.

In the second image, “keeps going without i++” is wrong. Look at the for-loop:

for(int i = 0; i < 10; i++)
                     //^ there it is.

But also, it keeps going for the same reason mentioned about continue.

1 Like

You’ll also notice the lack of the digit 4 in the output in the second image, as expected: your continue skips over the print statement.

1 Like

No one would ever write it the first way. Those two look like an example of why FOR loops are often better than WHILE loops. There’s showing you “if you try it with a WHILE, it will need this extra i++ in the middle that looks really funny and is easy to forget”.

3 Likes

this is just a general overview of my own loop practices, not an answer to this question.

as a rule of thumb: do not use while for general purposes.

while is to be used when you have a clean condition to bail, and similarly no counters to rely upon.

additionally, while is to be used in place of empty for(;;) clause, that is deliberately endless because it expects a bailing condition in the code block.

when this happens, and this really depends on the problem at hand, I almost always have it as while(true) { } to emphasize this intent, as this kind of loop should be buried deep into the low-level parts of the system, and commented out, so no random tinkering is expected in the future.

I personally only use do .. while clause when my algorithm has a trailing condition check anyway, and so it saves a line or two.

in short:

  1. I never mix the loop clauses intermittently, that’s a sure way of fooling the brain into doing something wrong
  2. I always use for front and center, occasionally foreach if a design demands it (foreach is slower and more involved; but uncountable sets and other enumerated collections simply have to be iterated with it)
  3. I take extra care with while loops, and sometimes even introduce a safety counter (called like that) to prevent it from being eternally stuck under unforeseen combinations of factors (obviously, whenever this makes sense; it does make sense for some non-critical high-level code)
  4. while will block your editor from time to time due to poor design or non-handling on your part, learn to expect it, and save your work until Unity finds a way to detect this in-editor and suspend the execution (I’m not sure why’s that hard to do)
  5. I only use do .. while when this produces code that’s less likely to confuse me

A useful mnemonic to adopt when working with for is that after you decide whether you need a forward iteration or a backward one, you stick to these templates, and simply reuse them everywhere.

forward

for(int i = start; i < exclusiveEnd; i++) { }

backward (notice how I don’t play smart here, I just completely invert the logic)

for(int i = start; i >= inclusiveEnd; i--) { }

I know it looks like nothing, but this can massively boost your reading of for sections, and make your reasoning about them snappier and unhindered with trying to translate the logic into individual iterations – after practice it becomes ‘what you see is what you get’ without too much thinking.

Additionally, having backward iterations to work as complements, makes your code prettier. Consider working with arbitrary counts and zeroes, which is the typical for usage pattern.

forward

for(int i = 0; i < Count; i++) { } // Count will be excluded

0, 1, 2, 3, 4, 5, 6, 7

backward

for(int i = Count - 1; i >= 0; i--) { } // zero will be included

Count - 1 is a standard mnemonic for “the last item” so nothing particularly messy there
7, 6, 5, 4, 3, 2, 1, 0

1 Like