Lesson 6 - Loops (While), Break, Continue

Hi,

So today we learn loops. Many times we have some scenario where we need to repeat the steps.

Here we come with solution called Loops.

Python have "While" & "For" loop.

While Loop

While loop executed until the condition remains true.

In while loop we initiate the variable that controls the counter for loop. Then apply the condition and inside the loop increment the counter by 1 or depends on your requirement.

Example

i = 1
while i <= 10:
    print ("Do some coding")
    i++

So first it check the conditions

i <= 10 -> 1 <= 10 -> True -> Print -> Do some coding

And so on until the condition is true. Above examples shows "Do some coding" 10 times.

We can use below keywords inside loop to control the execution.

1. Break
2. Continue

Break

If our conditioned satisfied and we didn't need to execute the remaining we use break. It stops the execution and get the control out from loop.

Continue

We use "continue" keyword to control the execution but the difference is only after satisfying the condition it didn't skips the remaining just move the control to condition part of loop.

See you in lesson 7 :-) Happy coding














Comments

Popular posts from this blog

Python Lesson 4