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

4 Notes

Chapter 4 covers conditional and looping constructs in programming, focusing on if-elif-else statements and their syntax in Python. It explains how to control the flow of execution based on conditions and introduces iteration through loops like for and while. Additionally, it distinguishes between simple, compound, and empty statements, providing examples and emphasizing the utility of algorithms and flowcharts in programming.

Uploaded by

v.kesavaraj8
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)
4 views3 pages

4 Notes

Chapter 4 covers conditional and looping constructs in programming, focusing on if-elif-else statements and their syntax in Python. It explains how to control the flow of execution based on conditions and introduces iteration through loops like for and while. Additionally, it distinguishes between simple, compound, and empty statements, providing examples and emphasizing the utility of algorithms and flowcharts in programming.

Uploaded by

v.kesavaraj8
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

CHAPTER 4: CONDITIONAL AND LOOPING # Block E

CONSTRUCTS 3. if-elif-else Statement


Introduction: Used to test multiple conditions. Example: Check number type and range
Control flow: Syntax: num = 15
Control statements in programming are if condition1:
used to control the flow of execution based # code if condition1 is True if num >= 0:
on conditions or repetitions. elif condition2: if num < 10:
if Statement in Programming # code if condition2 is True print("Number is between 0
else: and 9")
The if statement is a control structure used to
# code if none of the above are elif num < 20:
execute a block of code only if a specified condition
True print("Number is between 10
is true.
Example: and 19")
Basic Syntax (in Python):
marks = 75 else:
1. if condition:
print("Number is 20 or
# Code to execute if condition is True
if marks >= 90: more")
print("Grade: A") else:
Example: print("Number is negative")
elif marks >= 75:
x = 10
print("Grade: B") Output:
elif marks >= 60: Number is between 10 and 19
if x > 5: print("Grade: C") Explanation:
print("x is greater than 5") else:  First, it checks if num >= 0 (non-
Output: print("Grade: D") negative).
x is greater than 5  Inside that, it further checks the range of
num:
2. if-else Statement 4. Nested if-elif-else Statement in  < 10
Used to execute one block of code if a condition is Python  < 20
true, and another if it's false. A nested if-elif-else statement is when you  Else, it's 20 or more.
Syntax:
place one if-elif-else block inside another.  If the number is negative, the outer else
if condition:
This allows you to check multiple levels of conditions. handles it.
# code block if condition is
True Syntax:
if condition1: Iteration/Looping
else:
if condition1a: Iteration (or Looping) is a programming
# code block if condition is
# Block A concept where a block of code is repeatedly
False
elif condition1b:
Example: executed as long as a certain condition is
# Block B
age = 18 true or for a specific number of times.
else:
if age >= 18: Iteration means doing something again and
# Block C
print("You are an adult.") again, usually with slight changes each
elif condition2:
else: time.
# Block D
print("You are a minor.")
else:
Why It's Useful: Output:
 Automates repetitive tasks 1
 Reduces code duplication 2 S. Algorithm Flowchart
 Useful for working with lists, user input, files, 3 No
etc. 4 1. An algorithm is A flowchart is a
Types of Loops in Python: 5 a step-by-step diagram created with
Loop Type Description procedure to different shapes to
for loop Repeats over a sequence (e.g., list, Example 1 – for Loop: solve a show the flow of
string, range) for i in range(5): problem. data.
while print("Hello") 2. The algorithm A flowchart is easy to
Repeats while a condition is True
loop Output: is complex to understand.
Hello understand.
for Loop
Hello 3. In the In the flowchart,
Used for iterating over a sequence (like a list, string,
Hello algorithm, plain symbols/shapes are
or range).
Hello text is used. used.
Syntax:
for variable in sequence: Hello 4. The algorithm A flowchart is hard
# code block is easy to to debug.
Example: Example 2 – while Loop: debug.
for i in range(1, 6): count = 1 5. The algorithm A flowchart is simple
print(i) while count <= 3:
is difficult to to construct.
print("Count is:", count)
Output: construct.
1 count += 1
6. The algorithm The flowchart follows
2 Output:
does not follow rules to be
3 Count is: 1
any rules. constructed.
4 Count is: 2
7. The algorithm A flowchart is just a
5 Count is: 3
is the pseudo- graphical
while Loop
code for the representation of
Repeats a block of code as long as a condition is true. Flowchart: program. that logic.
Syntax: A flowchart is a graphical representation of
while condition: an algorithm. Programmers often use it as a
# code block program-planning tool to solve a problem. It
Example: makes use of symbols that are connected
count = 1 among them to indicate the flow of
while count <= 5: information and processing.
print(count)
count += 1 Algorithm:
An algorithm is a step-by-step procedure to
solve a problem.
1. Simple Statement 3. Empty Statement (pass)

A simple statement is a single line of code that Sometimes, you need to write a statement
performs a single action. syntactically, but don’t want it to do anything.
That's where pass comes in.
Examples:
x = 10 # Assignment Example:
print(x) # Function call if x > 5:
y=x+5 # Expression pass # Placeholder for future code

You can also write multiple simple statements This is useful when:
on one line using semicolons (not recommended
for readability):  You’re planning to add code later
 You're defining an empty class or function
a = 1; b = 2; print(a + b)
More Examples:
def my_function():
2. Compound Statement pass

A compound statement contains one or more class MyClass:


blocks of code and controls execution flow (like pass
conditionals and loops). It includes a header
(ending with :) and an indented body.
Summary Table:
Examples: Type Description Example
# if statement Simple Single instruction x = 10
if x > 5: Statement
print("x is greater than 5") Compound Multiple lines, with if, for,
Statement indentation while, def
# for loop Empty Does nothing; pass
Flowchart: To find simple interest. for i in range(3): Statement used as
print(i) placeholder
In Python, a statement is a line of code that
performs an action, such as assigning a value, # function definition
calling a function, or controlling the flow (with if, def greet():
for, etc.). print("Hello!")

Structure:
Python statements fall into three categories:
<keyword> <condition>:
# Block of code (indented)

You might also like