CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium

A developer is creating a program to simulate a simple traffic light. The program needs to cycle through 'Red', 'Yellow', and 'Green' states repeatedly. Which control structure is most appropriate for ensuring this continuous cycle?

  1. AIf-else statement
  2. BInfinite loop
  3. CFor loop
  4. DDo-while loop
Show answer & explanation

Correct answer: B. Infinite loop

An infinite loop (often implemented using a 'while True' or 'for ;;' construct) is specifically designed to run indefinitely or until explicitly broken. This is ideal for processes that need to run continuously, like a traffic light cycle in a simulation, without a predefined end condition.

Why the other options are wrong

  • A. An if-else statement is for conditional execution, not repetition.
  • C. A for loop iterates a fixed number of times or over a collection, not endlessly.
  • D. A do-while loop repeats as long as a condition is true, but implies a condition that could eventually become false.

Infinite Loop

An infinite loop is a control flow statement that iterates indefinitely because the loop's termination condition is never met. It is intentionally used for processes that are meant to run continuously, such as server applications or simulations.

  • Runs without a natural end condition
  • Requires explicit break or external termination
  • Common in simulations or daemon processes

Memory trick: Continuous cycle? Infinite Loop, no finale!

More Software Development Concepts questions