Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
Labs

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 platform
Lab Info
Last updated
Feb 25, 2026
Duration
45m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. 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.cs file without needing to define an explicit Program class or Main method. 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 solution directory. 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 MathChallenge in 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.

  2. 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.

  3. 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 Random class 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.

  4. 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 use if-else statements to provide the user with immediate feedback.

  5. 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 loopcount logic you set up in the first step with a real user-driven replay prompt.

  6. 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 while loops, and then add a score tracker to give the player a sense of progress.

    You'll also change PlayRound to return a bool so 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 with dotnet run --project MathChallenge in the Terminal to observe the gameplay logic you implemented.

About the author

Pluralsight Code Labs offer an opportunity to get hands-on learning in real-time. Be it a Challenge, Sandbox, or Guided Lab, these provide the real world experience needed to succeed in your development role.

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.

Get started with Pluralsight