Resources | Subject Notes | Computer Science
This section explores the concepts of nested selection statements and various types of iteration statements in programming. Understanding these concepts is crucial for creating programs that can handle complex decision-making and repetitive tasks.
Nested selection occurs when one selection statement (e.g., an `if` statement) is placed inside another selection statement. This allows for more intricate decision-making processes where the outcome of one condition determines whether to evaluate another set of conditions.
Consider a scenario where you need to check if a number is both positive and within a specific range. You would use nested `if` statements to achieve this.
Example: Determine if a number is between 10 and 20 (inclusive) and also greater than 5.
Code Example (Python):
def check_number(number): if number > 5: if number >= 10 and number <= 20: print("The number is between 10 and 20 and greater than 5.") else: print("The number is greater than 5 but not between 10 and 20.") else: print("The number is not greater than 5.") check_number(15) check_number(4)
Explanation: The outer `if` checks if the number is greater than 5. If it is, the inner `if` checks if it's also between 10 and 20. The nested structure ensures that both conditions must be true for the final message to be printed.
Iteration statements allow a block of code to be executed repeatedly. The most common types are `for` and `while` loops.
A `for` loop is used to iterate over a sequence (e.g., a list, string, or range of numbers). It executes the code block for each item in the sequence.
Syntax (Python):
for item in sequence: # Code to be executed for each item
Example: Print the numbers from 1 to 5.
Code Example (Python):
for i in range(1, 6): print(i)
A `while` loop continues to execute as long as a specified condition is true. It's useful when the number of iterations is not known in advance.
Syntax (Python):
while condition: # Code to be executed while the condition is true
Example: Print numbers from 1 to 5.
Code Example (Python):
i = 1 while i <= 5: print(i) i += 1
Break: The `break` statement is used to terminate the loop prematurely, regardless of whether the loop condition is still true.
Continue: The `continue` statement skips the rest of the current iteration and proceeds to the next iteration of the loop.
Selection and iteration statements can be combined to create more sophisticated programs. For example, you might use a loop to iterate through a list of items and then use a selection statement to process each item based on certain conditions.
Example: Find all even numbers in a list.
Code Example (Python):
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = [] for number in numbers: if number % 2 == 0: even_numbers.append(number) print("Even numbers:", even_numbers)
Nested selection allows for complex decision-making, while iteration statements enable repetitive tasks. Understanding how to combine these concepts is essential for writing effective and efficient computer programs.
Concept | Description | Example |
---|---|---|
Nested Selection | An `if` statement within another `if` statement. | Checking if a number is positive and within a range. |
For Loop | Iterates over a sequence. | Printing numbers from 1 to 5. |
While Loop | Repeats as long as a condition is true. | Printing numbers from 1 to 5. |
Break Statement | Terminates a loop. | Exiting a loop when a specific condition is met. |
Continue Statement | Skips the current iteration. | Moving to the next iteration without executing the remaining code. |