For Loops in C#

Jonathan Weinberger
A free video tutorial from Jonathan Weinberger
Authorized Unity Instructor
4.5 instructor rating • 10 courses • 91,038 students

Learn more from the full course

The Unity C# Survival Guide

Created in partnership with Unity Technologies: Master C# with Unity in this Complete Guide!

12:21:51 of on-demand video • Updated March 2019

  • Master C# with Unity
  • Beginner C# concepts (variables, if-statements, methods, etc.)
  • Intermediate C# Concepts (lists, dictionaries, abstract classes, etc)
  • Advanced C# Concepts (delegates, events, lambda expressions, design patterns, etc)
  • Interview Prep
  • Unity quick tips and challenges
  • How to program from scratch
English [Auto] The first loop that we're gonna take a look at today is called the for loop. So to get started with a for loop. Let's imagine we were creating a program where you wanted to print your name a hundred times. Now in order for us to print our name a hundred times we need to define what our name is doesn't exist means to tell the program what our name is. So here we'll have a public string for my name and we'll set it to John imagine I want to print this out a hundred times. Well currently what I could do is I could say debug that log and I could just print out my name and then what I can do is I can copy this and basically just print it a hundred times right. But now imagine you had to print out your name ten thousand times does it make sense to copy this and paste ten thousand times. Right. So this is where we get into looping. So we want to perform this task more than once. So what we can do is use a for loop so for loop looks like this. We use the for keyword with an open closed parentheses and a pair of curly braces within the for loop. We need to define how long this loop is to run for. So what we do is we create what's called an index variable that's going to start within it. So here we say I equals zero. Now what most people get caught up in is this i equals zero part. This is just a variable. The standard in C sharp is to use i as the syntax. However I can name this to Bob to James I can be whatever I want it to be it's just a variable name and it's an int value that basically is an index. So here int i equals zero it lets me know that we're starting the loop and I'm keeping track of how many iterations it's running from this index variable so for int i equals zero. And then what I'm doing is while i is less than some max value and we define what that max value is while i is less than this value we're going to continue looping and once we run the code we need to then increment i. So we'll say I plus plus. So imagine we wanted to print out our name a hundred times inside the for loop would go the code to print out our name and we would say my name and then we specify how many times if I want to print it out a hundred times it goes here. So let's take a look at what this code is doing. For int i equals zero. Which is our index i is less than one hundred. So I zero. It's a question is zero less than one hundred. Yes. We're going to run the code and then we're going to increment i i is now 1. So is one less than a hundred. Yes we're going to run the code then increment i i is now to it's to less than a hundred and we do this all the way until we get to ninety nine. So imagine I equals ninety nine is ninety nine less than 100. Yes we print out my name and then we increment i i equals one hundred now is one hundred less than one hundred. No. So we're done with the loop and we move on with our program. So here this would get called when our loop is finished loop has finished let's save this and check it out so I'm going to run the editor My name is John here and the inspector. And if we look in the console you'll see here that John was printed out one hundred times. If I unkillable this you can see all the times it printed out. And as soon as the Loop finished loop has finished was called. So again let's say we only want to print out my name two times or save it. So while I had less than 2 so 0 and 1 Well go ahead and rerun this and you'll see here that my name was printed out twice followed by lupus finished. So that's the syntax for our for loop. The next video we're going to take a look at some functionality that we can do with for loops and get into some challenges.