Robotics & Artificial Intelligence
How many times will the following loop execute? What will be the output?
count = 0
while count < 3:
print("Hello")
count += 1
Answer
Number of times the loop will execute: 3 times
Output
Hello
Hello
Hello
Explanation
- The variable
countis initialised to 0. Thewhileloop runs as long ascount < 3is true. - Each time the loop executes,
"Hello"is printed and the value ofcountis increased by 1. - When
countbecomes 3, the conditioncount < 3becomes false and the loop stops.
Related Questions
What will be the output from the code given below:
a, b = 3, 5 c = a * 2 + b / 2 print(c * 2)A student executes the following program segment and the answer displayed is the wrong output. Name the error. How can the program be modified to get the correct answer?
x = 8, y = 2 if (x == y) print("both are unequal") else: print("both are equal")Predict the output of the following code:
flag = False if flag: print("True") else: print("False")Write the output of the following code:
nums = [1, 2, 3] print(nums) print(type(nums))