CMP452-Structured Programming-1
CMP452-Structured Programming-1
Debugging is an essential process in programming that involves identifying and fixing errors
in code. Errors in programming can be classified into three main categories:
1. Syntax Errors: Errors in the structure or grammar of the code. The compiler detects
these.
o Example: Missing semicolon, misspelled keyword.
2. Semantic Errors: Errors in the meaning of the code. The compiler may not catch
these, but the program behaves incorrectly.
o Example: Using an uninitialized variable, incorrect operator precedence.
3. Logical Errors: The program compiles and runs, but it doesn't produce the desired
output due to a flaw in the algorithm.
o Example: Incorrect loop condition, wrong formula.
4. Runtime Errors: Errors that occur while the program is running. These can cause the
program to crash.
o Example: Division by zero, accessing an invalid memory location (e.g., out-
of-bounds array access).
Code example for all errors :
#include <iostream>
int main() {
// Syntax Error: Missing semicolon
int x = 10 // Missing semicolon
Inserting std::cout statements to display the values of variables or trace the flow of
execution.
Example:
#include <iostream>
int main() {
int x = 10;
std::cout << "Value of x before loop: " << x << std::endl;
for (int i = 0; i < 5; ++i) {
x += i;
std::cout << "Value of x in loop (i=" << i << "): " << x << std::endl;
}
std::cout << "Value of x after loop: " << x << std::endl;
return 0;
}
Code Inspection:
Code inspection involves manually reviewing code for errors before execution.
Test Cases:
What is Program Verification? Program verification is ensuring that a program meets its
specifications and works correctly under all conditions.
Types of Verification
Manual Verification:
o Proof by hand: Using mathematical logic to reason about the program's
behavior.
o Code Reviews: Can also be considered a form of manual verification,
especially when reviewers are looking for logical errors.
Formal Verification:
o Using mathematical logic and automated tools to verify program correctness.
o Theorem Proving: Expressing program properties as theorems and using a
theorem prover to prove them.
o Model Checking: Checking if a program satisfies a given specification by
systematically exploring all possible states.
o Abstract Interpretation: Analyzing the program's behavior by considering an
abstract version of its data and operations.
Assertions are typically enabled during development and testing, and disabled in production
code (by defining the macro NDEBUG).
Loop Invariants:
A loop invariant is a condition that is true before the loop starts, remains true after
each iteration of the loop, and is true when the loop terminates.
Loop invariants are crucial for verifying the correctness of loops.
Steps to verify a loop using a loop invariant:
o Initialization: Show that the invariant is true before the loop starts.
o Maintenance: Show that if the invariant is true at the beginning of an iteration,
it is also true at the end of the iteration.
o Termination: Show that when the loop terminates, the invariant and the loop
termination condition imply the desired result.