In a program processing a list of numbers, the objective is to print all numbers until a negative number is encountered. Which control statement would be apt for this task?

  • break
  • for
  • if
  • while
A while loop is apt for this task because it allows you to repeatedly check if the current number is negative and, if not, print it. When a negative number is encountered, the loop terminates, ensuring only non-negative numbers are printed.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *