Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Understanding For Loops | Section
/
C# Loops Practice

bookUnderstanding For Loops

Svep för att visa menyn

Loops are a core part of programming in C#, and in this module we're going to take a deeper dive into how they work — especially the for loop.

The for loop is one of the most commonly used control structures when you need to repeat an action a specific number of times. Its syntax is compact and built around three key parts: initialization, condition, and increment.

for (initialization; condition; increment)
{
    // code to execute
}

In this module, you'll explore how these parts work together, how to control the flow of execution, and how to apply for loops in real scenarios — from working with arrays to building counters and processing structured data efficiently.

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 allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 1. Kapitel 1
some-alt