Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

C# Control Structures / For Loop




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Now let's talk about the For Loop. The For Loop differs from the While Loop or the DoWhile Loop in one simple respect and I've left these on the screen here so you can see them and we're going to clear them off in just a second but a While Loop is going to run as long as A is less than 4. The DoWhile Loop will run as long as the condition is true or A is less than 4. It just checks the condition at the end rather than the beginning. Let's delete these and let's talk about a For Loop. Now, what's the deal with a For Loop? Well, a Four Loop is going to allow us to loop through anything a certain number of times and we pick how many times that is. The simple syntax on this is we're going to say, let me just type this out because this gets a little involved. We're going to say For and then we'll open our parentheses and then we will initialize our variables that we're going to use as our counter, OK, our initialize expression, OK. Then we'll put a semicolon, then we'll put the Boolean expression. OK, what we're actually trying to check or what we're looking for as far as a true false condition. So we'll put our Boolean expression here. We'll put another semicolon, OK and then we'll put our increment or decrement and so we will determine what we want to do with our looping number here and then we will come down here and we will open a block of code with our braces and then we will put our statements inside here, whatever we want to have happen. So let's talk about what that looks like. OK? Let's just delete this and let's say For and we're going to create an integer A and set it equal to 3 and this is the same thing. That's just a standard integer declaration line there so A equals 3 and now we're going to say the Boolean check is as long as A is greater than 1, wait a minute. As long as A is greater than 1, keep going and let's increment A by 1 every time you go through the loop. OK? Now if we open our brace and start to type, we're going to do our same old example here. I'm going to say A equals and I'll put a space, close that literal, concatenate it with Convert to String A. Now, I need to close that, close my line with a semicolon and close my brace. Then we're going to put a message box and I can't type and talk. I know I've said that three million times. I'm sorry. And we'll just say Out of Loop here. So we can tell when we've actually fallen through and gotten out of the loop. OK? Notice we're saying OK, A equals 3. Keep looping through here as long as A is equal than 1 and keep adding to it. Does anybody see a problem here? Because I actually fat-fingered something here. What I actually want to do instead of A is greater than 1, I know what I'm thinking, as long as A is less than 6, we will continue to loop through and every time we go through, A is going to go up by one. So let's just run this and see if that is indeed what happens. So I click it. Notice A is equal to 3 like we originally said and let me do something here. Let me do something here that makes a little more sense. Let me run this. How about this? I'll pull it out of the way. A is equal to 3 just like it did and it shows us a message box. We click OK. It loops through, it adds one right here to A, OK? And A is now equal to 4. We click it again, A is equal to 5. We click it one more time, A is equal to 6 which means it's not greater than 6 and it's out of the loop, which means that the difference between 3 and 6, we told this thing to loop through three times. OK? If I drop this to 1, woops, I've got to break out here. If I drop A to 1, now it's going to loop through five times. So now A is 1, 2, 3, 4, 5. The next time we're out of the loop. OK? Now, notice if I say as long as A is greater than 6, alright, run this and when I click on OK, we instantly go out of the loop. Why? Because it checks the condition. It wasn't true and so it skipped all this and we went straight to the message box. We're out of the loop. The big deal here is by controlling our variable, variable right here and the condition, we control the number of times this loops and that's the big difference. A While Loop or a DoWhile Loop is going to loop until a certain condition is true and we might not be able to control that condition and with this one, by setting our variable and setting the condition here and using the For Command, we can set the number of times that it loops and also, a lot of people like and this is one of those advantages of C# over VB that you'll hear about. It's very concise, very tight code. Not a lot of verbage here to make all this stuff happen. So anyway, that's a For Loop; very simple example. Get your head around this and you can do some cool things with this with your applications.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available