Control Structure in Python

  • If some condition or requirement makes the body of the code work or execute multiple times then we can say that condition controls the body of the code.
  • Condition which control the flow of the code are called as Control Structures.

There are three types of Control Structures :

  1. Sequential :
  • Default way in which any program works.
  • It works in sequence(line by line).

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. : -

  • if Control Structure : -

Syntax :

if condition:
statement

example: -

  • If-else Control Structure : -

Syntax :

if condition:
statement
else:
statement

example : -

  • Nested if else or if-elif-else : -

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. : -

  • for loop : -

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 loop : -

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 :

  • pass : -

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

  • break :-

break the flow of code

  • continue : -

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…..

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Complete CDK for a Module-Federated Micro-Frontends AWS Architecture

Find the duplicate number-coding question asked by Meta, JPMC

Using a Raspberry Pi as HDMI camera

Let’s get started!

What Did I Do When My Website Server Crashed?

Review of Designing Microservices platform with NATS by Chanaka Fernando

How to reduce your AWS cost — Part 1

Using TTY-Prompt (gem), in a CLI App

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
Rahul Kumar

Rahul Kumar

More from Medium

Learning Programming with simple language: Python, Part 2

Python Anonymous/Lambda Function

Python Socket Timeouts

Random numbers in Python

Random in Dockyard