Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Understanding For Loops | For Loops: Fundamentals and Applications
C# Loops Practice

bookUnderstanding For Loops

Prerequisites
Forudsætninger

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

Program.cs

copy
1234567891011121314
namespace 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

Program.cs

copy
1234567891011121314151617181920
namespace 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?

question mark

Which part of a for loop controls how many times the loop will run?

Select the correct answer

question mark

Which of the following is the correct syntax for a for loop that counts down from 10 to 1?

Select the correct answer

question mark

When would you use a custom step value in a for loop?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 5.56

bookUnderstanding For Loops

Stryg for at vise menuen

Prerequisites
Forudsætninger

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

Program.cs

copy
1234567891011121314
namespace 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

Program.cs

copy
1234567891011121314151617181920
namespace 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?

question mark

Which part of a for loop controls how many times the loop will run?

Select the correct answer

question mark

Which of the following is the correct syntax for a for loop that counts down from 10 to 1?

Select the correct answer

question mark

When would you use a custom step value in a for loop?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
some-alt