Jump to content

Tutorial: Looping in C#, Part 2

- - - - -

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

#1
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

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:

[HIGHLIGHT=csharp]
foreach (*initialization* in *objectcollection*)
{
//Code goes here.
}
[/HIGHLIGHT]
Here is an example of a foreach loop:

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

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

[HIGHLIGHT=csharp]
while (condition)
{
//Code goes here.
}
[/HIGHLIGHT]
Here is an example (which in this case could easily be done using a for loop):

[HIGHLIGHT=csharp]
int x = 0;
while (x < 10)
{
MessageBox.Show(x.ToString());
x++;
}
[/HIGHLIGHT]
The Do While loop is similiar:

[HIGHLIGHT=csharp]
do
{
//Code goes here.
} while (condition);
[/HIGHLIGHT]
For example:

[HIGHLIGHT=csharp]
int x = 0;
do
{
MessageBox.Show(x.ToString());
x++;
} while (x < 10)
[/HIGHLIGHT]

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!
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
Eh? LogicKills did a -rep on me, because my tutorials are too short! I don't really think that's fair. :(
Jordan said:

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

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yeah - but I spent good time on that tut, and then people start being ungrateful. LogicKills hasn't exactly posted any tutorials yet. :(
Jordan said:

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

#5
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

Xav said:

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.
http://logickills.org
Science - Math - Hacking - Tech

#6
Guest_Jordan_*

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

#7
Xav

Xav

    Writes binary right handed and hex left handed

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

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

#8
Guest_Jordan_*

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

#9
Xav

Xav

    Writes binary right handed and hex left handed

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

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

#10
TcM

TcM

    Writes binary right handed and hex left handed

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

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

TcM said:

Well.. I got like 5 or so reps with the power of 0
I really feel for you... :)
Jordan said:

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