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
The if-statement is the most basic form of conditional branching, where criteria are evaluated to be true or false and the appropriate code executed. Using the moth example, I'll declare two integer variables porchLumens and moonLumens – a lumen is a measure of light brightness – and a string called flightpath, which is the action taken depending on the condition evaluation. Is the porch light brighter than the moon? If yes or true, head to the porch light.
The syntax is the keyword "if" followed by brackets enclosing the Boolean condition that evaluates to true or false. After the brackets is the code to execute if the condition is true. We can add an else branch, so if the condition is false, we execute different code - so when true, do one thing; otherwise, do something else. Suppose you want the moth to fly to multiple light sources sequentially when the specified criteria is met, and the flight path assignment statement was a placeholder for the flying code, then this would work.
The porch is brighter than the moon, so head to the porch. The neighbor's porch is brighter than the moon, so visit the neighbor's.
However, if the "if-statements" are setting the flight path, and the console WriteLine is the flying, then we end up flying to the neighbor's porch instead of our porch because the neighbor moon comparison was the most recent if-statement executed. If there is only one destination or choice, then we can make all the options mutually exclusive by using else-if statements. Once we hit a true condition, we branch to that code and ignore all the other options.
An if statement will evaluate and execute its code branches independently of other if statements, while if-else statements work together, so only one code branch of many is executed. It is more efficient to use else-if when only one outcome is possible, preventing the execution of redundant code. Adding more else if statements enable you to string together multiple mutually exclusive options.
If the code branches are more than one statement, they must be enclosed in curly braces. There is a shorthand if-then-else syntax that allows you to assign the result of the conditional branch to a variable. There is no if keyword; we start with the condition evaluation, with a question mark representing then and a colon representing else. Like the standard if-then-else, you could nest multiple statements, but it would quickly become hard to read in the shorthand format. Because the "if" one-liner produces a result, you can use it directly as input into another function, like the parameter of Console WriteLine. I'll change moon lumens to 1200 and run the code again.
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.