BSS Main | Previous Lesson | Next Lesson
This lecture focuses on:
Our first algorithm problem was solved using the sequential approach. That is the steps were written in a logical sequence and anyone following the given steps in the correct sequence would be able to solve the problem.
Our next problem requires not only the sequential approach but a selection method also. Whenever we have to make a decision the selection approach must be used.
We must use the IF statement to solve the next problem.
Problem Three
Write the algorithm for a program that will accept two numbers as input and print out the larger of the two numbers.
Solution to Problem Three
Begin
Input x, y
If x > y
Print x is greater
else
Print y is greater
End If
End
The IF else statement works like this : There must be a condition to be tested. Is the value X greater than the value Y? If that is true then Print X is greater, else ( the else tells us what should be done if the original IF condition evaluates to false ) Print Y is greater.
On the surface you may think this was a simple problem but this algorithm is missing something. Programmers will say that there is a logic error here somewhere. Can you find it?
To find the bug you must test your program with some test data and try to determine what the output would be. This is called giving your program a dry run because you are testing your program on paper before you write source code in a programming language.
Here is a HINT
What will the output be if I enter the same number twice (for example 25 ) as values for both X and Y?