Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Tutorial: Looping in C#, Part 2

loop

  • Please log in to reply
10 replies to this topic

#1 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 May 2008 - 05:50 AM

Looping in C# - Part 2
By Xav


Following on from my other tutorial, http://forum.codecal...-looping-c.html, here we will discover the other three types of loop: while,foreach,do while.

The Foreach Loop
This loop is very useful, as it cycles through every object in an ObjectCollection. For every iteration of the loop, there is a local variable available which contains the current object. Here is the syntax:


foreach (*initialization* in *objectcollection*)
{
//Code goes here.
}

Here is an example of a foreach loop:


int numbers = {1,2,3,4,5};
foreach (int num in numbers)
{
MessageBox.Show(num.ToString());
}


The While/Do While Loop
The while loop executes until a certain condition is met. It takes the following syntax:


while (condition)
{
//Code goes here.
}

Here is an example (which in this case could easily be done using a for loop):


int x = 0;
while (x < 10)
{
MessageBox.Show(x.ToString());
x++;
}

The Do While loop is similiar:


do
{
//Code goes here.
} while (condition);

For example:


int x = 0;
do
{
MessageBox.Show(x.ToString());
x++;
} while (x < 10)


Differences between While and Do While
There is one main difference between the Do and Do While loops - the Do loop always executes at least once, as the condition is at the bottom, not the top. However, there is a chance that the Do While loop may never execute at all. Bear this in mind when writing your code.

Test yourself: Add some controls to a form. Write a program that cycles through all the controls, and displays the name of each one to the user in a message box. Hint: Use the Form.Controls[] ObjectCollection. Another hint: It's an object collection...

Hope this helps!
Xav

P.S. If this was helpful, please +rep. Leave any comments/suggestions/praise below!
  • -3
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 May 2008 - 11:17 AM

Eh? LogicKills did a -rep on me, because my tutorials are too short! I don't really think that's fair. :(
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 27 May 2008 - 09:38 PM

Well, he has the fully right to do so. If he thinks your tutorials are short, then he's allowed to give you negative reputations.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 May 2008 - 12:29 PM

Yeah - but I spent good time on that tut, and then people start being ungrateful. LogicKills hasn't exactly posted any tutorials yet. :(
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 LogicKills

LogicKills

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 28 May 2008 - 02:27 PM

Yeah - but I spent good time on that tut, and then people start being ungrateful. LogicKills hasn't exactly posted any tutorials yet. :(


Spent a lot of time?
That is 5 minutes worth of work. When you write something you want it to stick out compared to others ones, not a carbon copy. Stop whining, this is the internet.
  • 0
http://logickills.org
Science - Math - Hacking - Tech

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 28 May 2008 - 05:47 PM

Not a bad tutorial. Again, I like the test yourself portion. It seemed like a fair enough length to me but I did read it on my mobile.:)

I will give +rep in the morning.


Posted via CodeCall Mobile
  • 0

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 29 May 2008 - 10:29 AM

Just received the rep. Thanks!

I know LogicKills' negative rep didn't leave a massive dent, but it's the principle. For my <script language="php"> 'tutorial' I received +rep, and it wasn't more than 5 mins' worth of work. It's not about length, it's about usefulness. Anyway, you requested this based on the other tut, which I didn't receive rep for, so this was just a follow-up in the series. :)

But if I'm going to lose reputation points whenever I post a tutorial, then maybe I'll just stop writing them... :(
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 29 May 2008 - 10:32 AM

I agree it isn't about the time spent or length. In fact, some newb may find this extremely helpful, however, he does have the right to give you -rep just as I gave you +rep.
  • 0

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 29 May 2008 - 10:36 AM

I know he has the right, but it's not very encouraging in terms of how likely I am to give up some of my time to write a tutorial...

... but he (or she - I didn't read his Introduction thread) has a rep power of 1, and you have a rep power of 50, so... I'm not too fussed. :D
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 31 May 2008 - 04:32 AM

Well.. I got like 5 or so reps with the power of 0, so should I start whining because I didn't receive any rep points although they gave me +rep? Nope... just live with it!

My articles in the security forum are over 1000 words long.. and I don't receive any +rep for them (since the only ones I receive are of power 0) I don't care. I write tutorials because I know I can give the community something, not because of a +rep!

But still... quite a good and basic tutorial. +/- rep from me :) so that evens out.. so basically.... I shouldn't even press the button :)

Everyone happy!
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 31 May 2008 - 08:17 AM

Well.. I got like 5 or so reps with the power of 0

I really feel for you... :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download