Sunday, June 11, 2023

Chapter 3: Control Structures: Conditionals and Loops

 


3.1 Conditional Statements 3.1.1 if statement and its syntax Explanation: The if statement allows you to execute a block of code based on a certain condition. Example:

python

///Example

num = 10

if num > 0:

    print("The number is positive.")

 

3.1.2 else and elif statements Explanation: The else statement provides an alternative block of code to execute when the condition in the if statement is False. The elif statement allows you to check multiple conditions. Example:

python

///Example

num = 0

if num > 0:

    print("The number is positive.")

elif num < 0:

    print("The number is negative.")

else:

    print("The number is zero.")

 

3.2 Looping Constructs: for and while Loops 3.2.1 The for loop: iterating over sequences, lists, and ranges Explanation: The for loop allows you to iterate over a sequence or a range of values, executing a block of code for each iteration. Example:

python

///Example

fruits = ["apple", "banana", "orange"]

for fruit in fruits:

    print(fruit)

 

3.2.2 The while loop: loop continuation conditions and control flow Explanation: The while loop executes a block of code repeatedly as long as a certain condition is True. Example:

python

///Example

count = 0

while count < 5:

    print("Count:", count)

    count += 1

 

3.3 Loop Control Statements 3.3.1 Using the break statement to exit loops prematurely Explanation: The break statement allows you to exit a loop prematurely, skipping the remaining iterations. Example:

python

///Example

fruits = ["apple", "banana", "orange"]

for fruit in fruits:

    if fruit == "banana":

        break

    print(fruit)

 

3.3.2 Using the continue statement to skip iterations Explanation: The continue statement allows you to skip the current iteration of a loop and move on to the next iteration. Example:

python

///Example

numbers = [1, 2, 3, 4, 5]

for number in numbers:

    if number % 2 == 0:

        continue

    print(number)

 

3.4 Exception Handling with try and except 3.4.1 Understanding exceptions and error handling Explanation: Exceptions are events that occur during the execution of a program that disrupt the normal flow of the program. Error handling allows you to catch and handle these exceptions gracefully. Example:

python

///Example

try:

    x = 10 / 0

except ZeroDivisionError:

    print("Error: Cannot divide by zero.")

 

3.4.2 Handling specific exceptions and multiple exceptions Explanation: You can handle specific exceptions by specifying the exception type. Multiple exceptions can be handled using multiple except blocks. Example:

python

///Example

try:

    num = int(input("Enter a number: "))

    result = 10 / num

except ValueError:

    print("Error: Invalid input. Please enter a valid number.")

except ZeroDivisionError:

    print("Error: Cannot divide by zero.")

 

This chapter covers the fundamentals of control structures, including conditional statements and looping constructs. It also introduces exception handling for handling errors and unexpected situations. The examples provided demonstrate the usage and syntax of each topic, helping readers understand how to apply these concepts in their own programs.

0 comments:

Post a Comment