Skip to content
Sign upLog in

C# for loops

Profile icon

In C# you will want to use for looping to make programming easier to do. This is how you will make a for loop to go through a number.

for (int i = 0; i < 10; i++){ Console.WriteLine(i); }

Use the keyword 'for' to start the for loop. Then encapsulate its contents in parentheses like a method. after that you need to initialize an interger value and terminate it by semicolons like done in the example. Then state what the for loop should last for. I have made it to where it will last while i is less than 10. Terminate that with a semicolon and move on. Then you need to increment what you are going through. adding '++' by a number value in C# increments that value by 1. So this value will go up by 1 every time the for loop loops. Then in curly brakets you contain what you actually want to loop. Here i have made the console write out the value of the i variable.
It will have the output of this

0 1 2 3 4 5 6 7 8 9

If you have any questions ask me.
Thank you for reading.

You are viewing a single comment. View All
Profile icon

I use it with unity for game development