Basic for
Loop Practice
Complete the following exercises related to for
loops.
Exercise 1: Bugs
Visit each of the following Repls. Start by trying to run the programs, and make a note of any error messages received. Then, fork the Repl and try to fix the code!
Exercise 2: Rocket Launch Countdown
Create a program that uses a for
loop to count down the numbers from 10
to 1
, then says "Lift off!"
- Create a new project in Repl
- In the body of the
Main
method, print out a message that says "Rocket Launch" - Under that, create a
for
loopfor () { }
- For the first part of the
for
loop (initializer), create anint i
variable and set it to10
for (int i = 10;) { }
- For the next part (conditional), check if
i
is greater than0
for (int i = 10; i > 0;) { }
- For the final part (modifier), decrease the value of
i
by1
withi--
for (int i = 10; i > 0; i--) { }
- In the body of the
for
loop (between the brackets), print the value ofi
Console.WriteLine(i);
- Under the
for
loop, print a message that says "Lift off!"
Run the program, and verify that it counts down and says "Lift off!" at the end.
Bonus: Slow Counter
Right now, all of the countdown numbers appear pretty much at the same time. The computer moves too fast! It is actually possible to slow it down using the Thread.Sleep
method. This will stop program execution for a given amount of time. This can be very helpful for programs that deal with timing of any kind.
- At the top of the file, add
using System.Threading;
- In the body of the
for
loop, make a new line - Add
Thread.Sleep(1000);
to wait for1000
ms (one second) before continuing
Run the program, and make sure the countdown is slowed down!
Exercise 3: Triangle of Zeroes
Create a program that can print a triangle consisting of powers of 10
. It should ask the user how tall the triangle should be, and then print it out. This will be possible using the Math.Pow
function to calculate each power of 10
.
Examples
If the user entered 3
, the program should print:
1
10
100
If the user entered 5
, the program should print:
1
10
100
1000
10000
Note: This will not work for large inputs, because C# will truncate the numeric output
Implementation
Follow the steps below to implement the program.
- Create a new project in Repl
- In the body of the
Main
method, print out a message that says "How tall of a triangle?" - Under that, create a new
int
variable namedheight
- Set the
height
variable toConvert.ToInt32(Console.ReadLine())
- Under that, create a
for
loopfor () { }
- For the first part of the
for
loop (initializer), create anint i
variable and set it to0
for (int i = 0;) { }
- For the next part (conditional), check if
i
is less than theheight
variablefor (int i = 0; i < height;) { }
- For the final part (modifier), increase the value of
i
by1
withi++
for (int i = 0; i < height; i++) { }
- In the body of the
for
loop, create a newdouble
variable namedpower
- Set the
power
variable toMath.Pow(10, i)
- This will calculate the power of
10
for the given index
- This will calculate the power of
- Use
Console.WriteLine
to print out the value of thepower
variable
Run the program, and verify that it properly prints out some triangles!
Exercise 4: Rainbow Road
Create a program that will print out a road that changes colors as it goes. This will be possible using a List
of ConsoleColor
values. Create a new Repl, and remove any code from the body of the Main
method to get started.
The List<ConsoleColor>
First, create a new List
of ConsoleColor
values containing the colors of a rainbow: red, orange, yellow, green, blue, and purple. For convenience, the code for that is here:
List<ConsoleColor> colors = new List<ConsoleColor>()
{
ConsoleColor.Red,
ConsoleColor.DarkYellow,
ConsoleColor.Yellow,
ConsoleColor.Green,
ConsoleColor.Blue,
ConsoleColor.Magenta
};
Make sure to add using System.Collections.Generic;
so the List
works properly!
The Road
Follow the steps below to create the rainbow road.
- Under the
colors
list, create afor
loop that will loop through15
timesfor (int i = 0; i < 15; i++) { }
- In the body of the
for
loop, print out the text| || |
- Run the program, and verify that a colorless road appears in the console
- Back in the
for
loop body, make some new lines above theConsole.WriteLine
statement - At the top of the
for
loop body, create a newint
variable namedcolorIndex
- Set the
colorIndex
variable toi % colors.Count
- This will allow the road to cycle through the colors multiple times
- On the next line, create a new
ConsoleColor
variable namedcurrentColor
- Set the
currentColor
variable to the value in thecolors
list atcolorIndex
ConsoleColor currentColor = colors[colorIndex];
- Under that, set the
Console.ForegroundColor
value tocurrentColor
Console.ForegroundColor = currentColor;
Run the program again, and verify that a rainbow road appears! Feel free to change the length of the road by updating the 15
in the conditional part of the for
loop.