&&, ||, !), see how short-circuit evaluation lets you write safe guards, and learn how C treats truthiness differently from Python. Then you'll wire those expressions into if / else if / else and switch statements, and learn the four bugs that bite cadets writing their first real conditionals.
By the end of this lesson, cadets will be able to:
&&, ||, !) and predict the effect of short-circuit evaluation. (Outcome 1)if, else if, else, and switch statements. (Outcome 1)Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 3 (sections on logical operators, if / else, and switch) |
Logical operators, conditional statements, truthiness rules |
| King, C Programming: A Modern Approach | Chapter 5 (Selection Statements) | Optional deeper dive on if, switch, and short-circuit evaluation |
if, switch)&&, ||, ! (logical AND, OR, NOT)if, else if, elseswitch, case, break, defaultToday's content has two halves that come together. First half: logical operators and how C decides what counts as true. Second half: the statements (if, else if, else, switch) that act on those decisions. The reference material below covers what you'll see in lecture and use in the logic tracer.
The Lesson 4 deck covers logical operators, truthiness, short-circuit evaluation, the conditional statements, and the four bugs to watch for. The slides are the spine of the in-class lecture; the logic tracer is where you practice predicting how expressions evaluate.
Open Lesson 4 slides → replace href with your deployed slide deck URL
C does not have a dedicated boolean type the way Python does. The native keyword _Bool exists (added in C99), and <stdbool.h> provides the friendlier bool, true, and false on top of it. You will see plain integers used for truth values all over real C code, though. The rule is exactly one sentence:
That includes negative numbers, positive numbers, floating-point values that are not exactly zero, and yes, even -1. All of these evaluate to true:
What is different from Python:
None, and zero are all false. C does not have those concepts here; it just checks "is this number zero?"== produce a result, they give you 1 for true and 0 for false. The logical operators (&&, ||, !) do the same.True or False keyword in C the way there is in Python. Write 1 and 0.Three logical operators. They combine truth values (which, remember, are just zero and nonzero in C) and produce 1 or 0 as their result.
| Operator | Name | True when |
|---|---|---|
| a && b | Logical AND | Both a and b are nonzero |
| a || b | Logical OR | At least one of a or b is nonzero |
| !a | Logical NOT (unary) | a is zero (i.e. flips truth) |
Truth tables:
| a | b | a && b | a || b |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
| a | !a |
|---|---|
| 0 | 1 |
| nonzero | 0 |
Remember: ! turns nonzero into 0, and 0 into 1. It always returns exactly 0 or 1.
and, or, not. In C, those words mean nothing. Use the symbols: &&, ||, !. Also: && is two ampersands. A single & is the bitwise AND (Lesson 34) and means something completely different.C evaluates && and || left to right and stops as soon as the answer is known.
a && b: if a is false, the whole thing is false. b is never evaluated.a || b: if a is true, the whole thing is true. b is never evaluated.That is not just a performance optimization. It is a tool. You can rely on it to protect dangerous expressions:
Swap the order and the program crashes on division by zero whenever denom is zero:
Order matters. The cheaper or safer check goes on the left.
The basic conditional in C is shaped like Python's, with three syntactic differences: parentheses around the test, curly braces (not indentation) define the body, and the keyword is else if (two words) instead of Python's elif.
Style note: braces are technically optional for single-statement bodies, but always include them. It prevents the dangling-else bug (see below) and makes the code less ambiguous to humans and easier to extend later.
if / else / switch statement (the K&R style you see in every example on this page). §7.4 forbids single-line if statements; every branch body sits on its own line inside braces. See the DFCS C Programming Standard in the course Resources section for the full rules.
The else branch is optional. Exactly one branch executes: C tries each condition in order and stops at the first true one. If none are true and there is no else, nothing in the chain runs.
C has a compact form of if/else that returns a value. It's called the ternary operator because it takes three operands. The syntax is condition ? value_if_true : value_if_false.
Use it when both branches produce a value of the same type and the whole thing fits comfortably on one line. Common cases: picking between two strings, two numeric constants, or two simple expressions. The printf example below is a typical use:
Reach for a full if/else when the branches are statements (not values), when either branch is more than a short expression, or when nesting ternaries would hurt readability. Most code in this course should use full conditional statements; the ternary appears here so you recognize it when you see it in Beej or in real C code.
When you're branching on the integer value (or character value) of a single variable, switch is often cleaner than an if/else if chain.
Rules to keep in mind:
char (which is just a small integer). You cannot switch on a double or a string.case label must be a constant, not a variable or a range.default is optional and runs when no case matches.break, execution falls through to the next case. See the bugs section below.When in doubt, write if / else if. Reach for switch when you have three or more cases on a single integer value and the cases are exact matches (not ranges or comparisons).
= instead of ==if, it bites hard:-Wall. Read your warnings. Defensive trick: write the constant on the left (if (5 == x)) so an accidental single = becomes a compile error.elseelse binds to the nearest unmatched if, not the one your indentation suggests:break in switchbreak, execution falls through into the next case:break. (Intentional fall-through is a real pattern, but until you have a reason, treat the missing break as a bug.)ifif into an empty statement:x. The compiler accepts this; -Wall may warn. When your conditional “always runs”, look for a stray semicolon first.Two things to do in class.
Eight rounds. Each one shows you a small snippet, the current values of the variables, and asks what prints. After you predict, the widget walks you through the evaluation step by step, including which conditions got short-circuited and which conditionals ran.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.