Lost Password?


  #1 (permalink)  
Old 05-22-2008, 12:37 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 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!
__________________


Mr. Xav | Website | Forums | Blog

Last edited by Xav; 05-22-2008 at 12:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-24-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#

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


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-24-2008, 08:03 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#

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!
__________________
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
  #4 (permalink)  
Old 05-25-2008, 04:18 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#

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-25-2008, 01:53 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#

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


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

Sponsored Links
  #6 (permalink)  
Old 05-25-2008, 02:52 PM
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#

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-25-2008, 03:01 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#

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


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-25-2008, 03:02 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#

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


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-25-2008, 03:13 PM
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#

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-25-2008, 03:16 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#

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


Mr. Xav | Website | Forums | Blog
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial - ListBox, ComboBox & Command button. travy92 VB Tutorials 9 07-06-2008 12:48 AM
Java tutorial : my sql with java Arkie Java Tutorials 2 04-05-2008 01:51 PM
CodeCall Tutorial Contest #4 Jordan Announcements 29 02-25-2008 12:25 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 04:05 PM


All times are GMT -5. The time now is 12:16 PM.