Python - While LoopWhich of the following is the correct syntax to prevent an infinite loop in Python?Awhile i < 5: print(i) i += 1Bwhile i < 5 print(i) i += 1Cwhile i < 5: print(i) i += 1Dwhile i < 5: print(i) i = i - 1Check Answer
Step-by-Step SolutionSolution:Step 1: Check syntax correctnesswhile i < 5: print(i) i += 1 uses correct indentation and colon after while condition.Step 2: Verify loop variable updatewhile i < 5: print(i) i += 1 increments i, moving towards exit condition, preventing infinite loop.Final Answer:while i < 5: print(i) i += 1 -> Option AQuick Check:Correct syntax + variable update = prevent infinite loop [OK]Quick Trick: Indent and update loop variable properly [OK]Common Mistakes:MISTAKESMissing colon after while conditionIncorrect indentationNot updating loop variable or updating wrongly
Master "While Loop" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Conditional Statements - Why conditional statements are needed - Quiz 9hard Conditional Statements - Ternary conditional expression - Quiz 15hard Input and Output - Formatting using format() method - Quiz 13medium Operators and Expression Evaluation - Why operators are needed - Quiz 4medium Operators and Expression Evaluation - Comparison operators - Quiz 3easy Python Basics and Execution Environment - Why Python is easy to learn - Quiz 6medium Python Basics and Execution Environment - Python Block Structure and Indentation - Quiz 5medium Variables and Dynamic Typing - Why variables are needed - Quiz 15hard Variables and Dynamic Typing - Why variables are needed - Quiz 7medium While Loop - Counter-based while loop - Quiz 7medium