0% found this document useful (0 votes)
42 views9 pages

Control Flow: Conditional Blocks 1. Flow of Control

The document discusses different types of conditional statements in Python like if, if-else, and if-elif-else statements that allow executing different blocks of code based on whether a condition is true or false. It provides examples of using these conditional statements to check password matches, evaluate numbers, and make multiple decisions. The nested if-else statement allows placing if statements within other if-else blocks to make a series of decisions.

Uploaded by

dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views9 pages

Control Flow: Conditional Blocks 1. Flow of Control

The document discusses different types of conditional statements in Python like if, if-else, and if-elif-else statements that allow executing different blocks of code based on whether a condition is true or false. It provides examples of using these conditional statements to check password matches, evaluate numbers, and make multiple decisions. The nested if-else statement allows placing if statements within other if-else blocks to make a series of decisions.

Uploaded by

dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 3

CONTROL FLOW: CONDITIONAL BLOCKS

1. Flow of control
1. The order in which statements are executed is called the flow of control/execution
2. Execution always begins at the first statement of theprogram.
3. Statements are executed one at a time, in order, from top tobottom.
4. A program's control flow is the order in which the program's code executes. The
control flow of a Python program is regulated by conditional statements, loops,
and function calls. 
5. When the flow of control of a program is changed based on some condition using
control statements, it is termed conditional flow of control.

2. Boolean Values and Operators


A boolean expression is an expression that is either true or false. The following
examples use the operator ==, which compares two operands and produces True if
they are equal and Falseotherwise:

>>> 5 == 5

True

>>> 5 == 6

False

True and False are special values that belong to the type bool; they are not strings:

>>>type(True)

<class 'bool'>

>>>type(False)

<class 'bool'>

The == operator is one of the relational operators; the others are:

x != y # x is not equal to y

x > y # x is greater than y

x < y # x is less than y


x >= y # x is greater than or equal to y

x <= y # x is less than or equal to y

Note:All expressions involving relational and logical operators will evaluate to either
true or false

3. Conditional statements

In Python, condition statements act depending on whether a given condition is true or false.
We can execute different blocks of codes depending on the outcome of a condition.
Condition statements always evaluate to either True or False.

There are three types of conditional statements.

1. if statement
2. if-else
3. if-elif-else
4. nested if-else

4. If statement in Python

In control statements, The if statement is the simplest form. It takes a condition and


evaluates to either True or False.

If the condition is True, then the True block of code will be executed, and if the condition is
False, then the block of code is skipped, and The controller moves to the next line

Syntax of the if statement

if condition:
statement1
statement2
statement n
Let’s see the example of the if statement. In this example, we will calculate the square of a
number if it greater than 5

Example

number=6
if number >5:
# Calculate square
print(number * number)
print('Next lines of code')

Output

36

Next lines of code

5. If – else statement in python

The if-else statement checks the condition and executes the if block of code when the
condition is True, and if the condition is False, it will execute the else block of code.

Syntax of the if-else statement
if condition:
statement1
else:
statement2

If the condition is True, then statement 1 will be executed If the condition is False, statement
2 will be executed. See the following flowchart for more detail.

Example

password=input('Enter password ')

if password =="PYnative@#29":
print("Correct password")
else:
print("Incorrect Password")

Output 1:

Enter password PYnative@#29

Correct password

Output 2:
Enter password PYnative

Incorrect Password

6. Chain multiple if statement in Python

In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions


one after another. This is useful when you need to check multiple conditions.

With the help of if-elif-else we can make a tricky decision. The elif statement checks
multiple conditions one by one and if the condition fulfills, then executes that code.

Syntax of the if-elif-else statement:

if condition-1:
statement1
elif condition-2:
stetement2
elif condition-3:
stetement3
...
else:
statement

Flowchart of if – elif - else:


Example

defuser_check(choice):
if choice ==1:
print("Admin")
elif choice ==2:
print("Editor")
elif choice ==3:
print("Guest")
else:
print("Wrong entry")

user_check(1)
user_check(2)
user_check(3)
user_check(4)

Output:

Admin

Editor

Guest

Wrong entry

7. Nested if-else statement

In Python, the nested if-else statement is an if statement inside another if-else statement. It is


allowed in Python to put any number of if statements in another if statement.

Indentation is the only way to differentiate the level of nesting. The nested if-else is useful
when we want to make a series of decisions.

Syntax of the nested-if-else:

ifconditon_outer:
ifcondition_inner:
statement of inner if
else:
statement of inner else:
statementot outer if
else:
Outer else
statement outside if block

Example: Find a greater number between two numbers

num1 =int(input('Enter first number '))


num2 =int(input('Enter second number '))

if num1 >= num2:


if num1 == num2:
print(num1,'and', num2,'are equal')
else:
print(num1,'is greater than', num2)
else:
print(num1,'is smaller than', num2)

Output 1:

Enter first number 56

Enter second number 15

56 is greater than 15

Output 2:

Enter first number 29

Enter second number 78

29 is smaller than 78
8. Simple programs using Conditional statements

You might also like