Programming and Scripting Logic
Building blocks
Variables carry types (integer, float, string, boolean). Scope determines visibility: locals die with the function, globals persist. Operator precedence runs arithmetic, then comparison, then logical operators. Integer division truncates (7/2 = 3 in C, Java ints, Python 2) while 7 % 2 = 1. String concatenation is not addition: "10" + 1 errors in Python but yields "101" in JavaScript.
Control flow
if/elif/else branches; for loops are definite, while loops indefinite and risk running forever; break exits, continue skips. Nested loops multiply iteration counts. Functions take parameters, return values, and may recurse - every recursion needs a base case or the call stack overflows.
Collections and complexity
Arrays/lists are zero-indexed, so n items occupy indexes 0..n−1. Binary search requires sorted input and runs in O(log n); a single loop is O(n); nested loops are O(n²); sorting is typically O(n log n).
Traps
Off-by-one errors; = (assignment) vs == (comparison); post- vs pre-increment; short-circuit evaluation skipping a side effect; mutating a list while iterating over it.
Worked example
Trace: total = 0; for i in range(0,5): total += i. Values added are 0,1,2,3,4 → 10. Rule of thumb: range(a,b) runs b−a times, includes a, excludes b. For a while loop, ask what changes each iteration; if nothing does, it never ends.