Skip to content
Sign upLog in
← Back to Community

C# for loops

Profile icon
Jakman

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.

Voters
Profile icon
ce19b005
Profile icon
Raul8
Profile icon
Jakman
Comments
hotnewtop
Profile icon
pricklycapital

Btw this is a unity script: geometry dash
transform.Translate(Vector3.right * 8 * Time.deltaTime);

Profile icon
08KINPLIX

:) C# is the first programming language is learned

Profile icon
Jakman

@08KINPLIX
cool. It is the second language I learned.

Profile icon
08KINPLIX

@Jakman
I use it with unity for game development

Profile icon
08KINPLIX

@Jakman
My Icon and Pc background is the c# logo

Profile icon
Jakman

@08KINPLIX
yeah i noticed. Cool. You wanna make a C# program together?

Profile icon
08KINPLIX

@Jakman
Yes :) what do you want it to be about

Profile icon
Jakman

@08KINPLIX
I was thinking of a math or scientific formula. What formula do you think it should be?

Profile icon
08KINPLIX

@Jakman
Prime Numbers and Composite if thats a formula

Profile icon
Jakman

@08KINPLIX
Ok then. How do i tell which is which?

Profile icon
08KINPLIX

@Jakman
A prime number is a whole number that only has two factors which are itself and one. A composite number has factors in addition to one and itself. The numbers 0 and 1 are neither prime nor composite. All even numbers are divisible by two and so all even numbers greater than two are composite numbers.

Profile icon
Jakman

@08KINPLIX
Ok cool. I will start a program and invite you to it.

Profile icon
08KINPLIX

@Jakman
BTW dont expect me to be amazing at c# I use mainly for game development :)

Profile icon
08KINPLIX
Profile icon
Jakman

@08KINPLIX
no problem. It cant be that hard :)

Profile icon
08KINPLIX

@Jakman
btw this is a unity script:
transform.Translate(Vector3.right * 8 * Time.deltaTime);

Profile icon
ChezCoder

Very nice!

Profile icon
Jakman

@ChezCoder
thanks man