Sunday, September 23, 2018

Python Ninja Bootcamp 18-while, break, continue

Learn Python like a Professional! Take You from 0 to Hero






In this Python lecture, we will learn about the while loop, break, continue.



"while loop in Python is used to iterate over a block of code as long as the condition is true".


Let's see some example-


Create while loop



#  Display print the 'Hello' 3-times 
count=0
while count < 3 :
    print('Hello')
    count += 1
>> Hello
      Hello
      Hello


# Display and increment the value of variable on each iteration by 1
x=0
while x < 5 :
    print('current value of x=',x)
    x += 1
>>  current value of x= 0
      current value of x= 1
      current value of x= 2
      current value of x= 3
      current value of x= 4

Break & continue

# break statement is used to terminate the loop
x=0
while x < 5 : 
    print('current value of x=',x) 
    if x==2:
        print('\nAlert!! x is equal to 2')
        break
    x += 1
>> current value of x= 0
      current value of x= 1
      current value of x= 2

      Alert!! x is equal to 2


# #Continue forces the next iteration of the loop to take place, skipping any code in between.
x=0
while x < 5 : 
    
x += 1
if x==2: continue
    print('current value of x=',x)     
>>  current value of x= 1
      current value of x= 3
      current value of x= 4
      current value of x= 5




EDITS ARE WELCOMED!!

In the next Blog, we will discuss range 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-19-range.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/
 

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 




Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment