0% found this document useful (0 votes)
11 views3 pages

Computer Chapter 1 Notes

The document introduces control structures in Python, which dictate the flow of program execution through sequential, conditional, looping, and jumping statements. It details three main types of control structures: Conditional Statements (if, if-else, if-elif-else), Looping Statements (for, while), and Jumping Statements (break, continue), providing examples and explanations for each. Mastering these structures is essential for writing efficient and flexible Python programs.

Uploaded by

Nivriti Gupta
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)
11 views3 pages

Computer Chapter 1 Notes

The document introduces control structures in Python, which dictate the flow of program execution through sequential, conditional, looping, and jumping statements. It details three main types of control structures: Conditional Statements (if, if-else, if-elif-else), Looping Statements (for, while), and Jumping Statements (break, continue), providing examples and explanations for each. Mastering these structures is essential for writing efficient and flexible Python programs.

Uploaded by

Nivriti Gupta
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/ 3

Introduction to Control Structures In Python, the instructions in a program usually run one after the other, in the order

they are written. This is


known as a sequential control structure. The statements that manage this flow are called control flow statements.

Types of Control Structures


Python provides three main types of control structures to make programs flexible and efficient:
●​ Conditional Statements
●​ Looping Statements
●​ Jumping Statements

1. Conditional Statements (Decision Making)


Conditional statements, also known as decision-making statements, execute a set of instructions only if a specific condition is met. They help
your program make choices. For instance, a program could check an employee's salary to decide whether they need to pay tax. Python has the
following types of conditional statements:
●​ if
●​ if-else
●​ if-elif-else
●​ nested if

The if Statement

The if statement is the simplest decision-making statement. It executes a block of code only when the given condition is TRUE. If the condition
is FALSE, it simply does nothing and skips the block.
●​ A quick note on Indentation: Python uses whitespace at the beginning of a line (indentation) to define a block of code, unlike other
languages that might use curly brackets.
Code Example:
a = 10​
b = 20​
if a < b:​
print("a is smaller than b")​
print("End of if")​

Explanation: In this example, the condition a < b (10 is less than 20) is TRUE. So, the print statement inside the if block is executed. The final
print statement runs regardless, as it is not part of the if block.

The if-else Statement

While the if statement only acts on a TRUE condition, the if-else statement provides an alternative action for when the condition is FALSE. If the
condition is TRUE, the code inside the if block runs. If it's FALSE, the code inside the else block runs.
Code Example: Find the greater number
a = 10​
b = 20​
if a > b:​
print("a is greater than b")​
else:​
print("b is greater than a")​

Explanation: Here, the condition a > b (10 > 20) is FALSE. Therefore, the program skips the if block and executes the else block, printing that
"b is greater than a".

The if-elif-else Statement

Sometimes you have more than two possibilities. The if-elif-else statement lets you check multiple conditions. elif is Python's way of saying, "if
the previous conditions were not true, then try this condition." You can have as many elif blocks as you need. The else block at the end is
optional and runs if none of the preceding if or elif conditions are met.
Code Example: Grading based on marks
marks = float(input("Enter Marks in Percentage: "))​
if marks >= 60:​
print("First Division")​
elif marks >= 45 and marks < 60:​
print("Second Division")​
elif marks >= 33 and marks < 45:​
print("Third Division")​
else:​
print("Fail")​

Explanation: This program checks the marks against several conditions in order. If marks were 58.75, it would fail the first if condition, but
meet the first elif condition, printing "Second Division" and then stop.
The Nested if Statement

A nested if is simply an if statement inside another if statement (or elif/else block). This is useful when you need to test a combination of
conditions.
Code Example: Find the biggest of three numbers
a = int(input("Enter value for A: "))​
b = int(input("Enter value for B: "))​
c = int(input("Enter value for C: "))​

if a > b:​
if a > c:​
print("A is Biggest")​
else:​
print("C is Biggest")​
elif b > c:​
print("B is Biggest")​
else:​
print("C is Biggest")​

Explanation: This code first checks if a is greater than b. If it is, it then enters a nested if-else to compare a with c. If a is not greater than b, it
moves to the elif to compare b and c.

2. Looping Statements (Iterative Statements)


Looping statements, also called iterative statements, are used when you need to execute a block of code multiple times. Instead of writing the
same statements over and over, you can use a loop. The loop will repeat a block of code as long as a certain condition is met. Python has two
main types of loops:
●​ while loop
●​ for loop

The while Loop

A while loop executes a set of statements as long as its condition remains TRUE. The condition is checked at the beginning of each iteration.
Once the condition becomes FALSE, the loop stops.
Code Example: Printing natural numbers
a = 1​
while a <= 10:​
print(a)​
a = a + 1​

Explanation: This loop starts with a = 1. The condition a <= 10 is TRUE, so it prints a and then increments a by 1. This process repeats until a
becomes 11. At that point, the condition is FALSE, and the loop terminates.

The for Loop

The for loop in Python is used to iterate over the items of any sequence, like a list or a string, one by one. It's different from the C-style for loop
and works more like a "for each" loop found in other languages.
Code Example: Iterating over a string
str = "Welcome to Python"​
for i in str:​
print(i)​

Explanation: In this example, the for loop takes each character from the string str one at a time and assigns it to the variable i. It then prints the
character. The loop continues until all characters in the string have been processed.

The range() Function

The range() function is often used with for loops to generate a sequence of numbers. The syntax is range(start, stop, step).
●​ start: The first number in the sequence (optional, defaults to 0).
○​ stop: The number where the sequence ends (this value is not included in the output).
●​ step: The difference between each number in the sequence (optional, defaults to 1).
Code Example: Using range()
# Prints numbers 0 to 9​
for i in range(10):​
print(i)​

# Prints numbers from 2 up to 7​
for i in range(2, 8):​
print(i)​

# Prints even numbers from 0 up to 8​
for i in range(0, 10, 2):​
print(i)​
Finite vs. Infinite Loops

●​ Finite Loop: A loop that has a clear termination condition and eventually stops. All the examples above are finite loops.
●​ Infinite Loop: A loop where the termination condition is never met. This causes the loop to run forever, which can make your program
unresponsive.
Infinite Loop Example:
while True:​
print("This is an infinite loop!")​

Nested Loops

A nested loop is a loop inside another loop. The inner loop will complete all its iterations for each single iteration of the outer loop. You can nest
any combination of for and while loops.
Code Example: Nested for loop
for i in range(1, 4): # Outer loop​
for j in range(1, 4): # Inner loop​
print(i * j, end=" ")​
print() # Move to the next line​

Explanation: The outer loop runs for i = 1, 2, and 3. For each value of i, the inner loop runs completely for j = 1, 2, and 3, printing the product of
i and j.

3. Jumping Statements
Jumping statements are used to alter the normal flow of a loop. They can be used to terminate a loop prematurely or skip a part of it. Python
supports two jumping statements:
●​ break
●​ continue

The break Statement

The break statement immediately terminates the current loop it's in, and the program execution resumes at the very next statement after the
loop. It can be used in both for and while loops.
Code Example: break in a for loop
for i in range(10):​
if i == 7:​
print("break")​
break​
print(i)​
print("Out of loop body")​

Explanation: This loop prints numbers from 0. When i becomes 7, the if condition is TRUE. The word "break" is printed, and the break
statement is executed, which immediately terminates the for loop. The program then jumps to and executes the final print statement.

The continue Statement

The continue statement is used to skip the rest of the code inside the current iteration of a loop. The loop does not terminate but continues with
the next iteration. It can also be used in both for and while loops.
Code Example: continue in a for loop
for i in range(6):​
if i == 3:​
continue​
print(i)​

Explanation: This loop prints the value of i in each iteration. However, when i is 3, the if condition is TRUE, and the continue statement is
executed. This causes the print(i) statement to be skipped for that iteration, and the loop proceeds directly to the next value, which is 4.

Summary
Control Structures are fundamental tools in Python that allow you to dictate the flow of your program's execution.
●​ By default, Python follows a Sequential flow, executing code line by line.
●​ Conditional Statements (if, if-else, if-elif-else) allow your program to make decisions. They execute specific blocks of code only if certain
conditions are true, enabling different outcomes based on different inputs.
●​ Looping Statements (for, while) are used for repetition. They execute a block of code multiple times, which is essential for tasks like
processing items in a list or repeating an action a specific number of times. The for loop is great for iterating over sequences, while the
while loop is ideal for repeating as long as a condition holds.
●​ Jumping Statements (break, continue) provide finer control within loops. break allows you to exit a loop entirely, while continue lets you
skip the current iteration and move to the next one. Mastering these structures is key to writing flexible, efficient, and powerful Python
programs.

You might also like