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
Main
body, print a welcome message - Under that, declare a new
int
variable namedi
- Set the
i
variable to0
- Under that, create a
while
loop structurewhile
keyword- Parentheses
- Curly brackets
- For the
while
condition (between the parentheses), check ifi
is less than50
- For the
while
body (between the curly brackets), print out a smiley face - Under that, still within the
{
and}
, incrementi
by 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
Main
body, print a welcome message - Under that, declare a new
int
variable namedi
- Set the
i
variable to0
- Under that, create a
while
loop structurewhile
keyword- Parentheses
- Curly brackets
- For the
while
condition (between the parentheses), check ifi
is less than12
- For the
while
body (between the curly brackets), use theConsole.WriteLine
s to print an egg - Under that, still within the
{
and}
, incrementi
by 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
Main
body, print a welcome message - Under that, declare a new
string
variable namedanswer
- Set the
answer
variable to""
- Under that, create a
while
loop structurewhile
keyword- Parentheses
- Curly brackets
- For the
while
condition (between the parentheses), check ifanswer
is equal to"You are not annoying"
- In the
while
body (between curly brackets), add aConsole.WriteLine
statement - 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
answer
variable to aConsole.ReadLine()
- Under that, use an empty
Console.WriteLine
to print out an empty line - Outside of the
while
loop, 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
Main
body, print a welcome message - Under that, create a
while
loop structurewhile
keyword- Parentheses
- Curly brackets
- For the
while
condition (between the parentheses), simply puttrue
- In the
while
body (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.WriteLine
printing the same thing - Repeat the three lines of code from above, updating the color for each of these colors:
ConsoleColor.DarkYellow
ConsoleColor.Yellow
ConsoleColor.Green
ConsoleColor.Cyan
ConsoleColor.Blue
ConsoleColor.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.