One of the things that you will do a lot in programming is repeat yourself… repeat yourself…. repeat yourself…. see what I did there? He he he I am very funny.
Anyways, the fact is that you will repeat a lot of the code that you wrote. Take the lines of code below for example:
Debug.Log("Something to debug");
Debug.Log("Something to debug");
Debug.Log("Something to debug");
Debug.Log("Something to debug");
Debug.Log("Something to debug");
Instead of writing Debug.Log 5 times, or more depending on your needs, you can use a loop do that for you.
The Anatomy Of A Loop
The structure of a loop goes like this:
for(int i = 0; i < 5; i++)
{
// code that you want to execute
}
This is called a for loop, hence the opening for, the int i = 0 is called an iterator e.g. a variable that we will use to control how many times the for loop will loop, i < 5 means this loop will loop 5 times, and i++ means that we are incrementing the value of i after every loop:

To make it more simple, think of the code between the () as the condition for the loop.
So we have a variable i, and as long as i is less than the specified value, in this case 5, the loop will loop, and when it finishes it will increment the current value of i by 1 until the value of i gets to the point where the condition is no longer true and the loop will stop looping.
To demonstrate this, just add the following line of code inside the for loop:
Debug.Log("The value of i is: " + i);
When you run the game this is what you will see in the Console:

As you can see the value of i goes from 0 up to 4 which means in the first iteration i has a value of 0, then when the loop finishes it will increase the value of i by 1 with the ++, after that the i has a value of 1, and then the process repeats.
When i gets to the point where the condition of i < 5 is no longer true, the loop will stop executing, that’s why the last value of i is 4 because when i gets to the value of 5 then the condition i < 5 is no longer true because 5 is not less than 5.
Of course, naming the variable i is not mandatory, you can name it g, t, u, Carl, Kenny, or whatever name you want, but i is a shortcut for iteration, that’s why you will see i is the most common name for the iterating variable.
While Loop
A for loop is not the only loop that we have at our disposal, there is also a while loop that goes like this:
while(condition)
{
// code to execute
}
The structure of a while loop is a little bit different but the purpose is the same. while denotes the opening of the loop, the condition between the parenthesis needs to be true so that the loop will execute the code.
If we were to rewrite the example with the for loop by using an integer it would go like this:
int i = 0;
while(i < 5)
{
Debug.Log("The value of i is: " + i);
i++;
}
Where To Use For And Where To Use While Loop?
This is a question that a lot of people ask me while they are following a lecture about loops.
The short answer is we use loops for arrays and when we want to repeat a certain part of code that we have.
The long answer is with more examples that we will see in the upcoming lectures we will see a practical use of loops with a real world example and that’s when things will be even more clear.
One thing we need to understand is that programming is like that, the more you practice and the more lines of code you write and think about ways to solve problems the better you get and the more you understand how programming works.
So for now, we need to be satisfied with these basic examples of loops, but later down the road when we learn more things, then we will see practical examples in real projects.
Where To Go From Here
In this lecture you learned how to use loops to repeat your code multiple times with little effort.
To continue your learning journey you can check out our Introduction To Arrays Lecture which is the next lecture in this tutorial series.
127 thoughts on “C# Programming With Unity – Loops”
Your article helped me a lot, is there any more related content? Thanks!
I have been absent for some time, but now I remember why I used to love this website. Thanks, I?¦ll try and check back more often. How frequently you update your web site?
Great write-up, I’m normal visitor of one’s site, maintain up the excellent operate, and It is going to be a regular visitor for a long time.
sex nhật hiếp dâm trẻ em ấu dâm buôn bán vũ khí ma túy bán súng sextoy chơi đĩ sex bạo lực sex học đường tội phạm tình dục chơi les đĩ đực người mẫu bán dâm
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Hey there, You have done a fantastic job. I will certainly digg it and personally suggest to my friends. I’m confident they’ll be benefited from this website.
Nagano Lean Body Tonic: An IntroductionNagano Lean Body Tonic is a dietary supplement designed to help lose unhealthy weight
I think this internet site has got some real great info for everyone : D.
Hello, I think your site might be having browser compatibility issues.
When I look at your website in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up!
Other then that, fantastic blog!
Excellent beat ! I wish to apprentice while you amend your web site, how could i subscribe for a blog site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea
Hey there just wanted to give you a quick heads up.
The words in your article seem to be running off the screen in Internet explorer.
I’m not sure if this is a format issue or something to
do with web browser compatibility but I figured I’d post to let you know.
The design and style look great though! Hope you get the issue resolved soon. Many thanks
My brother recommended I might like this website. He was totally
right. This post actually made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!
darknet marketplace darkmarket 2025
information.|My family members every time say that I am killing my time here
You ought to take part in a contest for one of the highest quality websites on the internet. I’m going to recommend this site!
Thanks for your marvelous posting! I seriously enjoyed reading it, you might be a great author.I will always bookmark your blog and definitely will come back later in life. I want to encourage continue your great job, have a nice afternoon!
Your article helped me a lot, is there any more related content? Thanks!
This is a topic which is close to my heart… Best wishes! Exactly where can I find the contact details for questions?
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
I have been surfing online more than 3 hours today yet I never found any interesting article like yours It is pretty worth enough for me In my opinion if all web owners and bloggers made good content as you did the web will be much more useful than ever before
Thanks for sharing your thoughts about sex trẻ em. Regards
He was employed at Corson Packaging in Middletown for more than 30 years and was a member of the Moose Lodge in Middletown.
The crux of your writing while appearing reasonable initially, did not work well with me after some time. Someplace within the sentences you actually were able to make me a believer but only for a very short while. I still have got a problem with your leaps in assumptions and you might do well to help fill in those breaks. In the event that you can accomplish that, I could undoubtedly end up being impressed.
darknet sites darknet site
dark web market darkmarket list
Your article helped me a lot, is there any more related content? Thanks!
dark markets 2025 darkmarkets
darknet links darknet drug links
darknet markets onion darknet drug links
darkmarket 2025 darknet market links
darknet markets 2025 best darknet markets
darkmarkets darkmarket list
darkmarket dark market link
darkmarket link darknet market
bitcoin dark web darknet drug links
dark web market list darkmarket 2025
darknet drug market dark web marketplaces
tor drug market darknet markets 2025
darknet markets links dark websites
best darknet markets darknet markets
dark web markets dark web markets
darknet markets 2025 darkmarket list
dark web markets darknet drug links
dark web drug marketplace darknet markets onion
darknet market list bitcoin dark web
dark websites darknet markets links
dark market url darknet marketplace
darkmarket list darknet market lists
dark web market list dark web market links
dark market 2025 dark web markets
darknet markets onion address darkmarket link
tor drug market bitcoin dark web
dark web market darknet market lists
dark web market darkmarket url
darknet marketplace dark websites
tor drug market dark market link
darknet markets 2025 darknet markets links
darknet drug store darknet site
tor drug market darknet markets
darkmarket darknet market links
darknet links onion dark website
dark web markets darknet websites
dark markets onion dark website
darknet websites dark markets
dark web market urls onion dark website
darknet marketplace darknet market links
darkmarket link dark web market list
dark web link dark web sites
darknet drug market darknet markets
darknet markets links darkmarket
darknet marketplace dark web link
dark market 2025 dark market link
darknet drug market dark web marketplaces
darknet drug market darknet site
darknet markets links darknet sites
darkmarket dark websites
darknet marketplace darknet drugs
dark web market list dark market url
darknet markets 2025 dark markets 2025
darkmarket best darknet markets
darknet drug store darknet links
dark market link tor drug market
dark web drug marketplace darkmarket link
darkmarket url dark web link
darknet markets onion dark web market urls
dark web market dark market list
darkmarket url dark market link
darknet market lists dark websites
dark market url darknet markets links
dark markets 2025 darknet market lists
dark web market urls darkmarket 2025
dark web marketplaces bitcoin dark web
darknet drug links bitcoin dark web
bitcoin dark web dark web marketplaces
dark market darknet markets onion address
darknet market links bitcoin dark web
darknet markets links dark market link
darkmarket 2025 dark market
darknet links dark market list
best darknet markets darkmarket link
darknet marketplace darknet sites
darknet market lists darknet markets onion
darknet markets 2025 darkmarket link
dark web market darkmarkets
dark market darknet websites
darkmarket 2025 dark web drug marketplace
dark market list dark market list
dark web markets dark web link
darkmarket darknet marketplace
vykřiknout a říct, že mě opravdu baví číst vaše příspěvky na blogu.
bitcoin dark web darknet market list
darknet site darkmarket list
darknet marketplace best darknet markets
dark markets 2025 darknet marketplace
darknet markets url darknet markets onion address
dark web sites darknet drugs
tor drug market darknet markets onion
darknet sites dark web market list
darknet marketplace darkmarket link
dark markets 2025 onion dark website
darkmarkets dark markets 2025
dark web market urls onion dark website
dark web market urls bitcoin dark web
darkmarket list dark market
darknet drug store darknet drug market
darknet site darknet market links