Conditionals

4. Conditionals#

Programs become useful when they can make decisions. This chapter introduces the C# constructs that choose different code paths: if, else if, else, switch, and the Boolean expressions that control them.

  • Boolean expressions: Conditions evaluate to true or false and drive program decisions.

  • if statements: An if statement runs code only when its condition is true.

  • Multi-branch logic: else if, else, and switch handle several possible cases.

  • Logical operators: Operators such as &&, ||, and ! combine or reverse conditions.

Conditionals let programs validate input, express business rules, and respond differently as data changes.

Video

C# if statements 🤔 by Bro Code.

This video remains hosted on YouTube and is the property of its original creator or rightsholder; it is embedded here via YouTube’s official player for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Write if, else if, and else chains correctly

  2. Construct Boolean expressions with comparison and logical operators

  3. Handle multiple exclusive cases with a switch statement

  4. Trace which branch of a conditional statement will execute

  5. Avoid common conditional mistakes such as dangling else and boundary errors

Chapter Flow