ICSCT -- Writing Algorithms Lesson
Writing Algorithms Lesson
When you design algorithms you use a method called Top Down Design. In this method, you write down the steps in the correct order to solve the problem starting from the top down. It is like completing instructions in a numbered list going from 1 to 10. You perform step 1, then step 2 until you get to step 10. Sometimes the solution to a problem is more complex and it requires that a condition be checked or certain instructions be repeated. In programming, control flow refers to the order in which individual statements are executed.
There are three basic control structures used in programming.
Sequential, Conditional, and Iteration
Sequential Structure: If you have a very simple problem you may only need a sequential control structure. This type of structure does not need to make any decisions or repeat any lines of code to solve the problem.
Examples of when to use sequential control flow:
- Find the product of two numbers
- Change the time in seconds to minutes
- Find the difference between two numbers
- Change the temperature in Fahrenheit to Centigrade
- Print a name or a message
- Move forward one space
Conditional Statements: Often the solution to a problem involves making a decision. This type of control flow is called a decision statement, conditional statement, or if statement. A question will be asked and the answer will be either true or false.
- Is the ball touching another object?
- Is the ball touching the edge?
- Is score > 20?
Based on the answer, the control flow of the program will branch and execute a set of instructions. If the answer to the question is true, a certain set of statements will be executed. If the answer to the question is false, either nothing will happen or a different set of instructions will be executed.
The answer to the condition will always result in a true or false answer.
if(it is raining) if(the ball goes out of bounds) if( number1 > number 2)
Examples of when to use conditional control flow:
- To print the largest of two numbers
- To decide if a number is positive or negative
- To decide if a number is even or odd
- To decide if the temperature is too cold to play tennis
- To change the score if the ball hits another player
Iteration: Some problems require that sections of code be repeated. This type of structure is more efficient than sequential because it eliminates unnecessary lines of code. This control structure is called a repeat, loop, or iteration. You use the words repeat until, while and for when writing your algorithms. This type of structure also requires that a condition be true in order for the instructions to be repeated. As soon as the condition is false, the iteration stops.
Examples of when to use iteration control flow:
- While number is less than 10 print the number
- While the car is still not clean wash car
- While password is incorrect ask for password
- Print the number 10 to 20
- While you are still hungry eat
- Draw a line 4 times
In programming, you will find that very few problems use just sequential, conditional, or iteration alone. Most problems will use a combination of the three structures.
- Use the correct order: For your instructions and the correct control structure needed to solve the problem.
- Use clear and unambiguous instructions: All the steps in the algorithm should be clear and unambiguous.
- The algorithm must produce a correct solution: It must be able to solve correctly for all possible outcomes.
- The algorithm can be reused: An algorithm should be written so that it can be used in different programing languages with the same results.
- The algorithm must end or terminate the program: It must be finite and have an end.
Binary Activity
Complete the Binary learning object below by clicking the arrows. [CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION