An indefinite loop, also known as an infinite loop, is a type of loop that continues to execute indefinitely as long as a certain condition remains true. It's important to exercise caution when using indefinite loops, as they can potentially lead to your program becoming unresponsive or crashing if not managed properly.while True: user_input = input("Enter a value (or 'exit' to quit): ") if user_input == 'exit': break print(f"You entered: {user_input}")
What is the purpose of a while loop?
How do you terminate a while loop?
What happens if the condition of a while loop is never true?
Break Statement
The break statement in Python is used to exit a loop prematurely. When the break statement is encountered within a loop (such as for or while), the loop immediately terminates, and the program continues executing the code after the loop.while condition: # loop code if some_condition: break # exit the loop # more loop codeOr within a for loop:for element in iterable: # loop code if some_condition: break # exit the loop # more loop codeHere's an example of using the break statement within a while loop:count = 0while True: print(f"Count: {count}") count += 1 if count >= 5: break # Exit the loop when count reaches 5
What is the purpose of the 'break' statement in programming?
Which programming construct does the 'break' statement typically belong to?
In which direction does the control flow transfer when a 'break' statement is encountered?
Continue Statement
A continue statement is used in programming to skip the remaining instructions within a loop iteration and proceed to the next iteration.
The continue statement is typically used in loops, such as for loops or while loops, where you want to continue the loop without executing the remaining code block for a particular iteration under certain conditions.
When a continue statement is encountered, it immediately jumps to the next iteration of the loop, thus skipping any code below it within that iteration. The loop updates its control variable and continues executing from the beginning of the loop again.
Example usage:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
console.log(i);
}
In this example, the loop iterates from 1 to 10. However, when the value of the loop variable i is even, the continue statement is triggered, and the loop moves on to the next iteration without executing the console.log statement. As a result, only odd numbers will be displayed in the console.
When is the 'continue' statement used in programming?
What happens when a 'continue' statement is executed in a loop?
Which programming construct is commonly associated with the 'continue' statement?