Page 244 - Computer_Science_F5
P. 244
Example 4.5 of the C++ program avoid logical errors, the programmer
illustrates a semantic error. must plan the solution clearly before
writing the program. Once the error
Program Example 4.5: occurs, the programmer must test the
program and find any logic errors. The Chapter Four: Object oriented programming with C++
FOR ONLINE READING ONLY
Semantic error following Program Example 4.6 of the
C++ program illustrates a logical error.
#include <iostream> Program Example 4.6:
using namespace std;
int main() Logic error
{ #include <iostream>
int a, b, c; using namespace std;
a + b = c; //semantic error // A C++ program to illustrate // logical
return 0; error int main ()
} {
int i = 0;
// logical error : a semicolon after loop
for(i = 0; i < 3; i++);
Output: {
Error: value required as left operand cout << “loop “;
of assignment a + b = c; //semantic error continue;
}
return 0;
Logic errors }
Logic errors are constructs that can be
translated into a running program, but Output:
the resulting program does not perform Error:
the intended action. Logical errors may No output
occur even if the program is syntactically
correct and does something. However, Run-time error
the output of the program will be
different from what was intended by the Run-time errors are the types of errors
programmer. An example of a logical that occur during program execution
error is when a program uses a wrong (run-time) after successful compilation
formula, or assigns wrong value to the of the program. Division by zero is one
code assignment statements. Under of the most common run-time errors. In
this situation, the compiler cannot find this type of errors, the compiler does not
the error, and it must be flushed out directly point to the line where the error
when the program runs by testing it occurs. Run-time errors are also called
and carefully looking at its output. To “bugs”. Before a program is released to
235
Student’s Book Form Five
Computer Science Form 5.indd 235 23/07/2024 12:33

