In python the execution of code is sequentially it means the code(statement) written first will execute first by compiler and the second statement follow the first statement
But some time user want to run a block of code several times for that Python has various control structures. A loop statements simply help user to execute a statements or group of statements
1.How many loop Structures are there in Python ?
_Python supports basic loop structure through iterative statements . Iterative statements are decision control statements that are used tor repeat the execution of a list of statements. Python supports two types of iterative statements WHILE LOOP and FOR LOOPS .
1.While loop:
With the help of while loop user can repeat the execution of code up to condition is true .In the while loop the condition will check first if that is true then only it will continue with the execution and if not the compiler comes out from that loop . After one iteration it will again check for condition and this process will repeat till condition will not became false
Syntax :
while (condition) :
statement block
The brackets for condition //expression are optional. We can skip unlike other programming languages like c, c++ and java where brackets() are compulsory.
Now the question arises that how the loop will end or how many statements will be there in while loop
because in other language we use the curly brackets {} to define a body of loop but in Python
the body of any loop is determined by indentation ,It means to end the loop body we just have to break the indentation .
Example: Print Multiplication table of 22 ?
CODE _
x=1
while x <=10 :
print(24*x)
x+=1
In the above example as long as the value of x is less than or equal to 10 While loop will repeat the execution but when it get false i.e. value of x ==11 the compiler will come out from loop , indentation defines the body of loop .It's compulsory to increase the value of the counter variable otherwise the loop will execute for an infinite number of times .
While Loop with else :
While loop also have a else block ,the else block execute when the condition become false
we can use the control statements like break in that case user can avoid the else block.
e.g.
x=1
while x <=10 :
print(24*x)
x+=1
print("the table is over")
So, while loop is also referred to as a top checking loop since control condition is placed as the first line of the code. If the control condition evaluate to false ,the the statements are enclosed in the loop are never executed .
As we have discuss about while loop it required certain condition but this is not in case of for loop
For loop is work with sequence ,The sequence like list, tuple , string and sets .Body of for loop is separated by indentation from other statements (code) same as in the while loop . The increment / Decrement of an counter variable is done by the for loop it self in Python .Even we don't need to initialize the variable .Basically when a user want to print elements in sequential data type one by one ,for loop is used.
Syntax :
for expression :
statements /Body
e.g.
1. x = ["Python ","java " , "C"]
for i in x :
print (i)
//output
"Python "
"java "
"C"
2.
for a in range (1,10):
while a %2!=0
print (a)
In the second example we use the range() function. this function is use to define a range ,a value that we write first it will be its starting point and the second one is the end point . But the end point is is not included in a range , above example range (1,10) means it start from 1 and end at 9
we can define an interval between two values e.g. range (1,100,2) so now difference between two numbers is 2 like 1,3,5,7,9.
When a for loop is used, a range of sequence is specified .The items of the sequence are assigned to the loop control variable one after other. The for loop is executed for each item in the sequence. With every iteration of the loop , a check is made to identify whether the loop control variable has been assigned all the values in the range .If all the values have been assigned , the statement block of the loop is executed else, the statements comprising the statement block of the for loop are skipped and the control jumps to the immediate statement following the for loop Body.