Search⌘ K

For loop

Explore the concept of for loops in C#, understanding how to initialize, condition-check, and increment loop variables. Learn how nested loops operate to perform multiple iterations effectively, helping you control the flow of repetitive programming tasks.

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.

Syntax

A for loop is set up like this:

C#
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.

Example

Here’s an ...