1 Answers
Absolutely! It's smart to focus on iteration and selection, as they're the building blocks of almost any algorithm. Let's get you squared away with a concise guide and then test your knowledge!
Quick Study Guide
- Iteration (Loops):
- Definition: The process of repeating a block of code multiple times until a specific condition is met or a certain number of repetitions is completed.
- Purpose: Automating repetitive tasks, processing collections of data (e.g., arrays, lists), performing calculations iteratively.
- Common Constructs:
forloop: Used when the number of iterations is known in advance (definite iteration). Example:for (int i = 0; i < 10; i++) { // code }whileloop: Used when the number of iterations is unknown, and the loop continues as long as a condition is true (indefinite iteration/condition-controlled). Example:while (condition) { // code }do-whileloop: Similar towhile, but guarantees the loop body executes at least once before checking the condition.
- Key Idea: 'Do this repeatedly.'
- Selection (Conditionals):
- Definition: The process of executing different blocks of code based on whether a specified condition evaluates to true or false. It allows an algorithm to make decisions.
- Purpose: Controlling program flow, handling different scenarios, implementing business logic, validating input.
- Common Constructs:
ifstatement: Executes a block of code only if a condition is true. Example:if (score > 90) { // code }if-elsestatement: Executes one block of code if the condition is true, and a different block if it's false. Example:if (age >= 18) { // adult code } else { // minor code }if-else if-else(orelifin Python): Allows for multiple conditions to be checked sequentially. Example:if (grade == 'A') { ... } else if (grade == 'B') { ... } else { ... }switchstatement: Provides a way to select one of many code blocks to be executed based on the value of a single variable or expression.
- Key Idea: 'If this is true, do that; otherwise, do something else.'
- Together: Iteration and selection are often used in conjunction. For example, a loop might contain an
ifstatement to process items differently based on their value during each iteration.
Practice Quiz
1. Which of the following best describes the purpose of an 'iteration' control structure in an algorithm?
- To choose between two or more different paths of execution based on a condition.
- To repeat a block of code multiple times until a certain condition is no longer met.
- To declare and initialize variables at the beginning of a program.
- To define a function or subroutine that can be called multiple times.
2. An algorithm needs to determine if a student's score is high enough to pass an exam (e.g., score $\ge$ 60). Which control structure would be most appropriate for this decision?
- A
forloop - A
whileloop - An
if-elsestatement - A
do-whileloop
3. Consider the following pseudocode:
total = 0
FOR i FROM 1 TO 5
total = total + i
ENDFOR
What type of control structure is demonstrated here?
- Selection
- Recursion
- Iteration
- Parallel processing
4. What is a key characteristic that distinguishes a for loop from a while loop in many programming contexts?
- A
forloop always executes its body at least once. - A
whileloop is primarily used for definite iteration (known number of repetitions). - A
forloop is generally used when the number of iterations is known or easily determinable beforehand. - A
whileloop can only handle boolean conditions, whereas aforloop can handle numeric conditions.
5. An algorithm requires checking a user's age and providing different messages for 'child' (age $\lt$ 13), 'teenager' (age $\ge$ 13 AND age $\lt$ 20), and 'adult' (age $\ge$ 20). Which combination of control structures would best handle this?
- Nested
forloops - A single
whileloop with multiple conditions - An
if-else if-elsechain or aswitchstatement - A
do-whileloop with a counter
6. In a conditional statement like if (condition) { statementA; } else { statementB; }, what type of expression typically replaces condition?
- An arithmetic expression that evaluates to a number.
- A string literal or a character variable.
- A boolean expression that evaluates to true or false.
- A declaration of a new variable.
7. An algorithm needs to print all even numbers from 2 to 20. Which control structure combination is most suitable?
- A
whileloop to iterate from 2 to 20, with anifstatement inside to check for even numbers. - A
forloop that directly increments by 2 from 2 to 20. - A series of 10 separate
ifstatements, one for each even number. - A
do-whileloop that continues as long as the number is less than 2, with anelse ifstatement.
Click to see Answers
1. B (Iteration is about repetition.)
2. C (An if-else statement is perfect for making a pass/fail decision based on a score.)
3. C (The FOR...ENDFOR structure indicates a definite loop, which is a form of iteration.)
4. C (for loops are typically used when you know how many times you need to repeat, while while loops are for indefinite repetition based on a condition.)
5. C (An if-else if-else chain or a switch statement is designed to handle multiple distinct conditions and their corresponding actions.)
6. C (Conditional statements rely on boolean expressions to determine which path to take.)
7. B (A for loop with an increment step of 2 is the most direct and efficient way to achieve this. Option A would also work but is less direct; C is inefficient; D is incorrect in its logic.)
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀