Loop Control Statement

Loop Control Statement

Hello Friends, I am Nirav. And in this tutorial I going to show you Loop Control Statement in Python..
So, Let's get Started


What is Loop ?

          Loop is a sequence of instruction that is continually repeated until a certain condition is reached.
          Ex:- For example one teacher punish boy that  write something until she don't tell him to stop.  That is one loop that boy can't stop until teacher can't tell him to stop

Why we use loop statement ?

          To execute one think many times.
Ex:- if you want to print "hello" ten times than you have to use loop

Types of loop

 There are mainly 2 types of loop
          1.) Entry Control 
          2.) Exit Control

1.) Entry Control

           An entry control loop check the condition first and then after enter the loop body.
          Ex:-  For Loop , While Loop

2.) Exit Control

           An Exit Control loop execute body first then after check condition 
          Ex:- Do...While Loop

While Loop

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
We generally use this loop when we don't know beforehand, the number of times to iterate.
Ex:- In Below Example It will gives addition of 0 to 10 numbers.

In the above program, the test expression will be True as long as our counter variable i is less than or equal to n (10 in our program).
We need to increase the value of counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never ending loop).
Finally the result is displayed.

Break and continues statement in while loop

1.) Break
          Simple Break means stop something. Hear break is in sense of stop the program at some certain condition. with using break statement.
Ex:- We have while loop and you want break when your i will be 2 then...


In upper example i will be increase every time but when it's reach to  2 then if condition will be true and break statement execute and it will print only 1 and 2.

2.) Continues
            Simple continues means skip and go ahead. Hear Continues will be skip loop one time and it will start ahead.
Ex:- We have while loop and you want to skip 2 once so you have to use continues.

In upper Example it will skip loop one time when i will be 2 and after that it will execute program.

For Loop

          The for loop in Python is used to iterate over a sequence (listtuplestring) or other iterable objects. Iterating over a sequence is called traversal.

Range function

          Before starting for loop you have to learn range function.

          Range meaning what

                   Range means something in rage between some boundary.

           Ex:- range(10) = {0,1,2,3,4,5,6,7,8,9}
                  range(3,8) = {3,4,5,6,7}
                  range(5,20,2) = {5,7,9,11,13,15,17,19}


Now For loop

           Ex:- if you want to print Hello! 5 times than..


          Ex:- Factorial value of 10 by using for loop


You also visit my you tube video regarding to this Topic

https://youtu.be/eZjZnrAIYuk


                 

Comments

Popular posts from this blog

Recursion and lambda function in python

Boolean Expression and If..else Condition