← Back

Activity 2.9 For Loops


Introduction

C# has a number of different loop constructs built in to the language. The while loop is flexible enough to perform just about any task where iteration is required. However, there are other loops that can perform certain tasks with more efficient code.

The for loop is similar in nature to a while loop, but it has a different organization. There are three parts to a for loop, other than the for keyword and code block:

The following examples can be written with a while loop, but notice how the for loop is concise.

Example: Count up from 1 to 10 with a for loop

for (int i = 1; i <= 10; i++)
{
  Console.WriteLine(i);
}

Example: Count down from 10 to 1 with a for loop

for (int i = 10; i >= 1; i--)
{
  Console.WriteLine(i);
}

Example Video

Skills to Practice


Instructions

Setup

  1. Create a new C# console application. Name the project 2.9 For Loops.
  2. Click Create.

Code

Console.WriteLine("--- Multiples ---");

Console.Write("Choose a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = number; // Sum keeps track of current multiple

Console.Write($"How many multiples of {number} do you want listed? ");
int length = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < length; i++)
{
    Console.Write(sum + " "); // Count horizontally
    sum = sum + number;
}

Console.WriteLine("\nPress Enter to continue.\n");
Console.ReadLine();

Console.WriteLine("--- Exponents ---");

Console.Write("Choose a base number: ");
number = Convert.ToInt32(Console.ReadLine());
int product = number;

Console.Write("How many products do you want listed? ");
length = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= length; i++)
{
    Console.WriteLine($"{number}^{i} = {product}");
    product = product * number;
}

Debug

--- Multiples ---
Choose a number: 5
How many multiples of 5 do you want listed? 5
5 10 15 20 25
Press Enter to continue.


--- Exponents ---
Choose a base number: 10
How many products do you want listed? 5
10^1 = 10
10^2 = 100
10^3 = 1000
10^4 = 10000
10^5 = 100000

Tips, Tricks, and Reflection