While Loop Additional Exercises
After completing the code-along, complete these exercises.
1. Print Fifty Smiles
Using while loops, it is possible to repeat things a certain number of times. Rather than copying and pasting the same code over and over, a while loop makes it possible to write code once and execute it any number of times.
For this exercise, print out fifty smiley face emoji: 😊
- Create a new C# Repl project
- Name it "Fifty Smiles"
- In the main.cs file, in the
Mainbody, print a welcome message - Under that, declare a new
intvariable namedi - Set the
ivariable to0 - Under that, create a
whileloop structurewhilekeyword- Parentheses
- Curly brackets
- For the
whilecondition (between the parentheses), check ifiis less than50 - For the
whilebody (between the curly brackets), print out a smiley face - Under that, still within the
{and}, incrementiby 1:i++
Run the program, and verify that 50 smiley faces appear!
2. Print Twelve Eggs
Any number of statements can go within the body of a while loop. In this exercise, print out 12 eggs using ASCII art. Each egg can be drawn to the screen with these commands:
Console.WriteLine(" ,''`.");
Console.WriteLine(" / \\");
Console.WriteLine(": :");
Console.WriteLine(": :");
Console.WriteLine(" `.___,'");
Console.WriteLine("");
- Create a new C# Repl project
- Name it "A Dozen Eggs"
- In the main.cs file, in the
Mainbody, print a welcome message - Under that, declare a new
intvariable namedi - Set the
ivariable to0 - Under that, create a
whileloop structurewhilekeyword- Parentheses
- Curly brackets
- For the
whilecondition (between the parentheses), check ifiis less than12 - For the
whilebody (between the curly brackets), use theConsole.WriteLines to print an egg - Under that, still within the
{and}, incrementiby 1:i++
Run the program, and verify that a dozen eggs appear!
3. Annoying Bot
It is also possible to use while loops more dynamically. Instead of repeating a specific number of times, the loop can repeat until any condition is met. For example, a loop could repeat until the user entered a certain phrase.
In this exercise, pester the user until they respond in a very particular way.
- Create a new C# Repl project
- Name it "Annoying Bot"
- In the main.cs file, in the
Mainbody, print a welcome message - Under that, declare a new
stringvariable namedanswer - Set the
answervariable to"" - Under that, create a
whileloop structurewhilekeyword- Parentheses
- Curly brackets
- For the
whilecondition (between the parentheses), check ifansweris equal to"You are not annoying" - In the
whilebody (between curly brackets), add aConsole.WriteLinestatement - Make the statement print out "Am I annoying you yet?"
- Under that, still within the
{and}, create a new line - On that line, set the
answervariable to aConsole.ReadLine() - Under that, use an empty
Console.WriteLineto print out an empty line - Outside of the
whileloop, print out a "Goodbye" message
Run the program, and enter a few responses. Verify that the loop continues forever, unless the message of "You are not annoying" is entered.
4. Rainbow Road Forever
It is also possible to make a while loop that continues forever. Note that an infinite loop can be a serious code error, but it can also be used for a useful purpose.
In this exercise, create a program that infinitely prints out a rainbow road.
- Create a new C# Repl project
- Name it "Rainbow Road"
- In the main.cs file, in the
Mainbody, print a welcome message - Under that, create a
whileloop structurewhilekeyword- Parentheses
- Curly brackets
- For the
whilecondition (between the parentheses), simply puttrue - In the
whilebody (between curly brackets), set the text color to redConsole.ForegroundColor = ConsoleColor.Red;
- Under that, still within the
{and}, add aConsole.WriteLine() - In the
WriteLine, print out"| || |" - Under that, add a second
Console.WriteLineprinting the same thing - Repeat the three lines of code from above, updating the color for each of these colors:
ConsoleColor.DarkYellowConsoleColor.YellowConsoleColor.GreenConsoleColor.CyanConsoleColor.BlueConsoleColor.Magenta
Run the program, and verify that a rainbow road rolls on forever! There should be a total of 21 statements within the while loop body.