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: 😊

  1. Create a new C# Repl project
  2. Name it "Fifty Smiles"
  3. In the main.cs file, in the Main body, print a welcome message
  4. Under that, declare a new int variable named i
  5. Set the i variable to 0
  6. Under that, create a while loop structure
    • while keyword
    • Parentheses
    • Curly brackets
  7. For the while condition (between the parentheses), check if i is less than 50
  8. For the while body (between the curly brackets), print out a smiley face
  9. Under that, still within the { and }, increment i 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("");
  1. Create a new C# Repl project
  2. Name it "A Dozen Eggs"
  3. In the main.cs file, in the Main body, print a welcome message
  4. Under that, declare a new int variable named i
  5. Set the i variable to 0
  6. Under that, create a while loop structure
    • while keyword
    • Parentheses
    • Curly brackets
  7. For the while condition (between the parentheses), check if i is less than 12
  8. For the while body (between the curly brackets), use the Console.WriteLines to print an egg
  9. Under that, still within the { and }, increment i 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.

  1. Create a new C# Repl project
  2. Name it "Annoying Bot"
  3. In the main.cs file, in the Main body, print a welcome message
  4. Under that, declare a new string variable named answer
  5. Set the answer variable to ""
  6. Under that, create a while loop structure
    • while keyword
    • Parentheses
    • Curly brackets
  7. For the while condition (between the parentheses), check if answer is equal to "You are not annoying"
  8. In the while body (between curly brackets), add a Console.WriteLine statement
  9. Make the statement print out "Am I annoying you yet?"
  10. Under that, still within the { and }, create a new line
  11. On that line, set the answer variable to a Console.ReadLine()
  12. Under that, use an empty Console.WriteLine to print out an empty line
  13. 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.

  1. Create a new C# Repl project
  2. Name it "Rainbow Road"
  3. In the main.cs file, in the Main body, print a welcome message
  4. Under that, create a while loop structure
    • while keyword
    • Parentheses
    • Curly brackets
  5. For the while condition (between the parentheses), simply put true
  6. In the while body (between curly brackets), set the text color to red
    • Console.ForegroundColor = ConsoleColor.Red;
  7. Under that, still within the { and }, add a Console.WriteLine()
  8. In the WriteLine, print out "| || |"
  9. Under that, add a second Console.WriteLine printing the same thing
  10. 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.

results matching ""

    No results matching ""