Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Understanding Loops

Discussion in 'Scripting' started by deepdarkblue, Oct 14, 2020.

  1. deepdarkblue

    deepdarkblue

    Joined:
    Sep 17, 2020
    Posts:
    6
    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)? f432dc59-e5bf-4cc9-995e-40933a183ec4.jpg d59ed98c-9039-4aee-955c-819fd1fa3874.jpg
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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.
     
    deepdarkblue and Bunny83 like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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:
    Code (CSharp):
    1. for(int i = 0; i < 10; i++)
    2.                      //^ there it is.
    But also, it keeps going for the same reason mentioned about
    continue
    .
     
    deepdarkblue likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,753
    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.
     
    deepdarkblue likes this.
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    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".
     
    deepdarkblue, Bunny83 and Vryken like this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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
    Code (csharp):
    1. for(int i = start; i < exclusiveEnd; i++) { }
    backward (notice how I don't play smart here, I just completely invert the logic)
    Code (csharp):
    1. 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
    Code (csharp):
    1. for(int i = 0; i < Count; i++) { } // Count will be excluded
    0, 1, 2, 3, 4, 5, 6, 7

    backward
    Code (csharp):
    1. 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
     
    Last edited: Oct 15, 2020
    deepdarkblue likes this.