Use logic statements to define parts of an algorithm solution

Resources | Subject Notes | Computer Science

Cambridge A-Level Computer Science 9618 - 9.2 Algorithms - Logic Statements

Cambridge A-Level Computer Science 9618

9.2 Algorithms - Using Logic Statements

This section focuses on how to effectively use logic statements (conditional statements and loops) to define different parts of an algorithm solution. Understanding and applying these statements is crucial for creating algorithms that can handle various scenarios and achieve the desired outcome.

1. Conditional Statements (if, else if, else)

Conditional statements allow an algorithm to make decisions based on whether a certain condition is true or false. This is fundamental for creating algorithms that are not linear and can adapt to different inputs.

The basic structure of an `if` statement is:

if (condition) {
    // Code to execute if the condition is true
}

You can also use `else if` and `else` to handle multiple conditions:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition1 is false and condition2 is true
} else {
    // Code to execute if none of the above conditions are true
}

Example: An algorithm to determine if a number is positive, negative, or zero.

Suggested diagram: A flowchart illustrating the if/else if/else structure for checking the sign of a number.
Step Description Condition
1 Get the number from the user. N/A
2 Check if the number is greater than 0. $number > 0$
3 If the condition in step 2 is true, output "Positive". N/A
4 Else if the number is less than 0. $number < 0$
5 If the condition in step 4 is true, output "Negative". N/A
6 Else (if the number is equal to 0). $number == 0$
7 Output "Zero". N/A

2. Loops (for, while)

Loops allow an algorithm to repeat a block of code multiple times. This is essential for processing lists of data or performing repetitive tasks.

2.1 For Loops

A `for` loop is used when the number of iterations is known in advance. The structure is:

for (initialization; condition; increment) {
    // Code to be repeated
}

Example: An algorithm to print numbers from 1 to 10.

Suggested diagram: A flowchart illustrating the for loop structure for printing numbers.
Step Description Condition
1 Set a counter variable to 1. $counter = 1$
2 Check if the counter is less than or equal to 10. $counter <= 10$
3 If the condition in step 2 is true, output the value of the counter. N/A
4 Increment the counter by 1. $counter = counter + 1$
5 Go back to step 2. N/A

2.2 While Loops

A `while` loop is used when the number of iterations is not known in advance and depends on a condition. The structure is:

while (condition) {
    // Code to be repeated
}

Example: An algorithm to repeatedly ask the user for input until they enter 'quit'.

Suggested diagram: A flowchart illustrating the while loop structure for repeated input.
Step Description Condition
1 Set a flag variable to false. $flag = false$
2 Check if the flag is false. $flag == false$
3 If the condition in step 2 is true, prompt the user for input. N/A
4 Check if the user input is 'quit'. $user_input == 'quit'$
5 If the condition in step 4 is true, set the flag to true and exit the loop. $flag = true$
6 If the condition in step 4 is false, go back to step 3. N/A

3. Combining Logic Statements

Algorithms often require combining conditional statements and loops to create more complex logic. For example, you might use a `while` loop to process a list of items, with an `if` statement inside the loop to perform different actions based on the item's properties.

Understanding how to structure algorithms using these fundamental logic statements is a key skill in computer science.