ALGORITHMIC TRACING
Course: CS-101 Fundamentals of Programming in C | Module 02: Comprehensive
Lecture & Class Notes
1. Foundations of Structured Control Flow
According to the Böhm–Jacopini Structured Program Theorem, any computable
algorithm can be expressed using three fundamental control structures: Sequence,
Selection, and Iteration.
1.1 Boolean Truth Representation in C
Historically, ANSI C89 lacked a dedicated boolean primitive. Consequently, all conditional
expressions evaluate to an integer of type int :
Logical False: Strictly integer zero ( 0 ), floating-point zero ( 0.0 ), or the null pointer
( NULL ).
Logical True: Any non-zero value (positive or negative, such as 1 , -5 , or 42 ).
Relational and logical operators ( == , != , < , && , || ) are guaranteed by the C
standard to yield strictly 1 or 0 .
2. Selection Statements & Branching Mechanics
2.1 Single-Alternative and Dual-Alternative Selection
[ Evaluate Condition ]
/
True / \ False
vv
±---------------+ ±---------------+
| True Branch | | False Branch |
| Statement(s) | | Statement(s) |
±---------------+ ±---------------+
\/
vv
[ Next Statement ]
Syntax & Block Scoping
if (condition_expression) {
// Compound statement block (executes if condition_expression != 0)
} else {
,// Alternative compound block (executes if condition_expression == 0)
}
Scope Rule (C99/C11): Variables declared inside an if or else compound block
possess block scope and automatic storage duration; their memory is reclaimed
immediately upon exiting the closing brace } .
2.2 Multi-Alternative Selection: The else-if Ladder
When testing mutually exclusive conditions sequentially, the else-if ladder guarantees
that once the first true condition is encountered, its associated block executes and all
remaining branches are bypassed:
#include <stdio.h>
int main(void) {
double units, bill;
printf(“Enter electricity units consumed: “);
scanf(”%lf”, &units);
if (units <= 100.0) {
bill = units * 1.50;
} else if (units <= 200.0) {
bill = (100.0 * 1.50) + ((units - 100.0) * 2.50);
} else if (units <= 300.0) {
bill = (100.0 * 1.50) + (100.0 * 2.50) + ((units - 200.0) * 4.00);
} else {
bill = (100.0 * 1.50) + (100.0 * 2.50) + (100.0 * 4.00) + ((units - 300.0) * 6.00);
}
printf("Total Payable Bill: $%.2lf\n", bill);
return 0;
}
2.3 Multi-Way Dispatch: The switch…case Statement
The switch statement evaluates an integral control expression once and transfers
control directly to the matching case constant label.
Formal Syntax Constraints
Control Expression: Must undergo integer promotion to int , long , char , or enum .
Floating-point types ( float , double ) and strings ( char* ) produce a compile-time error.
Case Labels: Each case label must be a unique compile-time constant expression
(e.g., case 10: or case ‘A’: ). Variable identifiers (e.g., case x: ) are illegal in standard C.