Control flow : The control flow is the order where the PC executes statements or instructions in a program. Code is run all together from the main line in the record to the last line, except if the PC stumbles into the (very successive) structures that change the control flow, for example, conditionals and loops
Function Calls, Conditional statements and Loops is all are basic control flows in python.
Python has basically following control structures:
* Repetition: This type of control structure used for repeating part of any program or code multiple times.
* Selection: This type of control structure used for getting some decisions and branching the statements.
* Sequential: This type of control structure executes by default.
Sequential Statements:
Sequential Statements are a bunch of articulations whose execution interaction occurs in an arrangement. The issue with Sequential Statements is that assuming the rationale has broken in any of the lines, the total source code execution will break.
Example:
number_1 = 30
number_2 = 10
add = number_1 + number_2 #it adds number_1 + number_2 and prints addition of both numbers
print("Addition is=", add)
OUTPUT of above code:
Addition is= 40
Selection Controls:
Selection controls also called as branching statements or some times decision control statements.
The selection statements permits a program to test a few conditions and execute guidelines dependent on which condition is valid.
Decision Control Statements are:
* Simple if
* Nested If
* if-else
* if-elif-else
Simple if Statement:
If statements are control flow statements help us with running a particular code, anyway exactly when a particular condition is met or satisfied. A direct if simply has one condition to check.
Syntax:
if test condition:
# if statements to be executes
# condition is true
as shown in above flow chart if condition is true then conditional code is executed and if condition is false then it terminates the code.
Example:
num = 20
if num %2 == 0:"
print("This is even number")
OUTPUT of above code:
This is even number
Nested If Statements:
It is placement of if statement inside another if statement...That means we can place if statement inside into another if statements to check multiple conditions.
Syntax:
if (condition1):
# executes when condition1 is true.
if (condition2):
# Executes when condition2 is true
#if block is end here
#if block is end here
as shown in above flow chart if condition1 is false then it executes its respective else statement and if condition2 is false then it executes its respective else statements otherwise it executes true part of its statements
Example:
i=20
if(i==20):
# First If Statement
if(i<30):
print("i is smaller than 30")
# Nested if statement
# it will only be executes if statements above
#its true
if(i<25):
print("i is smaller than 25 too")
else:
print("i is greater than 30")
OUTPUT of above code:
i is smaller than 30
i is smaller than 25 too
if-else statement:
if else statement is used to check or to select any condition based on true or false...it means if condition is true then if statement is executes otherwise if is not true then else statement is executes
Syntax:
if test Expression:
Body of if Statement
else:
Body of else statement
As shown in above flow chart if test expression is true then it executes body of if / statements of if otherwise that means it false then body of else part is executes.
Example:
i=20
if(i<25):
print("i is smaller than 25")
else:
print("i is greater than 25")
OUTPUT of above code:
i is smaller than 25
if-elif-else Statement:
Here, a person can settle on different alternatives. The if explanations are executed starting from the top. When one of the conditions controlling the if is valid, the explanation related with that if is executed, and the remainder of the stepping stool is circumvent. On the off chance that none of the conditions is valid, the last else explanation will be executed.
Syntax:
if(condition):
# statement
elif(condition):
# statements
.
.
.
else:
#statements
As shown in above flow chart test expression of if is executes when it is true. when this expression is false then it executes test expression of elif inside of that condition if its true then it executes body of elif. in case test expression of elif is false then body of else is executes. and finally terminates the statements.
Example:
i=20
if(i == 5):
print("i is 5")
elif(i ==10):
print("i is 10")
elif(i ==20)
print("i is 20")
else:
print("i is not present in this")
OUTPUT of above code:
i is 20
Repetition:
Repetition statements are called loops, and are utilized to rehash a similar code on various occasions in progression in programs. such types of loops are reduced repetition of code means reusability of code so that we need not to write code again and again basically it saves our time and memory as well !!
basically python has following loops or repetitive statements:
* For loop
* while loop
for loop:
A "For" Loop is utilized to rehash a particular square of code a known number of times.
it repeats on different iteration until it's anything but met to its given fulfilled condition
Syntax:
for iterator_variable in sequence :
# Statements to be executes...
As shown in above flow chart of for loop it iterates that means it checks code again and again until its not satisfied by the given condition if condition is true then conditional code is checks and repeat and if condition is false then for loop comes out of loop.
Example:
fruits = ["orange", "mango", "banana"]
for x in fruits:
print(x)
OUTPUT of above code:
banana
while loop Statement:
While Loops is utilized to execute a block of statements
more than once until a given condition is fulfilled. Also,
when the condition turns out to be false, the line following
the loop in the program is executed.
While loop falls under the class of uncertain iteration.
As shown in above flow chart its an while loop flow chart so if condition is true then statements or body of while loop is executes otherwise if condition is false then it comes out of the while loop.
Example:
m= 4
i= 0
while i<m:
print(i, end="")
i = i+1
print("End")
OUTPUT of above code:
0 1 2 3 End
Comments
Post a Comment