3.1 - Conditionals (2026-09-02)
Overview
Chris Bourke introduces C conditionals as a way to branch from linear control flow, demonstrating how `if`, `else if`, and `else` select code blocks based on Boolean expressions. He explains comparison and logical operators, C truthiness and `stdbool.h`, then uses GCC examples to expose common errors such as chained range comparisons and using `=` instead of `==` in a condition.
Key takeaways
- An `if`/`else if` chain evaluates conditions from top to bottom and stops at the first true condition, so place more specific cases first and use `else` for the broadest fallback.
- C uses `==` for equality and `=` for assignment; in `if (A = 10)`, A is changed to 10 and the nonzero result makes the condition true.
- C does not interpret `0 <= A <= 10` as a mathematical range test; combine the comparisons with `&&`, for example `0 <= A && A <= 10`.
- C treats zero as false and every nonzero numeric value as true, so test a Boolean-style variable directly instead of assuming its true value must equal 1.
- Logical operator precedence is NOT, then AND (`&&`), then OR (`||`); parentheses are needed when the intended grouping differs from that order.
Chapters
- Chris Bourke points students to the weekly Bits and Bytes newsletter for job listings, internship opportunities, and campus announcements.
- He highlights a virtual reality study offering payment for spending one to two hours as a test subject.
- The computing student organization fair is announced for September 9, from 4 to 6 p.m. at the Nebraska Union, with groups such as Robotics Club, CornHacks, and the Game Development Club.
- Earlier programs followed a sequence: read input, calculate, and print output; conditionals let a program choose which actions to take.
- A Boolean condition evaluates to true or false, as in comparing A with B.
- In an `if` statement, C executes the brace-delimited block only when its parenthesized condition is true, then continues after the closing brace.
- An `if`/`else` chooses between two mutually exclusive blocks: the first runs when its condition is true, otherwise the `else` block runs.
- An `if`/`else if` chain checks conditions in order and executes the block for the first true condition.
- With a final `else`, exactly one branch runs; without it, none may run if every condition is false.
- Bourke models a football outcome using integer Huskers and Ohio scores, with branches for an Ohio win, a Huskers win, or a tie that leads to overtime.
- The example tests scores such as 40 to 10 and 40 to 40 to show how less-than and greater-than comparisons select different branches.
- Order matters in an `if`/`else if` chain: place more specific conditions before broader ones and use the final `else` as the catch-all.
- Bourke's C style places the opening brace on the `if` line, aligns the closing brace with `if`, and indents statements inside each block consistently.
- A variable such as `point_diff`, declared inside a conditional block, is only in scope inside that block and cannot be printed afterward.
- C comparison operators include `<`, `>`, `<=`, `>=`, `==`, and `!=`; a single `=` assigns a value rather than testing equality.
- An `else` is optional, so a tie can produce no output when the program has no matching condition for it.
- Use numeric comparison operators for numbers, not strings: comparing C string pointers with operators such as `==` compares memory addresses rather than string contents.
- Conditions such as `A == A` and `10 == 10` are tautologies, while `0 == 10` is a contradiction that makes its block unreachable.
- When comparing a variable with a literal, Bourke recommends putting the variable first, as in `A == 10`, because it reads more naturally.
- Prefer a direct opposite such as `A >= B` over `!(A < B)` when it makes the condition simpler and easier to maintain.
- In C conditions, zero, including `0.0`, is false, while nonzero values such as `1`, `1.5`, and `-42` are true.
- A student-discount example shows why an integer condition should be tested directly: checking `is_student == 1` can fail if a nonzero true value is something other than 1.
- Including `stdbool.h` provides the `bool`, `true`, and `false` names for clearer code; Bourke notes that the convention still maps false to zero and true to one.
- C uses `&&` for logical AND, which is true only when both conditions are true; a student-and-freshman check can gate a freshman discount.
- C uses `||` for logical OR, which is true when either condition or both are true, unlike an exclusive everyday use of “or.”
- Logical NOT is evaluated before AND, and AND before OR; parentheses can change the result, as shown by expressions combining A, B, and C.
- A single `&` is a bitwise operator, so logical conjunction requires the doubled `&&` symbol.
- The mathematical range check `0 <= A <= 10` does not work as intended in C: the first comparison becomes 0 or 1, and that result is then compared with 10.
- For a range from 0 through 10, write two comparisons joined by `&&`, such as `0 <= A && A <= 10`.
- Writing `if (A = 10)` assigns 10 to A, and the nonzero assignment result makes the condition true; use `if (A == 10)` to test equality.
- Bourke leaves a final conditional example as a prediction exercise for the next class.
Transcript excerpt
0:10 [music] [music] >> Take a step out of >> [music] >> the sound and then turn around. Take a look at you. >> [music] >> It's scary. So silly. Pretty important. >> [music] >> You're not much. You're not anything. >> [music] >> Take a step out of the city and [music] then turn around. Take a look at you. You're pretty important. >> [music] >> You're really nowhere. So wasteful.
0:59 So foolish. I'll be calm. >> [music] >> Who said don't look back? Don't believe them. Go for that crazy San Andreas run. Cuz they're going to try and get behind you. Don't you let them do it. You know what I'm talking about? You hear me talking? >> [music] >> You hear me talking? It's pretty scary. It's so robotic. It's pretty scary. It's so robotic. It's pretty scary. [music] It's so robotic. It's pretty scary. It's so robotic. >> [music] >> Take a step out of the country and then turn around.
1:49 Take a look at you. It's amazing. [music] Take a good look. You're not a big deal. You're >> [music] [music] [music] >> Pretty scary. >> Okay, so before we start I just wanted to bring your attention to our weekly e-newsletter bits and bytes. I've been posting this on campus wire for the last couple of weeks, but make sure that you're paying attention to it because it also has you know job listings or internship
2:37 opportunities or for example one of our one of my colleagues is doing a study in dynamic reality and a virtual reality. And if you want to you can go ahead and head on over that they're offering like some money to be a test subject. All you're going to do is spend an hour or two in VR and you can get some money for
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Chris Bourke.