1st PUC Computer Science Chapter 6 Flow of Control Notes
Complete Short Notes & Exam Preparation Guide
1st PUC / Class XI Computer Science
Based on the NCERT textbook
Chapter Overview
Flow of control means the order in which statements in a program are executed. This chapter explains selection, indentation, repetition, for and while loops, range(), break, continue and nested loops.
Table of Contents
1. Introduction to Flow of Control
In a sequential program, Python executes statements one after another from the beginning to the end. Control structures are used when a program needs decision making or repetition.
Main Control Structures
- Selection
- Repetition
Sequential Program Example
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
diff = num1 - num2
print("The difference of", num1, "and", num2, "is", diff)
2. Selection
2.1 if Statement
if condition:
statement(s)
If the condition is true, the indented statement block is executed.
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")
2.2 if..else Statement
if condition:
statement(s)
else:
statement(s)
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Positive Difference
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
Decision-Making Flowchart
Flow chart depicting decision making
2.3 if..elif..else
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Positive, Negative or Zero
number = int(input("Enter a number: "))
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
Nested if
A selection structure placed inside another selection structure is called a nested if. The calculator example in the chapter uses nested conditions.
Simple Calculator Logic
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed.")
else:
result = val1 / val2
else:
print("Wrong input, program terminated")
3. Indentation
- Statements at the same indentation level belong to the same block.
- Python checks indentation strictly.
- Incorrect indentation may produce syntax errors.
- The body of an if or loop must be indented.
- Nested blocks require further indentation.
if num1 > num2:
print("first number is larger")
print("Bye")
else:
print("second number is larger")
print("Bye Bye")
4. Repetition
Loops avoid writing a large number of repeated statements. The chapter describes two looping constructs: for and while.
4.1 The for Loop
The for loop iterates over a range of values or a sequence. It executes its body for each item and then transfers control to the statement following the loop.
for <control-variable> in <sequence/items in range>:
<statements inside body of the loop>
Flow chart of for loop
Example
for letter in 'PYTHON':
print(letter)
Even Number Example
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num, 'is an even Number')
4.1.1 range() Function
range([start], stop[, step])
- Generates a sequence of integers.
- The stop value is excluded.
- start is optional.
- step is optional.
- Default start is 0.
- Default step is 1.
- step may be positive or negative, but not zero.
list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list(range(2, 10)) # [2, 3, 4, 5, 6, 7, 8, 9] list(range(0, 30, 5)) # [0, 5, 10, 15, 20, 25] list(range(0, -9, -1)) # [0, -1, -2, -3, -4, -5, -6, -7, -8]
4.2 The while Loop
while test_condition:
body of while
- The condition is checked before the body.
- If initially false, the body is not executed.
- The condition is checked again after every iteration.
- The condition should eventually become false.
- Otherwise an infinite loop may occur.
Flowchart of while Loop
Flow chart of while Loop
Example
count = 1
while count <= 5:
print(count)
count += 1
Infinite Loop
If the loop condition never becomes false, the loop may continue indefinitely. The chapter identifies this as a logical error.
5. Break and Continue Statements
5.1 break Statement
for num in range(10):
num = num + 1
if num == 8:
break
print('Num has value ' + str(num))
5.2 continue Statement
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
| break | continue |
|---|---|
| Terminates the current loop. | Skips the remaining part of the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Remaining iterations are not executed. | The loop continues with later iterations. |
6. Nested Loops
The chapter states that any type of loop can be nested within another loop. Nested loops are used for patterns and other repeated calculations.
Nested Loop Example
for i in range(1, num + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
Pattern for num = 5
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Important Uses in the Chapter
- Pattern generation.
- Finding prime numbers.
- Repeated calculations.
- Using different loop types together.
š Important Questions & Answers
ā” Quick Revision
Important Definitions
- Flow of control: Order of program execution.
- Selection: Decision making based on conditions.
- Indentation: Leading whitespace used to define blocks.
- Repetition: Repeated execution of statements.
- for: Iterates through a range or sequence.
- while: Executes while a condition is true.
- break: Exits the current loop.
- continue: Skips the current iteration.
- Nested loop: Loop inside another loop.
Most Important Syntax
if condition:
statement
if condition:
statement
else:
statement
if condition:
statement
elif condition:
statement
else:
statement
for variable in sequence:
statement
while condition:
statement
range([start], stop[, step])
Memory Trick
FLOW ā SELECT ā INDENT ā REPEAT ā LOOP ā CONTROL ā NEST
Flow of Control ā Selection ā Indentation ā Repetition ā for/while ā break/continue ā Nested loops
Programming Practice ā Chapter Focus
- Check eligibility using age.
- Print the table of a number.
- Find minimum and maximum of five numbers.
- Check leap year.
- Generate the sequence ā5, 10, ā15, 20, ā25ā¦.
- Find the sum 1 + 1/8 + 1/27 + ⦠+ 1/n³.
- Find sum of digits.
- Check palindrome.
- Generate star and number patterns.
- Find student grade according to the given percentage table.
- Create the menu-driven student-information case study described in the chapter.
Final Revision
š This article was researched and written by Venkatesh A, Founder of verakworld.com.