0% found this document useful (0 votes)
12 views

CH 6 FLOW OF CONTROL-Conditional Statements

Chapter 6 discusses the flow of control in Python programming, explaining how programs can execute in different orders using control structures such as if-else, for, and while statements. It categorizes statements into empty, simple, and compound types, and outlines three control structures: sequential, selective, and iterative. The chapter also details decision-making constructs like if, if-else, and elif statements with examples for practical understanding.

Uploaded by

ai.crimson158
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CH 6 FLOW OF CONTROL-Conditional Statements

Chapter 6 discusses the flow of control in Python programming, explaining how programs can execute in different orders using control structures such as if-else, for, and while statements. It categorizes statements into empty, simple, and compound types, and outlines three control structures: sequential, selective, and iterative. The chapter also details decision-making constructs like if, if-else, and elif statements with examples for practical understanding.

Uploaded by

ai.crimson158
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CHAPTER-6

FLOW OF CONTROL
Generally, a program executes from beginning to end. Some
programs do not execute in order. As per the requirements, execution order of the program can be
changed and it is also possible to execute a program repeatedly.
Python provides control structures to manage the order of
execution of a program, which are if-else, for, while and jump statements like break and continue.

Types of Statements
There are three types of Statements provided by Python.
❖ Empty Statement
❖ Simple Statement
❖ Compound Statement
Statements are the instruction given to the computer to perform any kind of action, be it data
movements, making decisions or repeating actions.

Empty Statement
Empty Statement or null operation statement is the simplest statement which does nothing in
Python.
It takes the form of ‘pass’ statements.

Simple Statement
Any single executable statement is a simple statement in Python.
Eg. name=str(input(“Enter your name:”))

Compound Statement
A Compound Statement represents a group of statements executed as a unit. In Python, Compound
Statements have headers ending in colon, and a body containing a set of statements to be executed.
Eg. if x>y:
print(x)
➢ A Compound Statement has a header line which begins with a keyword and ends with a colon.
➢ It is followed by a body, which consists of one or more Python statements, each indented
inside the header line.
Statement Flow Control
Statements can be executed in three different ways or three types of control structures are there:

1. Sequential
The statements will be executed sequentially (one after the other), which represents the default flow
of control. It is the normal flow of control in a program, where Python program begins with the first
statement, followed by the second, till the final statement of the program is executed. Sequence
Statement Flow is the simplest flow of control.

Statement 1

Statement 2

Statement 3

2. Selective
The Selection construct was a condition test to determine the execution of statements. If a condition
is True, a set -of -statements is followed. Otherwise, a different set of statements is followed. This
construct is also called decision construct because it helps in making a decision about which set- of -
statements is to be executed.
TRUE FALSE
Condition?

Statement 1 Statement 3

Statement 2 Statement 4
3. Iterative (Looping)
The iteration construct means repetition of a set – of – statements depending upon a condition test.
The statement gets executed till the time a condition is True. As soon as the condition becomes False,
the repetition stops.

TRUE FALSE
Condition?
Once False, the loop will exit

Statement 1

Statement 2

Decision Making and Branching


There are three types of conditions in Python:

1. if-Statement
2. if-else Statement
3. elif – Statement

if-Statement
It is also called Selection Construct. The if statement selects a particular condition, if the condition
evaluates to True, a course of action is followed i.e. a set of statements are executed. Otherwise, another set of
statements gets executed.

In if statement, if we use True, it refers to the Boolean value True, as well as truth value true tval.
Similarly, False refers to truth value False tval and the Boolean value False.

Syntax:

if<conditional expression>:

Statement
Eg. ch=input (“Enter a single character:”)

If ch==’ ‘:

print (“You entered a space”)

if ch>=’0’ and ch<=’9’:

print (“Entered character is a digit”)

Q. Write a conditional expression in Python for the following:


1. To compare a number ‘a’ with another number ‘b’. If the number is greater than ‘b’, then print the number is greater
than b.

if a>b:

print (“a is greater than b”)

2. To check the variable ’x’ and print ‘x has truth value as true’ and the second statement as hence you can see this
message.

if x:

print (“x has truth value as true”)

print (“hence you can see this message”)

3.To check if grade is A and then print ’You did well’.

if grade==’A’

print (“You did well”)

if-else Statement
The if-else statement tests a condition. If the condition evaluates to True, a set of instructions will be executed, and if it
evaluates to False, another set of instructions are executed.

Syntax:

if<conditional expression>:

Statement

else:

Statement

Eg. if a>0:

print (a, “is a positive number”)

else:

print (a,” is a negative number”)


Q 1. Write a program to check whether a number is odd or even.

num=int(input(“Enter an integer:”))

if num%2==0:

print (num,” is an even number”)

else:

print (num,” is an odd number”)

Q 2. Write a program to compare three numbers and give the largest of the three.

x=float (input (“Enter the first number:”))

y=float (input (“Enter the second number:”))

z=float (input (“Enter the third number:”))

max=x

if y>max:

max=y

if z>max:

max=z

print (“Largest number is “, max)

Q3. Write a program to check whether a number is divisible by a second number.

x=int(input(“Enter the first number:”))

y=int(input(“Enter the second number:”))

if x%y==0:

print(x,”is divisible by “,y)

else:

print(x,”is not divisible by”,y)

Q4. Write a program to calculate the area or circumference of a circle.

radius=float (input (“Enter the radius of the circle:”))

print (“””1-Calculate Area

2-Calculate Perimeter”””)

ch=int(input(“Enter your choice 1 or 2:”)

if ch==1:

area=22/7*radius*radius

print(“Area of the circle of given radius is “,area)

else:

perm=2*22/7*radius
print(“Perimeter of the circle with the given radius is:”, perm)

if-elif Statement
It is the short form of else-if statement. If the previous conditions were not True, then do this condition.

Syntax:

if<conditional expression>:

Statement

elif<conditional expression>:

Statement

elif<conditional expression>:

Statement

else:

Statement

Eg: a=int(input(“Enter a number”))

if a=10:

print(“a is equal to 10”)

elif a=20:

print(“ a is equal to 20”)

elif a=30:

print(“a is equal to 30”)

else:

print(“Again give the number”)

Q1. Write a program to calculate the absolute value of a number or an integer using if-elif conditional statement.

num=float(input(“Enter the number”))

if num>=0:

print(num, ”is the absolute value”)

elif num<0:

print(num*-1,”is the absolute value”)

Q2. Write Python code using if-elif conditional statements for the following table.

Sales Discount
>=30,000 18%
>=20,000 15%
>=10,000 10%
<10,000 5%
sales=float(input(“Enter amount of sales”))

if sales>=30000:

discount=sales*0.18

elif sales>=20000:

discount=sales*0.15

elif sales>=10000:

discount= sales*0.10

else:

discount=sales*0.05

Q3. Write a program that reads two numbers and operator and displays the computation.

num1=float(input(“Enter the first number:”))

num2=float(input(“Enter the second number:”))

op=input(“Enter operator[+-*/%]:”)

if op==’+’:

result=num1+num2

elif op==’-‘:

result=num1-num2

elif op==’*’:

result=num1*num2

elif op==’/’:

result=num1/num2

elif op==’%’:

result=num1%num2

else:

print(“Invalid operator!”)

print(num1,op,num2,’=’,result)

Q4. Write a program to print the type of a given character.

char=input(“Enter a single character:”)

if char>=’A’ and char<=’Z’:

print(“You entered an uppercase character”)

elif char>=’a’ and char<=’z’:

print(“You entered a lowercase character”)

elif char>=’0’ and char<=’9’:


print(“You entered a digit”)

else:

print(“You entered a special character”)

You might also like