Jump to content

Tutorial: Looping in C#

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

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:

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:

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!

Edited by Xav, 22 May 2008 - 08:53 AM.

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Bump! Doesn't anyone have any praise/criticism/comments to leave? I spent a lot of time on this, you know... :D
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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!

#4
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

Jordan said:

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.

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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... :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
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.

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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. ;)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
OK, it's been too long, I can't edit it anymore. :(
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

Xav said:

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

That's lame. You should be able to edit it.

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Well it's not my forum, is it? Jordan or John's the peeps to ask.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
Guest_Jordan_*

Guest_Jordan_*
  • Guests

Xav said:

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... :)

Yes, you should do one. You should also make an index page with all of your tutorials which tells the user which ones to take first, second, etc.. I should also make one of these....

There is always rep in good tutorials!!!! And from me it's like getting a +rep from 10 members.

TcM said:

That's lame. You should be able to edit it.

err, yeah, I guess it is kinda lame.....

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Well, it's your forum, nothing's out of you and John's grasp... ;)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums