4. Conditionals#
Programs need to make decisions. This chapter introduces the core decision-making constructs in C#: if, else if, else, switch, and the boolean expressions that drive them.
Why this chapter matters
A program that always does the same thing regardless of its inputs has limited value. Conditionals unlock real behavior:
validate user input before using it
pick the right code path for each situation
express business rules such as grade cutoffs or access levels
combine multiple conditions with
&&,||, and!
Learning goals
By the end of this chapter, you should be able to:
write
if,else if, andelsechains correctlyconstruct boolean expressions using comparison and logical operators
handle multiple exclusive cases with a
switchstatementavoid common pitfalls: dangling else, off-by-one comparisons