- Home
- /
- CS Thinking
- /
- Mistakes
- /
- Programming Fundamentals
Common Mistakes in Programming Fundamentals
48 common pitfalls across 12 concepts, including 12 recurring stuck points
These are the pitfalls students hit across all 12 programming fundamentals concepts, from Assignment, Integer and String onward. Each entry names the concept it comes from, so you can tell a slip from a genuine gap and go straight to the explanation.
= means 'assign,' not 'equals.' Many languages use == for equality comparison.
Confusing the assignment operator (=) with the equality test (==), especially in conditions
Reading x = x + 1 as a mathematical equation instead of an update instruction
Forgetting that the right side is evaluated first, so all variables on the right use their old values
Integer division truncates in many languages: 7 / 2 gives 3, not 3.5. This catches many beginners by surprise.
Expecting 7 / 2 to give 3.5 when using integer division, which truncates to 3 in many languages
Causing integer overflow by exceeding the maximum value a fixed-width integer can hold
Using integers for values that need decimal precision, like currency calculations
'123' is a string, not a number. You can't do math with it until you convert it: int('123') gives the number 123.
Using + between a string and a number without converting, causing a TypeError in Python (Java and JavaScript instead turn the number into text and join them)
Forgetting that string indices start at 0, so the first character is s[0] not s[1]
Assuming strings are mutable—in many languages (Python, Java), strings cannot be changed in place; operations create new strings
0, empty string, None/null, and empty lists are often 'falsy' — they act like False in conditions even though they're not boolean.
Comparing a boolean to True explicitly (if x == True) instead of using it directly (if x)
Not understanding truthy/falsy values—0, empty string, None, and empty list evaluate as False in conditions
Using assignment (=) instead of equality (==) in a boolean expression, which changes the variable instead of testing it
Print and return are different. Print displays a value; return sends it back to the calling code for further use.
Using print instead of return, then wondering why the calling code receives None/undefined
Forgetting to capture the return value in a variable, so the result is computed but immediately lost
Placing code after a return statement, which never executes because return exits the function immediately
Modifying a global variable inside a function usually requires a special keyword (global in Python). Without it, you create a new local variable.
Accidentally creating a new local variable instead of modifying the intended global variable
Trying to access a local variable outside the function where it was defined, causing a NameError
Over-relying on global variables instead of passing data through parameters, making code hard to debug and test
Deep nesting makes code hard to read. Consider using elif/else-if chains or combining conditions with AND/OR instead.
Nesting too deeply (more than 3 levels) when the logic could be simplified with combined boolean conditions
Losing track of which else belongs to which if, especially without proper indentation
Duplicating code across branches instead of restructuring to eliminate redundancy
Forgetting to update the loop variable creates an infinite loop. Always make sure the condition will eventually become false.
Forgetting to update the loop variable inside the body, causing an infinite loop
Using the wrong comparison operator in the condition (< vs <=), causing off-by-one errors
Not initializing the loop variable before the while statement, causing an error or wrong behavior on the first check
range(5) gives 0 to 4, not 1 to 5. Off-by-one errors are the most common for-loop bug.
Off-by-one errors: range(5) gives [0,1,2,3,4], not [1,2,3,4,5]
Modifying the collection being iterated over during the loop, causing skipped elements or errors
Using a for loop when the number of iterations is unknown—a while loop is more appropriate in that case
An event handler is registered (set up) once but can fire many times. Setting it up doesn't run it immediately.
Calling the function immediately instead of passing it as a reference (button.onClick(myHandler()) runs it now; button.onClick(myHandler) registers it)
Registering a handler more than once (for example, setup code that runs twice and adds a new anonymous function each time), causing it to fire once per registration
Writing slow event handlers that block the main thread and make the interface freeze
Always close files when done (or use with-statements). Unclosed files can lose data or lock the file from other programs.
Forgetting to close the file after reading or writing, risking data loss or file locks
Opening a file in write mode ('w') when you meant to append ('a'), accidentally erasing all existing content
Not handling the case where the file does not exist, causing the program to crash with a FileNotFoundError
Defining a function doesn't run it — you must call it. Parameters (defined) differ from arguments (passed in).
Defining a function but never calling it, then wondering why nothing happens
Writing functions that are too long and do too many things, making them hard to test and reuse
Confusing parameters (placeholders in the definition) with arguments (actual values passed at the call site)