Making Choices

There are many occasions where a program is required to take alternative actions. For example, there are occasions where we need to take action according to the user choice.

All computer languages provide a means of selection. Usually it is in the form of If statement and our pseudo-code is no exception to this.

We will use the if statement together with logical operators to test for true or false as shown below.

If a = b
print “a = b”

The action is only taken when the test is true.
The logical operators used in our pseudo-code are.

= is equal to
> is greater than
< is less than
>= is greater than or equal
<= is less than or equal
<> is not equal to

Example 1: The following shows how the selection control structure is used in a program where a user chooses the options for multiplying the numbers or adding them or subtracting.

Use variables: choice, of the type character
ans, number1, number2, of type integer
display “choose one of the following”
display “m for multiply”
display “a for add”
display “s for subtract”
accept choice
display “input two numbers you want to use”
accept number1, number2
if choice = m then ans = number1 * number2
if choice = a then ans = number1 + number2
if choice = s then ans = number1 - number2
display ans


Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

buttons=(Accept !) days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !