The if-else Statement
The if-else statement extends the functionality of the if statement by providing an
alternative path of execution when the condition is evaluated as false. This
ensures that a block of code runs regardless of whether the condition is true
or false.
Syntax:
Example:
Here, the program prints the eligibility message if the age is 18 or older. Otherwise,
it prints the alternative message.
The if-elif-else Ladder
The if-elif-else ladder provides a clean and readable structure for situations
requiring multiple conditions to be checked. This construct evaluates
conditions sequentially, executing the first block whose condition evaluates
to true. If none of the conditions are true, the else block runs.
Syntax:
Example:
In this example, the program evaluates the conditions in order. If marks is greater
than or equal to 90, it assigns Grade A. If not, it checks the next condition
until a matching condition is found. If no condition matches, the else block
provides the default grade.
Looping Statements
Looping statements in Python allow you to execute a code block based on a
condition repeatedly. They are essential when performing repetitive tasks like
iterating through a list, processing data, or running calculations.
Python provides two primary loops: the for loop and the while loop. Additionally,
Python has a unique feature—loop control with others—that executes a code
block after a loop ends, provided it wasn’t terminated prematurely. Let’s dive
deeper into these concepts.
The for Loop
The for loop iterates over a sequence, such as a list, tuple, string, or range. It
processes each item in the sequence one by one, making it perfect for
situations where the number of iterations is predefined.
Syntax:
Example:
In this example, the for loop iterates over each item in the fruits list, printing a
message for each fruit.
The while Loop
The while loop executes a code block if a specified condition remains true. It is ideal
for scenarios where the number of iterations depends on dynamic conditions
rather than being predetermined.
Syntax:
Example:
In this example, the while loop increments the counter variable until it exceeds 5.
The loop ensures the “Count: x” message is printed five times.
Loop Control with Else
Python’s loops can include an else clause, which executes after the loop completes
all iterations. However, if the loop is terminated prematurely using a break
statement, the else block will not execute.
Syntax:
Example with a for Loop:
Here, the loop iterates through numbers. Since 5 is not in the list, the else block
executes after the loop completes.
Example with a while Loop:
This while loop increments x until it reaches 3. When the condition becomes false,
the else block executes, printing “Loop finished.”
Jump Statements
Jump statements in Python allow you to control the flow of loops and conditional
blocks by altering the standard execution sequence. They provide a way to
interrupt or bypass parts of the code without exiting the program.
Python offers three key jump statements: break, continue, and pass. Each serves a
distinct purpose, and understanding their use is essential for writing clean
and efficient code.
The break Statement
The break statement is used to exit a loop prematurely. When the break statement
is encountered inside a loop, the program immediately stops the current
iteration and exits the loop, even if the loop’s condition hasn’t been fully
satisfied.
Syntax:
Example:
Explanation:
In this example, the loop iterates through numbers from 1 to 9. When the
value of the number reaches 5, the break statement exits the loop, and the
program stops printing further numbers.
The continue Statement
The continue statement is used to skip the current iteration of a loop and proceed
directly to the next iteration. It doesn’t terminate the loop but bypasses the
remaining code in the current cycle.
Syntax:
Example:
Explanation:
In this example, the loop iterates through numbers from 1 to 9. When the
number is even, the continue statement skips the print statement and
proceeds to the next iteration, effectively printing only the odd numbers.
The pass Statement
The pass statement is a placeholder that does nothing when executed. It is often
used as a temporary stand-in for code that will be added later or to define an
empty block where syntax requires some content. Unlike break and
continue, it doesn’t alter the execution flow but serves as a no-operation
command.
Syntax:
Example:
Explanation:
Here, the pass statement is used when the number equals 3, effectively doing
nothing for that condition. For all other numbers, the program executes the
pr