Conditional Exercises
After completing the code-along activity, work on the exercises below.
One More Question
Building from the Choose Your Path project, add an additional question to make the game a little more involved.
- In the main.cs file, in the
Mainbody, find the finalifstatement - Above that, use
Console.WriteLineto ask another question -"You get to school. Do you stop at the water fountain?" - Display the options:
" A. Yes"/" B. No" - Use
Console.Writeto print"> " - Under that, create a new
stringvariable namedchoice3 - Set the
choice3variable toConsole.ReadLine() - Use
Console.Clear()to clear the console - Under that, create an
ifstructureifkeyword- Parentheses (condition)
- Curly brackets (body)
- For the condition (between the parentheses), check if
choice3equals"A" - In the body (between the curly brackets), set the
healthvariable tohealth + 1 - Also in the body of the
if, display a message saying "You drink some water." - Under the
ifbody (after the}), create anelse ifstructureelse ifkeyword- Parentheses (condition)
- Curly brackets (body)
- For the
else ifcondition, check ifchoice3equals"B" - In the body of the
else if, set thehealthvariable tohealth - 1 - Also in the body of the
else if, display a message saying "You do not drink water."
Run the program again, and verify that the third question properly impacts the game!
A New Project - Chat Bot
Using conditional statements, a whole lot of things are possible.

While the definition of "artificial intelligence" may not always include simple if statements, it is certainly possible to create the illusion of intelligence using conditionals!
Create a program that can respond to a number of different chat messages.
Setup
Start by creating the new project.
- Create a new C# Repl project
- Name it "Chatbot"
- In the main.cs file, in the
Mainbody, set the text color:Console.ForegroundColor = ConsoleColor.Black; - Under that, set the background color:
Console.BackgroundColor = ConsoleColor.White; - Under that, clear the console to make it a little cleaner:
Console.Clear(); - Next, use
Console.WriteLineto display a message saying "Welcome to the Chatbot" - Under that, add an empty
Console.WriteLineto make some extra space
Run the program, and verify that the message appears in the proper color.
A Question
Next, the program should ask the user a question. This can theoretically be any question.
- Under the lowest
Console.WriteLine, create anotherConsole.WriteLine - Display a message with a question, like "What do you want to say?"
- Under that, create a new
stringvariable namedanswer - Set the
answervariable to be aConsole.ReadLine()
Run the program. Verify that the question appears, and it is possible to enter an answer!
A Response
Now that the user has entered an answer, it is possible to respond to that answer. The response should depend on what the user said. For example, if the user says "You talkin' to me?" the bot could say "Well I'm the only one here."
Use an if statement to create that response!
- Under the
answervariable, create anifstructureifkeyword- Parentheses (condition)
- Curly brackets (body)
- For the condition (between the parentheses), check if the
answervariable equals"You talkin' to me?" - In the body (between the curly brackets), add a
Console.WriteLinestatement - Make the
WriteLinedisplay a message saying"Well I'm the only one here."
Run the program again. Verify that entering the exact answer "You talkin' to me?" will make the program display the correct response!
The code should look something like this:
if (answer == "You talkin' to me?")
{
Console.WriteLine("Well I'm the only one here.");
}
Other Responses
Using the same type of structure, it's possible to make the program respond to any message at all! Add some more if statements, each of them checking the answer value and printing a different response based on the answer. Feel free to customize the responses, or use the ones from this table:
answer |
Response |
|---|---|
| "You killed my father." | "No, I am your father!" |
| "I'm going to Mordor alone." | "Of course you are, and I'm coming with you!" |
| "You look like my grandpa." | "Oh, is your grandpa super cool?" |
| "Your grandpa invented baseball?" | "Did I say baseball? I meant spray-on eyebrows." |
| "Is this the Krusty Krab?" | "No, this is Patrick." |
| "This is the worst day of my life." | "The worst day of your life so far." |
Dynamic Colors
Instead of simply setting the colors at the beginning of the program, ask the user which colors to use!
- At the top of the
Mainbody, above theConsole.ForegroundColor, make a new space - There, use
Console.WriteLineto display a message: "Which text color?" - Under that, create a new
stringvariable namedcolorAnswer - Set the
colorAnswervariable to beConsole.ReadLine() - Under that, create an
ifstructureifkeyword- Parentheses
- Curly brackets
- For the condition (between the parentheses), check if
colorAnswerisRed - In the body (between the curly brackets), use
Console.ForegroundColor = ConsoleColor.Redto set the color
Run the program, and verify that it is possible to select "Red" as the text color!
Add additional if structures for some additional colors, like ConsoleColor.Green, ConsoleColor.Cyan, and ConsoleColor.Yellow.
Challenges
After completing these exercises, start working on the challenges.