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
Mainmethod, print out a message that says "Rocket Launch" - Under that, create a
forloopfor () { } - For the first part of the
forloop (initializer), create anint ivariable and set it to10for (int i = 10;) { } - For the next part (conditional), check if
iis greater than0for (int i = 10; i > 0;) { } - For the final part (modifier), decrease the value of
iby1withi--for (int i = 10; i > 0; i--) { } - In the body of the
forloop (between the brackets), print the value ofiConsole.WriteLine(i); - Under the
forloop, 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
forloop, make a new line - Add
Thread.Sleep(1000);to wait for1000ms (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
Mainmethod, print out a message that says "How tall of a triangle?" - Under that, create a new
intvariable namedheight - Set the
heightvariable toConvert.ToInt32(Console.ReadLine()) - Under that, create a
forloopfor () { } - For the first part of the
forloop (initializer), create anint ivariable and set it to0for (int i = 0;) { } - For the next part (conditional), check if
iis less than theheightvariablefor (int i = 0; i < height;) { } - For the final part (modifier), increase the value of
iby1withi++for (int i = 0; i < height; i++) { } - In the body of the
forloop, create a newdoublevariable namedpower - Set the
powervariable toMath.Pow(10, i)- This will calculate the power of
10for the given index
- This will calculate the power of
- Use
Console.WriteLineto print out the value of thepowervariable
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
colorslist, create aforloop that will loop through15timesfor (int i = 0; i < 15; i++) { } - In the body of the
forloop, print out the text| || | - Run the program, and verify that a colorless road appears in the console
- Back in the
forloop body, make some new lines above theConsole.WriteLinestatement - At the top of the
forloop body, create a newintvariable namedcolorIndex - Set the
colorIndexvariable toi % colors.Count- This will allow the road to cycle through the colors multiple times
- On the next line, create a new
ConsoleColorvariable namedcurrentColor - Set the
currentColorvariable to the value in thecolorslist atcolorIndexConsoleColor currentColor = colors[colorIndex]; - Under that, set the
Console.ForegroundColorvalue tocurrentColorConsole.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.