Locked lesson.
About this lesson
We learn how to create and use For Loops within our program.
Exercise files
Download this lesson’s related exercise files.
37 - Loops For Loops.docx60.9 KB 37 - Loops For Loops SOLUTION.docx
59 KB
Quick reference
Loops: For Loops
For loops allow you to loop FOR a certain number of times.
When to use
Use these when you know exactly how many times you need to loop.
Instructions
for (int i = 1; i<=10; i++)
{
Console.WriteLine(i);
}
Hints & tips
- For loops loop FOR a certain number of times.
- 00:04 In the last video, we looked at while loops,
- 00:06 in this video I want to look at for loops.
- 00:08 And for loop is just another type of loop.
- 00:10 The syntax is a little bit different than the while loop, but
- 00:12 essentially we're going to do the same thing.
- 00:14 We're looping through a thing over and over again until some condition is met and
- 00:18 then we stop.
- 00:19 So I can't say which is more popular while loops or for loops.
- 00:23 I tend to use for loops a little bit more often than while loops.
- 00:26 It's really just a matter of personal preference and style.
- 00:29 Like I said, you're going to develop your own feel for these things and
- 00:32 you might like one of these types of loops more than the other.
- 00:35 Doesn't matter at all which ones you use.
- 00:37 Whichever ones you are more comfortable with that make more sense in your specific
- 00:41 code, you can go ahead and use.
- 00:43 So like I said, in this video, we're going to look at the for loop.
- 00:45 Very similar to the while loop, except we're going to define everything
- 00:49 at the top of the loop and we don't need a counter.
- 00:51 Well, we need a counter, but it will be defined at the top loop.
- 00:54 Let me show you.
- 00:55 So let's build one of these out.
- 00:56 So we start out with for, so this is a for loop.
- 01:00 And then inside of here, we're going to need three things.
- 01:03 We need sort of to define our counter.
- 01:05 So that's going to be int, and then let's just call this i, or we could call it x.
- 01:11 But i is sort of common in for loops.
- 01:14 So i =, and then what?
- 01:17 Let's start out with 1, all right?
- 01:19 So this can be all together like this, you could put spaces.
- 01:23 Personally, I like just cramming it all together.
- 01:26 So now we need to define our condition.
- 01:28 What are we searching for?
- 01:30 What are we looking for?
- 01:31 So let's say i less than or equal to 10, right?
- 01:36 And you notice I'm separating each of these with a semi-colon.
- 01:39 Some programming languages you separate them with commas,
- 01:42 in C-Sharp we use a semicolon.
- 01:44 So now we need to define our increment or decrement, so I want to go i + +.
- 01:48 Every single time we loop through this, we want to add 1, right?
- 01:52 So we're saying hey start out at 1.
- 01:55 Keep testing this, keep looping until this condition is met,
- 01:59 i is less than or equal to 10.
- 02:01 And every time we loop add 1, right?
- 02:04 So that's all there is to it.
- 02:07 Now you'll notice this is all sort of defined right here at the top.
- 02:10 And inside of here, we can do whatever we want.
- 02:12 So let's just console write out again and let's just print i to the screen, right?
- 02:20 So notice we don't have to add our counter anywhere
- 02:24 inside of this block like we do with the while loop.
- 02:27 Remember with the while loop, we had to increment our counter inside of our loop.
- 02:31 Here, we're not doing that because we've predefined it up here.
- 02:34 The program already knows, hey, every time we loop through here, add 1.
- 02:37 So if we go ahead and save this and run it, we see we get the same thing.
- 02:42 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
- 02:44 Our condition is met and it stops, right?
- 02:47 And that's really all there is to it.
- 02:49 So we can do the same thing in reverse.
- 02:52 So we could say, int i = 10.
- 02:55 In our condition now we want it to be greater than or equal to 1.
- 03:02 And here we want to decrease, we want to decrement, right?
- 03:05 Same thing here, we want to print out i to the screen.
- 03:07 So if we save this and run it, we get the same thing in reverse 10, 9, 8,
- 03:12 7, 6, 5, 4, 3, 2, 1.
- 03:15 And then boom, it ends.
- 03:17 If we play around with this, we don't have to use or equal to,
- 03:20 we could just say as long as i is greater than 1.
- 03:22 If we save this and run it.
- 03:25 We're not going to get that last 1, right?
- 03:28 It starts at 10 goes down to 3, 2, 1.
- 03:30 So let's say we're at 2, 2 is greater than 1, right?
- 03:33 So we print this to the screen 2, then we decrease 1, now becomes 1.
- 03:38 Now, we loop through again.
- 03:40 Now 1 is not greater than 1, right?
- 03:44 So this whole thing stops, the 1 doesn't get printed.
- 03:47 So if you want your 1 printed at the end, add your equal to sign.
- 03:51 Now obviously, we're just printing this onto the screen.
- 03:55 We can do anything we want in here.
- 03:56 We could say The Number is and
- 04:00 then just concatenate this on.
- 04:05 We save this and run it.
- 04:08 The number is 10, 9, 8, 7, 6 as you want it.
- 04:11 So like I said, we can do anything we want in this block of code right here.
- 04:14 We can have one line, we can have 1000 lines, it doesn't matter.
- 04:17 We can run a function in here.
- 04:20 If we had a something function and
- 04:22 we could have that run every time it loops through.
- 04:26 Anything you want to do inside of this block you can and
- 04:29 it will just keep looping until your conditions are met for
- 04:33 it to break out of the loop, for the loop to end.
- 04:35 So that's a quick introduction to loops, for loops and while loops.
- 04:39 You'll use these things forever, all the time.
- 04:41 They are very fundamental to all programming loops, and are pretty simple.
- 04:45 In the next video, we'll look at breaking and continuing out of loops.
Lesson notes are only available for subscribers.