0% found this document useful (0 votes)
6 views2 pages

Chapter 6 Flow of Control

Chapter 5 discusses the flow of control in programming, which refers to the order of execution of statements. It explains two types of control structures in Python: selection and repetition, along with the importance of indentation for defining code blocks. The chapter also covers various selection statements such as if, if-else, and if-elif-else, emphasizing the need for consistent indentation to avoid errors.

Uploaded by

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

Chapter 6 Flow of Control

Chapter 5 discusses the flow of control in programming, which refers to the order of execution of statements. It explains two types of control structures in Python: selection and repetition, along with the importance of indentation for defining code blocks. The chapter also covers various selection statements such as if, if-else, and if-elif-else, emphasizing the need for consistent indentation to avoid errors.

Uploaded by

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

Chapter 5 – Flow of Control

Introduction to flow of control


The order of execution of the statements in a program is known as flow of control.
The flow of control can be implemented using control structures.

Python supports two types of control structures:

 Selection
 Repetition

Program 1: Program to print the difference of two numbers.


num1 = int (input ("Enter first number: "))
num2 = int (input ("Enter second number: "))

diff = num1 - num2

print ("The difference of", num1, "and", num2, "is", diff)

Output:
Enter first number 5

Enter second number 7

The difference of 5 and 7 is -2

Selection Statements (Decision Making)


Selection statements allow the program to make decisions and execute specific code
blocks based on conditions.
if Statement

 Executes a block of code only if a given condition is true.


if-else Statement

 Executes one block of code if the condition is true, otherwise executes


another block.
if-elif-else Statements

 Used for multiple conditions, where each condition is checked sequentially.


Indentation
What is Indentation?

 Indentation in Python means adding spaces at the beginning of a line.

 It is used to show which statements belong to a block of code.


 Example: In an if statement or loop, all the code inside must be indented.

Why is Indentation Important?

 Python does not use {} or other markers for blocks of code. It relies on
indentation to understand the structure of the program.

 Incorrect indentation can cause errors.

Rules for Indentation

1. Use the same number of spaces for all statements in a block.

2. Standard practice is to use 4 spaces per level of indentation.

3. Mixing spaces and tabs will cause errors.

Example

if True:
print("Hello") # Indented: Part of the 'if' block
print("World") # Indented: Still part of the 'if' block
print("Outside") # Not indented: Outside the 'if' block

You might also like