# Decision Control Statement in Python

oday we are going to familiar with the concept of Decision control statement in Python. Here you will learn about -

• What is Decision Control Statement
• Types of Decision control Flow?
• If
• If -Else
• If-elif Statement
• What is Loop?
• Why we need to use a loop?
• Types of Loops

# What is Decision Control Statement?

Decision Control statement is a statement that determines the control flow of a set of instructions. means D.C.S decides the sequence in which instruction in the program is to be executed.

The three fundamental methods of control flow in a programming language are -

1. Sequential Control
2. Selection Control
3. iterative Control

Here we will learn about only two methods of control flow.

# Sequential Control

-When the program is executed line by line means from the first line to the second line then from the second line to the third line and so on. This method is called Sequential Control.

# Selection Control Statement

- When we execute only a selected set of statements then we use the Selection control statement. It usually jumps from one part of the code to another depending on whether a particular condition is satisfied or not.

In Selection Control Statement we learn about -

• If statement
• If -else statement
• If-elif-else statement

# If Statement

- The if statement is the simplest form of decision control statement that is frequently used in decision making.

# Syntax of If Statement

if (test_expression) :

statement1

…………..

statement n

statement x

# Program to increment a number if it is positive

x = 10 # Initialize the value of x

if (x>0): # test the value of x

x = x+1 # Increment the value of x if it is > 0

print(x) # print the value of

# If — else Statement

The use of If — else statement is very simple. When you run your program, The test expression is evaluated and if the result is True, the statement followed by the expression is executed, else if the expression is False, the statement followed by the expression is executed.

Syntax of If — else Statement

if ( test expression ) :

statement block 1

else :

statement block 2

statement x

# If — elif — else statement

Python supports if — elif — else statements to test additional conditions apart from the initial test expression. The if- elif — else constructs works in the same way as usual to if-else statement. One more thing to remember that it is not necessary that every if statement should have an else block as python supports simple if statements also.

# Syntax of If — elif — else statement

if ( test expression 1):

statement block 1

elif ( test expression 2 ):

statement block 2

…………………………

elif ( test expression N ):

statement block N

else:

statement block X

Statement Y

# Introduction of Loop

- A loop is a programming function that repeats a statement or condition according to the specific condition.

The loop repeats a certain statement until the condition given by the programmer is True.

Let’s understand the loop through an example -

Suppose you went to the market to purchase a T-shirt. Now, you will visit each and every single shop until you get your desired T-shirt and when you find it you return to your home.

That’s how loop work, It will also perform the task until the condition will not be met.

# Why we need to use a loop?

Now, for understanding the use of loop let’s take also an example -

Here, You need to purchase some fruits from markets. You have the list of fruits in which you have to purchase -

• Mango
• Guava
• Banana
• Apple
• Water-melon

Now, you have to option to purchase all these fruits -

1. You told an individual person to purchase an individual fruit from the market. Like — You told you, dad, to buy mango and to Mom, you said to buy banana.
2. And, the other option is, you go to the market and purchase all fruits on your own.

It is not wrong if I said you choose the second option right.

Now, the use of a loop is also like this. Look at the two different programs to solve the same problem.

In both pictures, we are print the Table of a certain number with loop or without loop

On the Very first program, we choose option 1 of our example and called different print functions for doing the same task. But, In our second program, we apply our second option and perform all activity by only one print function.

so, this the use of loop I hope you will understand it.

# Types of Loop

In python programming language, loops are usually three types -

1. For loop
2. While loop
3. Nested loop

# What is For Loop?

- Basically ‘ for ‘ is a keyword of the python programming language used for repeat a task until a specific condition becomes True.

Syntax of ‘ for ‘ loop -

for < variable > in sequence:

statement block

An example of ‘ for ‘ loop

In the above example, we take a variable name, ‘ i ‘. which stores the value and with the help of print function it shows us the first 10 natural numbers.

Now, the above example we use a function name range ()

Range() Function

- The range() is a pre-defined function in python programming language used to iterate over a sequence of numbers. The syntax of range() is :

range( beginning, end, [step] )

NOTE:- One important thing about range function is, it prints one less value of its last value. means if you write a program like — range( 1, 10) then, it only prints the number from 1 to 9.

# What is While Loop?

The while loop also works the same as for loop. The difference is only their syntax.

## Syntax of while Loop

statement x

while ( condition ):
statement block

statement y

An Example of while loop

In the above example, we print the first 10 natural numbers using a while loop.

At very first we initialize a variable named ‘ i ‘ and store a value 1 in it. After that, we create a while loop and put a condition over there that the loop works continuously until the value of ‘ i ‘ is not greater than 10 and print the all value of ‘ i ‘ until condition is not True.

# Nested loop

- Nested loop is nothing but a loop under a loop means you can also execute a loop under another loop.

see this example :

So, in the above example, you can see that how we can use a nested loop.

# What is break Statement and continue Statement

The break statement is used to terminating the execution of the loop at a specific point. For example -

So, in the above example, we execute a program in which we give a statement to run until the value of ‘ i ‘ is not equal to 10 but after that, we realized we only need 4 value of ‘ i ‘ then we use break command to terminate the program.

Continue Statement — When we add a continue statement in our loop then the condition written under the ‘ continue ‘ statement will not be shown and execute. For example -

You can see that in the above example the value ‘5’ is not shown in the output.

Just for coders, by Coder.

Just for coders, by Coder.

## FFmpeg Tool with Flutter

Get the Medium app