- Home
- Training Library
- Programming
- Programming Courses
- C# Conditional Branching
Switch Statement
Contents
C# Conditional Branching
The course is part of this learning path
The ability for software to perform tasks or functions based on the state of variables or data is intrinsic to programming. In plain language, that means responding appropriately to input from the user or some data feed. C# Conditional Branching primarily focuses on If and Switch statements, the main mechanisms for executing or branching to different code depending on whether a condition is true or false. A simple example is allowing users to log in if their username and password match those stored in the software.
Learning Objectives
- Learn and understand If-statement syntax and how to use it in different scenarios
- Learn and understand the Switch statement and how to use it
- Learn about evaluating multiple Boolean conditions
- See how the dotnet new command has changed with the release of .NET 6.0 and C# 10
Intended Audience
This course is intended for students that are relatively new to programming but not absolute beginners and want to know how to make their software responsive and adaptive. Students should know the basics of a C# programs structure and have experience running a program within their development environment. While for-loops and object-oriented concepts will be mentioned in the course, in-depth knowledge of these topics isn't essential.
Prerequisites
- A working development environment like VS Code or Visual Studio
- Basic knowledge of a C# program's structure
- C# Loops Deep Dive
- Introduction to Object Orientation and C# Classes
Demo Source Code
The switch statement is a way to branch conditionally or take action based on the value of the switch variable, with each case clause representing a branching option. A case clause value must be a literal value or expression and not a variable. Each case ends with the break keyword that moves execution to the first line of code after the switch statement. You can use the default case to catch all situations where the switch variable hasn't already been matched or satisfied by a previous case.
Cases can utilize comparisons like less than, greater than, and the corresponding equal to operators. If I want to compress the five-level rating to 3 outputs with "less thans," I will need a case at the beginning to catch when the rating is less than 1. On the top right is the call to Rating.ToString() and below is the output. A rating of zero correctly returns invalid, but we end up with essentially two out-of-range cases. While you cannot use "and" or "or" operators, you can use the when keyword to add more conditions.
The case conditions implicitly use the switch variable in the comparison, but you need to reference the switch variable in the when clause explicitly. A rating of zero is less than or equal to five, but that is incorrect. I'll also need to add when the rating is greater than zero to the less than or equal to five case.
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.