3.2 - Conditionals (2026-09-04)
Overview
Chris Bourke reviews C conditionals through two worked programs, emphasizing curly braces, mutually exclusive if/else if chains, boundary testing, and GCC warnings enabled with -Wall and -Wextra. He builds a decibel classifier and a Cartesian-coordinate classifier, using them to explain input choices, error cases, ad hoc testing, regression, and why automated unit tests are valuable.
Key takeaways
- In C, a semicolon immediately after an if condition creates an empty controlled statement, so the next block can execute regardless of the condition; braces prevent this binding mistake.
- Compile C exercises with GCC's -Wall and -Wextra flags to expose issues such as misleading indentation, assignment in a condition, and unused variables.
- An if/else if chain is appropriate when classifications must be mutually exclusive, and ordering the most specific cases first prevents broad conditions from swallowing special cases.
- C does not interpret 0 <= value <= 60 as a chained mathematical comparison; write value >= 0 && value <= 60 instead.
- Boundary cases such as 60, 70, and 110 are especially useful for finding off-by-one errors in range-based conditionals.
- Interactive scanf input requires validation: malformed text can leave numeric variables unreliable, while double values provide about 16 to 17 significant digits before precision loss or overflow becomes a concern.
Chapters
- An opening music segment runs until about 3:02, when Chris Bourke returns to the C conditionals lesson.
- The class agenda is to finish conditionals before beginning loops at the next Wednesday meeting.
- A semicolon directly after an if condition forms an empty statement, so the following printf block is no longer controlled by the condition.
- In the example, a is 5 but the program still prints that a is 10 because the print block executes independently.
- Chris recommends always using curly braces, even for a single statement, since indentation alone does not determine C statement scope.
- GCC's -Wall flag detects the misleading indentation caused by an if statement followed by an unintended semicolon; -Wextra enables additional warnings.
- Chris distinguishes static analysis, which inspects source code before execution, from dynamic tools such as debuggers and profilers that analyze a running program.
- Warnings can reveal code smells such as assignment used in a condition instead of equality, or variables declared but never used.
- The course grader checks warnings and awards one design point for addressing them; CS50.AI is presented as a tool for explaining warnings, not simply writing fixes.
- The exercise maps sound levels to descriptions, including quiet from 0 to 60 and conversational from 61 to 70; Chris notes that the prompt leaves some range boundaries unspecified.
- The program begins with author and description comments, an ISO 8601 date in year-month-day order, and only the needed standard I/O headers.
- Chris explains that unspecified behavior, such as whether fractional decibel values are allowed, should be clarified through requirements elicitation with stakeholders.
- The program expects two command-line arguments in argc: the executable name at argv[0] and the decibel value at argv[1].
- If argc is not 2, it prints an error message and exits with status code 1 rather than continuing without input.
- The exercise uses atoi to convert argv[1] to an integer, matching the whole-number values specified in the prompt.
- A range test such as 0 <= decibel <= 60 is not a mathematical chained comparison in C; it must be written as two comparisons joined with &&.
- Chris uses an if/else if chain so only one classification can run, and removes redundant lower-bound checks after earlier branches establish the remaining range.
- He tests boundary values such as 60, 70, and 110, then adds handling for values below zero and a final catch-all for values such as 2000.
- Manual ad hoc tests can catch errors, but changing a branch can cause a regression, which motivates automated unit tests that rerun many cases.
- The second exercise asks for x and y coordinates and reports whether the point is at the origin, on an axis, or in one of four quadrants.
- Chris switches to interactive input with scanf and uses double variables so coordinates can include fractions such as 3.5.
- For scanf with the %lf conversion, each double variable must be passed by address, such as &x; a test print confirms that both coordinates are read.
- The if/else if sequence checks the most specific case first: x == 0 and y == 0 identifies the origin.
- Next, x == 0 identifies the y-axis and y == 0 identifies the x-axis, before any quadrant conditions are evaluated.
- Quadrants are determined by coordinate signs: I is positive/positive, II negative/positive, III negative/negative, and IV positive/negative.
- Because the origin, both axes, and four quadrants cover all valid coordinate pairs, an extra final else is unnecessary for numeric input.
- Chris tests representative cases including (4, 4), (0, 0), (0, 10), and (-10, 0) to check the origin, a quadrant, and both axes.
- Entering text such as hello can leave scanf inputs unusable and lead to an incorrect classification, illustrating why interactive input is brittle without validation.
- A double stores roughly 16 to 17 significant decimal digits; very large magnitudes can overflow when the exponent exceeds the representation's capacity.
- The lecture closes by noting that the next lab covers conditionals and the following class begins loops.
Transcript excerpt
0:02 [music] [music] Heat. Heat. [music] Anywhere [music] I see kiss [music] myself [music] kiss my she said she take me anywhere she take me [music] anywhere as long as she stay anywhere take me anywhere [music] as long as I say kiss myself kiss Kiss my [music] hands.
0:53 Kiss my hands. [music] [music] Heat. Heat. [music] [music] >> [music] >> Kiss kiss [music] [music] [music] my hands.
1:44 >> [music] >> All right. So today we'll finish up on
3:04 conditionals and then on Wednesday when we come back because Monday there is no class because of a labor day. Uh but on Wednesday when we come back we will start on loops. Uh but otherwise uh we left it off on this pitfall here and you can probably tell what it is by there's something fishy. There's something wrong here by immediately looking at it. Uh what was wrong with this code here? Yeah, there there's a semicolon sitting right here. We've never seen that before, right? We haven't been putting semicolons after conditional statements. We've only been putting them after executable statements and they don't belong after conditional statements. Conditional statements don't execute anything. They decide am I going to do this or I'm going to do that that and then it goes and does that thing. It doesn't actually do those things yet. Uh
3:51 so what happens when we put that into code here? I'll go ahead and compile this predict. Is it going to print out a is 10 or not? It's not right. I mean it's a is not 10. It's equal to five. But it ends up outputting that a is 10. Now the reason for that is what's called binding. All right? So if I just write this in isolation and I said put a semicolon
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.