+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Tutorial: Looping in C#

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Tutorial: Looping in C#

    Looping in C#

    By Xav

    Introduction

    Hi, Xav here (again). Here we're going to see how loops are used in C#. The syntax is actually very similar to languages such as JavaScript and PHP, so if you are familiar with either of those languages then it will be absolutely no effort to learn the nuts and bolts of C#'s looping constucts.

    What is looping?

    Looping is a special component of code. Basically, loops are used to re-execute lines of code multiple times. Instead of having to write out the same code multiple times, you can simply use a loop. Also, you may not know at the time how many times a piece of code needs to occur.

    The For Loop

    The For loop is used to execute the code a specific number of times. It has the following syntax:

    Code:
    for (initialisation; decision; increment)
    {
     //Code to loop goes here.
    }
    The main line is the "for" line. After the for keyword is used, the three settings are stated within brackets. For example:

    Code:
    for (int i = 1; i <= 100; i++)
    {
    MessageBox.Show("We are on message number " + i.ToString() + ".");
    }
    This would display a message box 100 times, each one containing the number it was on. Notice how the "i" variable is available as a parameter - you can use it to reference the current recurrence.

    So what is actually happening here? Well, first a new integer variable called "i" is created. This is then set to the value 1. It then checks if i (1) is smaller than 100 - it is, so it executes the code (remember, i = 1 at the moment). After the end bracket is reached, the code execution goes back to the top statement, and it uses i++ to add one to the variable. "i" now contains the value of 2. Again, it checks the condition, and executes the code again, this time using i = 2, until i goes beyond 100. After this, code execution resumes after the curly closing brace }.

    So there you have it! Loops can be complex to understand, but they make things unbelievably simple to repeat code blocks. Most programs will use some sort of loop, and can be very effective. Also, you can use a "do" or "while" loop - but that's for another tutorial, as this one's getting too long to type.

    Test yourself: Write a prime number generator, where you type in the upper and lower values, and it uses two nested "for" loops to find all the prime numbers between them. Hint: use modulus arithmetic, with the % symbol.

    Have fun!

    Xav

    P.S. If this helped you then please +rep my post. Leave any comments below!
    Last edited by Xav; 05-22-2008 at 09:53 AM.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Looping in C#

    Bump! Doesn't anyone have any praise/criticism/comments to leave? I spent a lot of time on this, you know...

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Jordan Guest

    Re: Tutorial: Looping in C#

    Nice tutorial, I didn't even see this one. What about the while, foreach and do while loops? I like the "test yourself", nice touch!

  5. #4
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: Tutorial: Looping in C#

    Quote Originally Posted by Jordan View Post
    Nice tutorial, I didn't even see this one. What about the while, foreach and do while loops?
    Agreed. It's quite a basic tutorial, but it's good.

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Looping in C#

    You think I should do one for the other loops? This one was supposed to be just an introduction to how loops work. I might do another one to cover the other three loops - if there's rep involved...

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: Tutorial: Looping in C#

    As far as I remember, weren't you the one that told me that there should be no begging for reps? Huh?

    Do it if you feel like it, MAYBE +rep will follow.

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Looping in C#

    I remember it well.

    But in both cases, we told one another that we were merely requesting appreciation for each other's posts.

    Why would I feel like making a tut? It's not like it's FUN or something...

    I'm gonna just go through and replace the CODE tags with HIGHLIGHT tags. See you soon.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Looping in C#

    OK, it's been too long, I can't edit it anymore.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    Re: Tutorial: Looping in C#

    Quote Originally Posted by Xav View Post
    OK, it's been too long, I can't edit it anymore.
    That's lame. You should be able to edit it.

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Tutorial: Looping in C#

    Well it's not my forum, is it? Jordan or John's the peeps to ask.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. need help with some looping
    By BRUTAL in forum Python
    Replies: 2
    Last Post: 01-24-2011, 02:38 PM
  2. need looping help
    By BRUTAL in forum Python
    Replies: 2
    Last Post: 01-22-2011, 07:57 AM
  3. help with looping
    By Root23 in forum Python
    Replies: 7
    Last Post: 06-10-2010, 08:08 PM
  4. new looping techniques
    By Chinmoy in forum C Tutorials
    Replies: 1
    Last Post: 07-23-2008, 02:04 PM
  5. Tutorial: Looping in C#, Part 2
    By Xav in forum CSharp Tutorials
    Replies: 10
    Last Post: 05-31-2008, 09:17 AM

Tags for this Thread

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