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.
- 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
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.
- 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
In C#, as with most programming languages, the if-statement is the primary means of conditional branching. Condition evaluation must result in true or false. Conditions can range from a single Boolean variable to complex expressions made up of multiple comparisons joined together with and, or, and negation operators. Brackets are used to group comparisons, so the totality of the conditional expression evaluates correctly.
An else branch can be added to take a different mutually exclusive action when the condition is false. Any number of mutually exclusive alternative code branches can be taken using else-if statements. C#, like other languages, supports a shorthand one-line if-syntax, where the result can be assigned to a variable.
The switch statement can be thought of as shorthand for multiple else-ifs with a few caveats. The condition evaluations, specified with the case keyword, are in the context of the switch variable. Case comparisons must be single literal values or expressions. A case evaluation can be augmented with a when clause that must also return a Boolean result. The default keyword is the equivalent of else in an if-then-else statement.
While the conditional branching mechanisms presented in this course are conceptually simple, they can be complex and convoluted in practice and a common source of unintended behavior, basically bugs. It is up to the developer to make sure they work correctly and are constructed in a way that is easy for other developers to understand. My name is Hallam Webber, and I hope you've found this course instructive.
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.