Control structures in python

Control structures in python

Control Structures are just a way to specify flow of control in programs. They are used for non-sequential and repetitive execution of instructions.

if statement

In Python, if statement evaluates the condition as a boolean expression. If the condition evaluates to True, the block of code following the if statement is executed. However, if the condition evaluates to False, the code is ignored and the control is transferred out of the if statement block.

SYNTAX

if condition :
   statements s

FLOW DIAGRAM

image.png

EXAMPLE

image.png

if - else statement

The else statement is to specify the execution of a block of code if the condition in the if statement is false.

SYNTAX

if condition:
   statements s1
else:
   statements s2

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png

if - elif - else statement

The elif is short for else if and is useful to avoid excessive indentation. It is used to check and evaluate multiple expressions for True. There can be n number of elif statements or conditions.

SYNTAX

if condition:
  statements
elif condition:
  statements
else:
  statements

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png

What are loops?

The process of repetitive execution of a statement or a sequence of statements is called a loop. Execution of a sequence of statements in a loop is known as an iteration of a loop.

for loop

This loop is used to execute a sequence of statements or instructions for a fixed number of times.

SYNTAX

for value in sequence:
    statements

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png To loop through a set of code a specified number of times, we can use the range() function. image.png image.png

while loop

This loop is used for executing a sequence of statements again and again on the basis of some test condition. The body of the loop gets executed as long as the test condition holds True.

SYNTAX

while <test condition>:
    statements

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png

A while loop with a condition that always evaluates to true is called an infinite loop.

break statement

This statement can alter the flow of a normal loop. It enables us to exit the loop and transfer the control to next iteration of the loop. If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the innermost loop.

SYNTAX

break

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png

continue statement

This statement is used to transfer the control to next iteration of the loop. When it gets executed, the code that occurs in the body of the loop after the continue statement is skipped.

SYNTAX

continue

FLOW DIAGRAM

image.png

EXAMPLE

image.png image.png

pass statement

This statement is a null statement and executes no code. However, it is different from comments. The difference is that while the interpreter ignores a comment entirely, pass is not ignored.

SYNTAX

pass

EXAMPLE

image.png image.png

With this we come to an end of control structures. Stay tuned for more easy learning notes and keep practicing !!