Basic C# Tutorials: Iteration[Part 1]
welcome to Game-Tuts.com - Register
Results 1 to 5 of 5
  1. #1
    ಠ_ಠ


    Join Date
    Nov 2009
    Location
    East Coast USA
    Posts
    1,660
    Thanks
    352
    Gamertag
    th3 d1rty hippe

    Default Basic C# Tutorials: Iteration[Part 1]

    Today I am going to be covering iteration. It is kind of hard for me to explain exactly so here is a definition:
    Quote Originally Posted by Wikipedia
    Iteration means the act of repeating a process usually with the aim of approaching a desired goal or target or result. Each repetition of the process is also called an "iteration", and the results of one iteration are used as the starting point for the next iteration.
    If you don't understand that then just read the first sentence. It is basically doing something over and over until you gaet what you want. In programming there are a few kinds of iteration statements. For loops, while loops, do loops, and foreach loops. I will try and go over all of them. Also in loops there are iteration variables which are the temporary variables that are being changed for each iteration. Ok. Now I will start explaining the loops.

    For Loop:

    A for loop is a loop that goes until some kind of number reaches a preset max value. It can be used to search for something in a file. And the preset value would be the size of the file. Then the loop can go byte by byte over the file and add all instances of what was searched for and where it was found to a list. They are incredibly useful. Here is the basic syntax of one:
    Code:
    for (int i = 0; i <= 10; i++)
    {
    //writes the number that the for loop is currently at to the console
    Console.WriteLine(i);
    }
    Console.ReadLine();
    BreakDown: the first statement int i = 0; creates the initial number to count to. The next statement i <= 10 is the check statement. Remember decision statements! That is kind of like an if then. If i is not less than or equal to ten then it will evaluate to false and the loop will terminate. Then the last statement i++ adds 1 to i if the previous statement is true. It can also be i-- when you are starting the iteration from a higher value. Here is an example of that:
    Code:
    for (int i = 10; i >= 0; i--)
    {
    Console.WriteLine(i);
    }
    Console.ReadLine();
    That will do what the first one did in reverse. This one counts down from 10 to 0. I hope you understood that. Next I will talk about the while loop.

    What the while loop does is it has a single statement inside its parentheses. If that statement evaluates to false then the loop terminates but if not then it continues. Even simpler than the for loop. Here is a a basic example of how a while loop works. It does not have to be exactly like this but this is how I like to do it.
    Code:
    bool condition = false;
    while (!condition)
    {
    if (checkStatementHere)
    {
    condition = true;
    //loop terminates
    }
    else
    {
    //loop Continues
    }
    }
    Its pretty basic so I am not going to explain more. Post if you want to know more about it. The reason I am not explaining more is because this is not a book and if you really want to learn a ton then go to a bookstore and buy a book. If you want recommendations then PM me.

    The last loop I will talk about is the foreach loop. The name is pretty verbatim. For each item in a collection it will iterate with statements for that specific item. I use it a ton when using X360 and FATx so I can do statements for each partions in all the partions. Here is an example. I will comment this a little more because it is a little more advanced:
    Code:
    //the following variable is an array of strings.  each quotation set in it
    //is a seperate string.
    string[] texts = new string[] { "Hello ", "This ", "Is ", "A ", "Test" };
    //the following loop will iterate through it with the variable x holding the 
    //temporary string that is currently being used.
    foreach (string x in texts)
    {
    //the following will write the strings to the console
    Console.WriteLine(x);
    }
    Post if you want to know anymore. You can also use generics and have a generic list of strings. I will go into those in the future.
    I see you read my signature.

  2. The Following User Says Thank You to Matt For This Useful Post:

    C4D3N (11-13-2010)

  3. #2
    That Guy You Know


    Join Date
    Jul 2010
    Posts
    5,236
    Thanks
    2057

    Default Re: Basic C# Tutorials: Iteration[Part 1]

    I hope this gets STICKIED! Good contribution. =D

    I will answer any and all questions regarding my big toe.

  4. The Following User Says Thank You to Skull Kid For This Useful Post:

    Matt (11-13-2010)

  5. #3
    ಠ_ಠ


    Join Date
    Nov 2009
    Location
    East Coast USA
    Posts
    1,660
    Thanks
    352
    Gamertag
    th3 d1rty hippe

    Default Re: Basic C# Tutorials: Iteration[Part 1]

    Thanks. :) I will make an index later with links to all the lessons. The first one was. I will tomorrow because I just got home from the gym and am ready to just chill and play black-ops. :)
    I see you read my signature.

  6. #4
    VIP
    Join Date
    Jul 2010
    Location
    Arkansas
    Age
    19
    Posts
    1,182
    Thanks
    360
    Gamertag
    NINJA O PAIN

    Default Re: Basic C# Tutorials: Iteration[Part 1]

    I'm going to save all your tuts and use them while hunting lol. Thanks

    Hope I don't get caught pirating at college. I'm like a kid in a candy store with this speed.

  7. #5
    ಠ_ಠ


    Join Date
    Nov 2009
    Location
    East Coast USA
    Posts
    1,660
    Thanks
    352
    Gamertag
    th3 d1rty hippe

    Default Re: Basic C# Tutorials: Iteration[Part 1]

    Lol. Thanks. You were the inspiration for starting them. :D And I am not sure on what the next one will be. I am thinking either string manipulation or generics. String manipulation, I am just going to going to talk about modifying them, reversing them and some other stuff. And with generics it is kind of boring but very important. It will be about the List<Type> and the type in that is the type of variable you would like the list to be of. I think string manipulation but IDK. Lol.
    I see you read my signature.


 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
visit GameTuts on these social networking sites