CONTROL FLOW STATEMENTS

In this article we will learn about control statement in Python language. It is very important to control the program execution .

statement-loops

reference: https://data-flair.training/blogs/

What are control flow statements ?

The word control flow has two words in it , control and flow here control means simply controlling and flow , flow is a way to sequence a program execution.

Types of control flow statements

If statement

The if statements are used to check a condition .if true then loop executes .

Syntax:

Example:

Output:

If else statement

if else statements has two blocks of codes, each for if and else.

Syntax:

Example:

Output:

if .. elif .. else statement

So far in if statement and if else statements, we can only check one condition or expression

Syntax:

Example:

Output:

Nested if else statement 

In nested if else statements, if statement is nested inside an if statemnts. So, the nested if statements will be executed only if the expression of main if statemnt returns TRUE.

Syntax:

Example:

Output:

For statement

for loop is used for repeated execution of a group of statemnts for the desired number of times. It iterates over the items of lists, tuples, strings, the dictionaries and other iterable objects.

Syntax:

Example :

Output:

While loop

Python while loop keeps reiterating a block of code defined inside it until the desired condition is met.

The while loop contains a Boolean expression and the code inside the loop is repeatedly executed as long as the Boolean expression is true.

Syntax:

Example:

Output

The break statement

The break statemnt , it is used to terminate while loop or for loop in Python.

It interrupts the flow of the program by breaking the loop and continues the execution of code which is outside the loop.

Syntax:

Example:

Output:

The Continue statement

Continue statemnt  skips the remaining statements in the body of that loop and continue the next iteration of the loop.

Unlike break statement, continue statemnt when encountered doesn’t terminate the loop, rather interrupts a particular iteration only

Syntax:

Example:

Output :

Use of loops in python

The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop which runs up to 10 iterations.

Advantages of loops

  • It provides code re-usability.
  • Using loops, we do not need to write the same code again and again.
  • Using loops, we can traverse over the elements of data structures (array or linked lists).
  • looping is what makes computer programming worthwhile.
  • reduces length of code.

 

https://docs.python.org

https://www.javatpoint.com

https://data-flair.training/blogs/python-loop/

%d bloggers like this: