NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #4

Chapter 6 – Flow of Control

NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #4

Q #4 – What is an infinite loop? Give one example.

Ainfinite loop is a loop that continues to execute indefinitely without terminating. This occurs when the loop’s terminating condition is never met, often due to a logical error in the code or because the loop is intentionally designed to run forever until an external action occurs (like user input or a signal).

Characteristics of Infinite Loops

  • Never-ending Execution: The loop keeps running, executing its block of code repeatedly.
  • Common Causes: They can arise from mistakes in loop conditions, such as forgetting to update the loop variable or using a condition that is always true.
  • Intentional Use: Sometimes, infinite loops are used in applications that need to run continuously, such as server applications waiting for client requests.

Example of an Infinite Loop

Here’s a simple example in Python that demonstrates an infinite loop:

# Infinite loop example
while True:
    print("This will print forever until stopped.")

In this example, the while True condition always evaluates to true, causing the loop to run indefinitely. It will continue to print the message until the program is manually terminated (e.g., by pressing Ctrl + C in the console).

Another Example with a Logical Error

An infinite loop can also occur due to a logical error:

# Infinite loop due to logical error
i = 0
while i < 5:
    print(i)
    # Missing increment statement for i

In this case, since there is no statement to increment i, it will always be 0, and thus the condition i < 5 remains true, resulting in an infinite loop.

Summary

Infinite loops are often unintentional and can lead to programs becoming unresponsive. However, they can also be used purposefully in certain applications. Understanding how they work and how to control them is essential for effective programming.

NCERT Computer Science Chapter 6 Solutions Q 4

Posts created 29

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top