Understanding For Loops
A for loop in C# is a fundamental control structure that allows you to repeat a block of code a specific number of times. The syntax is compact and consists of three main parts: initialization, condition, and increment.
for (initialization; condition; increment)
{
// code to execute
}
These parts work together to control the flow of the loop. You use for loops for tasks where you know in advance how many times you want to repeat an action, such as printing numbers, processing arrays, or creating simple counters.
Program.cs
1234567891011121314namespace ConsoleApp { public class Program { public static void Main() { // Print numbers 1 to 5 for (int i = 1; i <= 5; i++) { System.Console.WriteLine(i); } } } }
The structure of a for loop includes three key components inside the parentheses:
- Initialization: sets up the loop control variable, usually executed once at the start;
- Condition: checked before each loop iteration; if true, the loop continues, otherwise it stops;
- Increment: updates the loop control variable after each iteration.
Each part directly affects how the loop executes. For instance, changing the initialization or condition changes the starting or ending point, and modifying the increment changes how the control variable progresses through each loop.
Program.cs
1234567891011121314151617181920namespace ConsoleApp { public class Program { public static void Main() { // Counting by 2s from 0 to 10 for (int i = 0; i <= 10; i += 2) { System.Console.WriteLine(i); } // Counting down from 5 to 1 for (int i = 5; i >= 1; i--) { System.Console.WriteLine(i); } } } }
You use an increasing for loop when you need to count up, such as iterating through array indexes or generating a sequence in order. A decreasing for loop is useful for countdowns or when you need to process items in reverse. Custom step values are helpful when you want to skip values, such as printing every second number or processing every nth item in a list. Choosing the right form of the for loop depends on the pattern of repetition you need in your program.
1. Which part of a for loop controls how many times the loop will run?
2. Which of the following is the correct syntax for a for loop that counts down from 10 to 1?
3. When would you use a custom step value in a for loop?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 5.56
Understanding For Loops
Veeg om het menu te tonen
A for loop in C# is a fundamental control structure that allows you to repeat a block of code a specific number of times. The syntax is compact and consists of three main parts: initialization, condition, and increment.
for (initialization; condition; increment)
{
// code to execute
}
These parts work together to control the flow of the loop. You use for loops for tasks where you know in advance how many times you want to repeat an action, such as printing numbers, processing arrays, or creating simple counters.
Program.cs
1234567891011121314namespace ConsoleApp { public class Program { public static void Main() { // Print numbers 1 to 5 for (int i = 1; i <= 5; i++) { System.Console.WriteLine(i); } } } }
The structure of a for loop includes three key components inside the parentheses:
- Initialization: sets up the loop control variable, usually executed once at the start;
- Condition: checked before each loop iteration; if true, the loop continues, otherwise it stops;
- Increment: updates the loop control variable after each iteration.
Each part directly affects how the loop executes. For instance, changing the initialization or condition changes the starting or ending point, and modifying the increment changes how the control variable progresses through each loop.
Program.cs
1234567891011121314151617181920namespace ConsoleApp { public class Program { public static void Main() { // Counting by 2s from 0 to 10 for (int i = 0; i <= 10; i += 2) { System.Console.WriteLine(i); } // Counting down from 5 to 1 for (int i = 5; i >= 1; i--) { System.Console.WriteLine(i); } } } }
You use an increasing for loop when you need to count up, such as iterating through array indexes or generating a sequence in order. A decreasing for loop is useful for countdowns or when you need to process items in reverse. Custom step values are helpful when you want to skip values, such as printing every second number or processing every nth item in a list. Choosing the right form of the for loop depends on the pattern of repetition you need in your program.
1. Which part of a for loop controls how many times the loop will run?
2. Which of the following is the correct syntax for a for loop that counts down from 10 to 1?
3. When would you use a custom step value in a for loop?
Bedankt voor je feedback!