- Lab
-
Libraries: If you want this lab, consider one of these libraries.
Guided: C# 14 Logic and Control Flow
In this lab, you'll learn how to structure a C# application, handle user input, implement game logic with loops and conditional statements, and refactor your code into reusable methods. This lab aims to introduce you to algorithmic thinking in C# before diving into more complex object-oriented concepts.
Lab Info
Table of Contents
-
Challenge
Introduction
Welcome to the C# 14 Logic and Control Flow Lab!
In this lab, you'll build a complete, interactive console game from the ground up. You'll start with a minimal C# application that uses a feature called "top-level statements," which allows you to write code directly in the
Program.csfile without needing to define an explicitProgramclass orMainmethod. This is great for keeping things simple as you learn.
As you progress throughout the lab, you can check your implementation at any time by comparing it to the corresponding solutions in the
solutiondirectory. Keep in mind that you do not need to have the same exact solution as long as you are passing the tasks and your implementation is functionally the same.You can also run the application with
dotnet run --project MathChallengein the Terminal.This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 1: Setting up the Game Structure
The first step in this application is to create the main game loop. This loop will contain the logic for a single round of the game and will control whether the game continues or ends.
-
Challenge
Step 2: Creating the Challenge
A good game needs to be dynamic. Hardcoding the math problems wouldn't be great for obvious reasons. You'll use C#'s built-in
Randomclass to generate a new problem for the user in every round. You'll also randomly select an arithmetic operation (+, -, *, /) to keep the challenge varied. Once you have the numbers and operation, you'll calculate the correct answer and present the question to the user via the console. -
Challenge
Step 3: Handling User Input and Logic
Now for the interactive part. You need to get the user's answer, but this comes with a challenge: console input is always text. You can't do math with the string
"12". You must convert it to a number. This process is called parsing.Since the game includes division, answers can be decimals. You will use
double.TryParse(), a safe and efficient C# method for converting strings to numbers. It prevents your application from crashing if the user enters non-numeric text. Based on whether the parsing is successful and if the answer is correct, you'll useif-elsestatements to provide the user with immediate feedback. -
Challenge
Step 4: Refactoring and Replay
Your application is now fully functional, but as programs grow, it's important to keep the code organized. The process of restructuring code without changing its behavior is called refactoring. You'll refactor the game logic into its own dedicated method called
PlayRound.Finally, you'll complete the game by implementing the replay feature, allowing the user to decide when they're done playing. This will replace the placeholder
loopcountlogic you set up in the first step with a real user-driven replay prompt. -
Challenge
Step 5: Input Validation and Score Tracking
Your game works, but it could be more robust and engaging. Right now, if a user enters invalid input for their answer, the round is skipped entirely. And if they type something other than 'y' or 'n' at the replay prompt, the game just exits. You can fix both of those issues using
whileloops, and then add a score tracker to give the player a sense of progress.You'll also change
PlayRoundto return aboolso the main loop can track whether each round was answered correctly. If you've made it this far, then great job! You've completed this lab. Don't forget to try out your application withdotnet run --project MathChallengein the Terminal to observe the gameplay logic you implemented.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.