Sherman and Adams or
Sherman and Calhoun
Postcondition: Calhoun and Riley
if we are at sherman and adams then begin
move one block south;
move one block east;
end
else begin
move one block east
end;
OR
if we are at sherman and adams then begin
move one block south;
move one block east;
end
else
if we are at sherman and calhoun then begin
move one block east
end;
Precondition: somewhere on Sherman Avenue
Postcondition: Clay and Carroll
if we are at Sherman and Adams then begin
move one block south;
move one block south;
end
else
if we are at sherman and calhoun then begin
move one block south;
end
else
if we are at sherman and carroll then begin
end
else
if we are at jackson and sherman then begin
move one block north;
end
else
if we are at washington and sherman then begin
move one block north;
move one block north;
end;
move one block east;
move one block east;
SF
if condition1 then begin
statement or statements to be executed when condition1 is true
end
else
if condition2 then begin
statement or statements to be executed when condition 2 is true
end
.
.
.
else
if conditionn then begin
statement or statements to be executed when condition n is true
end
else
statements to be executed when none of the conditons 1 through n are true
In this unit, we are introduced to the second way of organizing statements in PASCAL and other higher level languages. This is the alternative. As before, we will illustrate this organization using the Street map example that we have before.
A simple form of the if statement is of the following form:
if condition then begin
one or more statements
end;
some other statements
If the condition is true thenWe will discuss the exact syntax of condition in PASCAL, for now consider it as an expression which can be evaluated as "true" or "false" In our examples of streets, they will be of the form, we are at a given street-corner intersection or not.
Sometimes, it is desired to consider several different alternatives and do a different thing if each alternative is true.
If we have two alternatives, we can write
if condition1 then begin
statement-set 1
end
else
if condition2 then begin
statement-set 2
end;
If condition1 is true then statement-set 1 will be executed.
If condition1 is not true, then it will go down and check condition2. If condition2 is true, then statement-set 2 will be executed.
If neither condition1 nor condition2 are true, then neither statement-set 1 nor statement-set2 will be executed.
Note that there is no semicolon after the end or the else between the two alternatives.
If there are more than one statement to be executed for
Sometimes, it is desired to do something if condition1 is true, otherwise do something
else. In that case, we can have the simplified form--
if condition1 then begin
statement-set 1
end
else begin
statement-set 2
end;
Note that if condition1 is true, then statement-set 1 will be executed. If condition1
is not true, then statement-set 2 will be executed. Note that when one has an "else"
condition, one of the statement sets will always be executed.
Then we come to the big statement shown on the transparency above. The system will test condition 1. If condition1 is true, then statement1 will be executed and we will proceed to the end of the entire if statement. If condition 1 is not true, then the system will test condition 2. If condition2 is true, then statements-2 will be executed. Otherwise, it will continue on to test condition 3. If all the conditions up to n turn out not to be true, then it will test condition n. If condition-n is true, then the statements-n will be executed. If none of the conditions from 1 to n turn out to be true, then the last statements get executed.
When setting up an if statement, one must identify the alternatives. In the first program, the alternatives were clearly specified in the Precondition.
1. Sherman and Adams 2. Sherman and CalhounWe then identify the things we have to do from each of the alternatives. In order to go from Sherman and Adams to Calhoun and Riley, we have the two moves:
move one block south
move one block east
Note that these two statements appear under the alternative "sherman and adam"
The other option is Sherman and Calhoun, we then "move one block east." to get to Calhoun and Riley.
This appears under the "else begin" or the "Sherman and Calhoun." Since there are only two alternatives in the precondition, we can get away with using the "else" without checking specifically to see if we are at Sherman and Calhoun.
In other times, the alternatives are not clearly specified. In the second example, our precondition is stated as simply "somewhere on Sherman Avenue." We must look at our map and see that the alternatives are:
1. sherman and adams 2. sherman and calhoun 3. sherman and carroll 4. sherman and jackson 5. sherman and washingtonEach of these alternatives are in the if statement.
We divided the program into two parts:
A. move to carroll and sherman B. move from Carroll and Sherman to Carroll and Clay.Part A is done by the if. For example, if we hit the condition"we are at Sherman and Adams" and find it true, note that the two "move one block south" statements will cause the computer to get to Caroll and Sherman. Then we drop down to the "move one block east" at the end and execute two fo these, getting us to the postcondition for the entire program, Clay and Carroll.
As another example, assume we are already at Sherman and Carroll when we start the program. We hit the condition corresponding to this. We execute the statements there--none. That leaves us at Sherman and Carroll. Then we drop down to the two "move one block easts" and get us to Caroll and Clay.
Yes, Virginia, we could have eliminate that condition and the null statements for it and the program would have worked just fine.