Tamil Nadu HSE First Year Computer Applications study materials
Tamil Nadu HSE First Year Computer Applications study resources

Bus Fare Slabs and a Loop That Never Ends

Tamil Nadu HSE First Year Computer Applications · Put your knowledge to work in context.

All case studies

Bus Fare Slabs and a Loop That Never Ends

A school bus contractor charges monthly fares by one-way distance: up to 5 km ₹400, more than 5 km and up to 10 km ₹650, more than 10 km and up to 20 km ₹900, and above 20 km ₹1,200. A student writes a pseudocode program to process 30 students:

SET count = 1 WHILE count <= 30 READ distance IF distance <= 5 THEN fare = 400 IF distance <= 10 THEN fare = 650 IF distance <= 20 THEN fare = 900 IF distance > 20 THEN fare = 1200 PRINT fare END WHILE

For a student travelling 4 km, the program prints 900. For a student travelling 7 km, it also prints 900. The student cannot see why the program gives the same fare for different distances, and the program never stops.

1. Trace the given pseudocode for distances 4, 7, 12 and 23. Explain why the output is wrong, then rewrite the decision logic correctly using nested IF.

Read the worked answer

Trace:

  • 4 km: first IF true → fare=400; second IF true → fare=650; third IF true → fare=900; fourth false. Output 900. Correct fare is 400.
  • 7 km: first false; second true → 650; third true → 900; fourth false. Output 900. Correct fare is 650.
  • 12 km: first and second false; third true → 900; fourth false. Output 900, which happens to be correct.
  • 23 km: first three false; fourth true → 1200. Output 1200, correct.

The fault is that the IF statements are independent. For a small distance, several conditions are true and each later true condition overwrites fare. The conditions must be exclusive, or tested in order with ELSE so that later tests are skipped.

Correct nested IF:

IF distance <= 5 THEN
  fare = 400
ELSE IF distance <= 10 THEN
  fare = 650
ELSE IF distance <= 20 THEN
  fare = 900
ELSE
  fare = 1200
END IF

Because the tests run from smallest to largest, a 7 km distance fails the first test, succeeds at the second and never reaches the third.

2. The loop never terminates. Explain the cause and give two different corrections, one using WHILE and one using FOR. State which is more suitable here and why.

Read the worked answer

count is set to 1 but is never changed inside the loop. The condition count <= 30 therefore remains true forever, and the program keeps reading distances and printing fares.

WHILE correction: add an increment before the loop returns to the condition.

SET count = 1
WHILE count <= 30
  READ distance
  ... calculate fare ...
  PRINT fare
  SET count = count + 1
END WHILE

FOR correction: use a counted loop, which updates the counter automatically.

FOR count = 1 TO 30
  READ distance
  ... calculate fare ...
  PRINT fare
NEXT count

FOR is more suitable because the number of students is known in advance - exactly 30. It removes the risk of forgetting the increment and makes the intended repetition count clear. WHILE would be preferable if the number of students were not known and the loop had to stop on a sentinel value.

3. Suggest three test distances that would expose the original decision-logic fault, and state the expected fare and the faulty program's output for each.

Read the worked answer

Good tests include boundary values and values just above a boundary, because those exercise the overlapping conditions.

  1. 5 km - expected ₹400; original program prints ₹900 because all three lower conditions are true and overwrite the fare.
  2. 7 km - expected ₹650; original program prints ₹900 because the 10 km and 20 km conditions both fire.
  3. 10 km - expected ₹650; original program prints ₹900 because the 20 km condition also fires.

A fourth useful test is 20 km, expected ₹900, which the original program happens to get right. That is important: a test that passes does not prove the logic is correct. Testing only 12 km or 23 km would also fail to reveal the fault, because those distances trigger only one true condition.