C If and Else
Classify a depth reading by testing ordered ranges rather than overlapping independent conditions.
How it works
An if selects a statement according to whether its controlling expression compares unequal to zero. An else belongs to the nearest unmatched if, which is one reason to use braces consistently. An else-if chain chooses the first matching branch and skips the remainder. Define what happens exactly at a threshold before writing the comparison. Survey classifications often have gaps or double-counted boundaries when their ranges are described informally. Choose inclusive and exclusive edges deliberately and test them.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror conditions.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
int main(void) {
int depth_mm = 45;
if (depth_mm < 0) {
puts("Invalid depth");
} else if (depth_mm < 30) {
puts("Shallow");
} else if (depth_mm <= 60) {
puts("Standard");
} else {
puts("Deep");
}
return 0;
}
Expected result
Standard
A negative value is rejected before ordinary categories are considered. Thirty belongs to Standard and sixty still belongs to Standard. Because this is a chain, only one label is emitted. To test the rule, include minus one, zero, twenty-nine, thirty, sixty and sixty-one rather than only the middle of each range.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.