A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The if statement is simple. The bugs it produces are not. Missing braces cause the Apple goto fail vulnerability. Assignment instead of comparison causes always-true conditions. Deeply nested if-else chains become unreadable pyramids. The best practices are not style preferences. They are defenses against specific, recurring, documented bugs. Follow them until they are reflexes.
#include <stdio.h>int main(void) {int authenticated = 0;int admin = 1;if (authenticated)printf("Access granted\n");printf("Loading dashboard\n");return 0;}
The indentation lies. "Loading dashboard" is not inside the if. Only "Access granted" is. The second printf executes unconditionally. This is the goto fail pattern: a line that looks conditional is not. With braces, the intent is explicit and the compiler enforces it. Two characters. Zero cost. Infinite protection.
#include <stdio.h>void process_nested(int *data, int len) {if (data != NULL) {if (len > 0) {if (len <= 1000) {printf("Processing %d elements\n", len);} else {printf("Too many elements\n");}} else {printf("Invalid length\n");}} else {printf("NULL data\n");}}void process_guard(int *data, int len) {if (data == NULL) { printf("NULL data\n"); return; }if (len <= 0) { printf("Invalid length\n"); return; }if (len > 1000) { printf("Too many elements\n"); return; }printf("Processing %d elements\n", len);}int main(void) {int arr[] = {1, 2, 3};process_nested(arr, 3);process_guard(arr, 3);return 0;}
Same logic. The nested version has four indentation levels. The guard clause version has one. The main logic sits at the base level, unindented, easy to read. Each guard handles one failure condition and returns. The happy path is the last thing in the function. This pattern scales. Ten validation checks add ten lines, not ten indentation levels.
Braces always. Guard clauses for validation. Flat indentation for the happy path. These three rules eliminate an entire category of conditional logic bugs. They cost nothing. They save everything. Next: the return statement in void functions, and why it is more useful than you think.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.