Lost Password?


  #1 (permalink)  
Old 05-27-2008, 09:50 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Tutorial: Looping in C#, Part 2

Looping in C# - Part 2
By Xav

Following on from my other tutorial, http://forum.codecall.net/c-tutorial...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:

csharp Code:
  1. foreach (*initialization* in *objectcollection*)
  2. {
  3.  //Code goes here.
  4. }
Here is an example of a foreach loop:

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

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

csharp Code:
  1. while (condition)
  2. {
  3.  //Code goes here.
  4. }
Here is an example (which in this case could easily be done using a for loop):

csharp Code:
  1. int x = 0;
  2. while (x < 10)
  3. {
  4.  MessageBox.Show(x.ToString());
  5.  x++;
  6. }
The Do While loop is similiar:

csharp Code:
  1. do
  2. {
  3.  //Code goes here.
  4. } while (condition);
For example:

csharp Code:
  1. int x = 0;
  2. do
  3. {
  4.  MessageBox.Show(x.ToString());
  5.  x++;
  6. } 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!
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-27-2008, 03:17 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Tutorial: Looping in C#, Part 2

Eh? LogicKills did a -rep on me, because my tutorials are too short! I don't really think that's fair.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-28-2008, 01:38 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,635
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Tutorial: Looping in C#, Part 2

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-28-2008, 04:29 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Tutorial: Looping in C#, Part 2

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


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-28-2008, 06:27 PM
LogicKills's Avatar   
LogicKills LogicKills is offline
Learning Programmer
 
Join Date: May 2008
Location: You Es Of Eh
Posts: 96
Rep Power: 4
LogicKills has a spectacular aura aboutLogicKills has a spectacular aura aboutLogicKills has a spectacular aura about
Send a message via AIM to LogicKills
Default Re: Tutorial: Looping in C#, Part 2

Quote:
Originally Posted by Xav View Post
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
Daily Dose Of Knowledge.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-28-2008, 09:47 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-29-2008, 02:29 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Tutorial: Looping in C#, Part 2

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...
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-29-2008, 02:32 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Tutorial: Looping in C#, Part 2

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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-29-2008, 02:36 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Tutorial: Looping in C#, Part 2

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.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-31-2008, 08:32 AM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 8,332
Rep Power: 68
TcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of light
Default Re: Tutorial: Looping in C#, Part 2

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump