Search Unity

Loops (While,Do,For) help C#

Discussion in 'Community Learning & Teaching' started by twinspectre90, Mar 12, 2014.

Thread Status:
Not open for further replies.
  1. twinspectre90

    twinspectre90

    Joined:
    Feb 2, 2014
    Posts:
    15
    as the title say
    can someone of you explain to me how (While,Do,For) loops works ?
    please i need your help :(
    i'm using the C#
    thanks in advance :)
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. twinspectre90

    twinspectre90

    Joined:
    Feb 2, 2014
    Posts:
    15
    Last edited: Mar 12, 2014
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    There is tons of good material for this kind of basic question around. It is a different topic if you are asking for something that wasn't answered hundreds of times on the net.

    Edit: You can even find tons of video tutorials about it:
    http://www.youtube.com/results?search_query=c# loops
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    A loop is a process that continues over and over again. When we use loops in programming we are iterating through a piece of code or multiple lines of code until a condition is met. The condition met tells the computer when to stop looping.

    With a for loop:

    for (int i = 0; i < 10; i++)
    {
    print(i);
    }

    the for loop reads: for variable i assigned to 0; for as long as i is less than 10, print i, then increment i by 1.

    This will loop till i is no longer less than 10. Now i = 1, is 1 less than 10? So loop the code, and increment. does that 8 more times. i = 10. is 10 less than 10? No, so the loop stops.

    While(CONDITION)
    {

    }

    The while loop is a loop that loops while a condition is met.

    ex:

    while(true)
    {
    //code
    }

    This is an example of an infinite game loop. While true, we want to run the code. The problem with this, is that true is never false, thus the while loop runs for ever and will crash your system. You typically do while loops for spawn managers. Check out my tutorial series for some examples.

    Do-While loop.

    do
    {
    i++
    }while(i < 5);

    This loop will run always at least one time, and then the while loop is a condition. So for instance, do this code, while i is less than 5.

    It will run through this code 5 times till i is no longer < 5.

    This is a very brief explanation of how loops work. I suggest checking out some youtube videos or documentation online.


    Best,

    Jon
     
  6. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606


    thank you!!!!
     
    SubZeroGaming likes this.
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sorry, I'm seemed to have missed this thread.

    Please Note:

    The Teaching section is to discuss and support specific Teaching material and the Learn section of the website. For basic support please use the relevant section of the forum. Some suggestions would be:

    As this topic is not specific to Teaching Material or the Learn Section of our site, but is simply asking a technical question - I am closing this thread.
     
    hippocoder likes this.
Thread Status:
Not open for further replies.