Junior — Middle
Can you explain what is meant by 'cycle' in programming?
sobes.tech AI
Answer from AI
In programming, a loop is a construct that allows repeating a block of code multiple times while a certain condition is true. Loops are used to automate repetitive tasks, such as iterating over list elements or performing operations a specified number of times.
In Python, there are several types of loops:
for— iterates over elements of a sequence (lists, strings, ranges, etc.).while— executes code as long as the condition is true.
Example of a for loop:
for i in range(5):
print(i)
This code will output numbers from 0 to 4.
Example of a while loop:
count = 0
while count < 5:
print(count)
count += 1
Loops help avoid code duplication and make programs more concise and understandable.