Page 461 - ComputerScience_Class_11
P. 461
12.3.4 Logical Errors
A logical error is a mistake in the design or thinking behind a program. In this type of error, the program runs without
showing any syntax or runtime error, but the output produced is incorrect. This happens because the steps written in
the program do not correctly represent the intended logic of the problem.
Logical errors are often difficult to identify because the program appears to work normally. The code executes
successfully, but due to incorrect formulas, wrong conditions, or incorrect sequence of steps, the result does not
match what the programmer expected.
Logical errors usually occur due to:
• Using the wrong formula or operator
• Writing incorrect conditions in decision statements
• Performing steps in the wrong order
• Misunderstanding the problem requirement
Program 7: Imagine you want to write a program to calculate the factorial of a number.
Program 7.py
File Edit Format Run Options Window Help
num1 = 10
num2 = 5
average = num1 + num2 / 2 Output
Average is: 12.5
print("Average is:", average)
The formula used to calculate the average is incorrect. Because of operator precedence, only num2 is divided by 2
before adding to num1. The correct average should be calculated as: (num1 + num2) / 2
The correct program is as follows:
Program.py
File Edit Format Run Options Window Help
num1 = 10
num2 = 5
average = (num1 + num2) / 2
print("Average is:", average)
Output
Average is: 7.5
To find logical errors, we need to test the programs with different input values, check intermediate outputs, and
carefully review the logic of the code. Correcting these errors requires analysing the program step by step and ensuring
that the instructions properly solve the given problem.
Data Processing in Python 459

