For Loop C# with Example



For Loop C# with Example

A For Loop is great for doing things a certain amount of time. It's like a While Loop but the increment is included 
with the condition. 
A For Loop is set up like this: 
for (Initialization; Condition; Increment) 
{ 
// Code 
} 
Initialization - Makes a new local variable that can only be used in the loop. 
Condition - The loop only runs when the condition is true. 
Increment - How the variable changes every time the loop runs. 
An example: 
for (int i = 0; i < 5; i++) 
{ 
Console.WriteLine(i); 
} 
Output: 
0 
1 
2 
3 
4 
You can also leave out spaces in the For Loop, but you have to have all semicolons for it to function. 
int input = Console.ReadLine(); 
for ( ; input < 10; input + 2) 
{ 
Console.WriteLine(input); 
} 
Output for 3: 
3 
5 
7 
9 
11 
 

0 Comment's

Comment Form

Submit Comment