Loop and Selection Structure :
If Statement :
If statement is a selection statement .In if statement it will check the condition first the enter it if it is true .In other language like c++ the syntax is same .If statement is contain sequence of statement and execute sequentially .In if statement we can put one or more then one condition using ‘and’ and ‘or’ .
if test_cond: < if body > elif test2: < then if body> elif test3: <then if body> else: <else body>
In if statement the test_cond if the condition is true the it enter in 1st if body if it not true then goes to elif and then goes to 2nd statement check test 2 and then check it if non of these are not true then it automatically goes to else statement.
if any of these are true then that part are executed then program is come out the if block entirely
lets see how its work
while loop :
while is typically about executing a block of statement over a over again till a time given statement is true . Now this given statement can be a expression which evaluate true or false .This means while statement is combination of a loop and a condition checker is a sequence of statement or a instruction that continuously repeat until the condition is false. In while loop it check the condition first then enter into while body.
while loops can also have a else in python ,break statement continue statement is also can be use in python different statement can be used for different purpose
we can put break statement inside the loop then interpreter come to break statement the it can exit from loop
while <condition> : <statement> if<condition>: <statement> else: <statement>
for loops :
for loop is a sequence of iterator. For loop is work on tuples , String ,List and others
for <target> in <object>: <for body or statement> else: <statement>
when for loop is executing it can assign the item in the sequence of objects to target one by one and executing the statement in side the for at every time. In python language else is also used in for loop you can also use break and continue statement are used/
for <condition>: <statement> if <condition>: break #exit the loop now if <condition>: continue #goto top of the loop else: <statement>