Control Structure in Python

There are three types of Control Structures :

for e.g. :

a=10 #first a with a value of 10 will be taken
b=20 # b with a value of 20
c=a+b #addition will happen and the result gets stored in c.

In the above code a sequence is followed from the first value to addition and storing.

2. Selection:-

The selection control structure allows one set of statements to be executed if a condition is true and another set of actions to be executed if a condition is false.

e.g. : -

Syntax :

if condition:
statement

example: -

Syntax :

if condition:
statement
else:
statement

example : -

Syntax : -

if condition:
statement1
elif:
statement2
else:
statement3

example : -

3. Iterative : -

Some part of the code that gets to execute until a condition is not false. That part of the code is called as loop.

e.g. : -

this is also Finite loop in python.

Syntax :

for val in sequence:
loop body

for sequence we are using range() function in python.

definition of range() :

range(start, stop, step)start -> Optional. An integer number specifying at which position to start. Default is 0.
stop -> Required. An integer number specifying at which position to stop (not included).
step -> Optional. An integer number specifying the incrementation. Default is 1.

While is called as Entry control loop here the condition in the while loop will make sure when the loop will work if the condition is true the loop works otherwise it will not work.

while is also called Infinite loop.

Syntax :

while condition:
Body of while

example :

Infinite loop :

when we need to create a empty block {non operating block}

break the flow of code

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.

continue shifts the control to the next iteration.

continue forces the next iteration to come ahead.

Thanks for reading…..

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store