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

Chapter_6

Chapter 6 discusses the flow of control in Python, detailing the order of execution of statements and the three main control structures: sequential flow, selection/conditional flow, and looping. It explains various selection statements such as simple if, if-else, and if-elif-else, as well as looping constructs like for and while loops. Additionally, it covers the importance of indentation, the range() function, and the differences between break and continue statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter_6

Chapter 6 discusses the flow of control in Python, detailing the order of execution of statements and the three main control structures: sequential flow, selection/conditional flow, and looping. It explains various selection statements such as simple if, if-else, and if-elif-else, as well as looping constructs like for and while loops. Additionally, it covers the importance of indentation, the range() function, and the differences between break and continue statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter -6

FLOW OF CONTROL
1. What is Flow of control in python?
➢ The order of execution of the statements in a program is
known as Flow of control.
➢ It can be implemented using control structures.

2. What are the flow controls/ control structure used in python?


➢ There are three types of control structures used in python.
i. Sequential Flow
ii. Selection/Conditional Flow
iii. Looping /repetition/iteration

3. Write a note on sequential flow. Give an example.


➢ The sequential execution of codes or statements take place
one after another from beginning to end.
➢ It is not possible to exclude any statement from the
execution
➢ It consumes more time and leads to more complexity in
larger programs.
Example:
a=int (input (“Enter the value of a:”))
b=int (input (“Enter the value of b:”))
c=a+b
print(c)

Krithika Sivakumar, Department of Computer Science


4. What is Selection/conditional Fow?
➢ Block of statements need to be included or excluded as
per the requirement of the program. This can be done by
using selection statements.
➢ The execution of block of statements take place based on
the specified condition. (True/False).

5. What are the various selection statements used in python?


➢ Simple if statement.
➢ If else statement.
➢ If elif…else statement.

6. Explain simple if statement with syntax and an example.


➢ It is a simple decision control instruction.
➢ If the given condition is True, then all the indented
statement(s) are executed,
➢ Otherwise, control goes out of if condition.
Syntax:

if condition:
statement(s)

Example:

n=int (input (“Enter the number”))


if n>0:
print(“Positive number”)

Krithika Sivakumar, Department of Computer Science


7. Explain if...else statement with syntax and an example.
➢ It is an extension/variant of if statement.
➢ Here, the alternate block for the if statement will be
available.
➢ It executes either of the block depends on the condition
given.
Syntax:
if condition:
statement(s)
else:
Statement(s)

Example:
n=int (input (“Enter the number:”))
if n>0:
print (“Positive number”)
else:
print (“Negative number”)

8. Explain if…elif...else statement with syntax and example.


➢ It is used to check multiple conditional in the program.
➢ Number of elif is dependent on the number of conditions
to be checked.
➢ If the first condition is false, then the next condition is
checked, and so on.
Krithika Sivakumar, Department of Computer Science
➢ If one of the conditions is True, then the corresponding
indented block executes, and if statement terminates.
➢ If no conditions is True, then the else block executes.
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)

Example:
n=int (input (“Enter the number:”))
if n>0:
print (“Positive number”)
elif n==0:
print(“ZERO”)
else:
print (“Negative number”)

Krithika Sivakumar, Department of Computer Science


9. What is Indentation? How important it is in python
programming? Explain.
➢ Indentation is very important concept in python
programming, because the interpreter checks indentation
levels very strictly and throws up syntax error if it is not
correct.
➢ In most programming languages, the block of statements is
put inside the curly brackets, but python uses indentation.
➢ Indentation use a single tab space for each level.
➢ Before beginning of indentation, (:) symbol is used.
➢ Python uses uniform indentation to mark block of
statements.

10. What is Iteration/Repetition/looping in python?


➢ It is an ability to repeat the set of statements based on
certain condition given.
➢ The repetition can be done zero or more number of times
depending on requirement of the program.
➢ This is also called iteration/looping.
➢ This can be implemented by setting initial value, step
value/condition and stop value followed by block of
statement(s).
➢ The block of statement(s) under the looping will execute
repeatedly until the condition becomes false.
➢ If the stop value is not mentioned in the loop, it becomes
infinite loop.

Krithika Sivakumar, Department of Computer Science


11. Name two looping constructs used in python.
➢ for loop
➢ while loop

12. Explain the Range () in python.


➢ The range() is a built-in function in python.
➢ It is used to create a list containing a sequence of integers
from the given start value upto stop value(excluding stop
value),with a difference of the given step value.
➢ Syntax:
Range([start],stop[,step])
➢ Here, Start, stop and step are parameters
➢ Start and step are optional.
➢ Default value of starting value is 0 and step value is 1.
➢ All parameters value should be in integer.
➢ Eg. for i in range(1,10)

13. Explain for loop with syntax and example.


➢ for loop is used to iterate over a range of values or a
sequence.
➢ The values can be either numeric or any other data types
like string, list etc.
➢ In every step of iteration, the control variable checks
whether each of the values in the range have been
traversed or not.

Krithika Sivakumar, Department of Computer Science


➢ When all the items in the range are checked, loop will be
terminated and control goes the statement present after
the for loop.
➢ To use the for loop, it is known in advance that the number
of times the loop will execute.
Syntax:
for<control_variable> in <sequence/items in range>:
<statement(s) inside the loop>

Example:
for i in range (1,10):
print(i)

Flowchart:

Krithika Sivakumar, Department of Computer Science


14. Explain while loop with syntax and an example.
➢ It is used to execute a block of statements repeatedly until
a given condition is True.
➢ If the condition becomes False, control terminates from
the while loop and execute the next statements.
➢ Here, the condition is tested before the execution of
statements present inside the loop.
➢ It is also called pretested loop.
Syntax:
while test_condition:
statement/s

Example:
i=0
while i<=5:
print(i)
i+=1

Krithika Sivakumar, Department of Computer Science


15. Differentiate break and continue statement.
Break statement Continue Statement
➢ It is used to alter the ➢ It is used to skip the
normal flow of current iteration and jumps
execution, and to the next iteration in the
terminates the current loop
loop. ➢ The control remains in the
➢ The control transfers same loop.
outside of the loop. ➢ The keyword ‘continue’ is
➢ The keyword ‘break’ is used.
used. ➢ Eg:
➢ Eg: for i in range(10):
for i in range(10): if i==5:
if i==5: continue
break print(i)
print(i)
Flowchart for continue:
Flowchart for break:

Krithika Sivakumar, Department of Computer Science


16. Explain nested loop with example.
➢ A loop inside another loop is called a nested loop.
Example:

for i in range (3):


print(“I”)
for j in range(3):
print(“j”)
print(“loop end”)

********************************************

Krithika Sivakumar, Department of Computer Science

You might also like