SchoolFreeware’s Free Pascal Program Tutorials
Tutorial 5 If statements. These are the relational operators that if statements use.
The syntax for the s statement is as follows:
We have if followed by the condition then begin
We’ll have the computer do something and then we’ll end the if statement. If the condition is not met then this code will not be run. If statements may also contain an else if conditions. There may be
multiple else if conditions or none at all within the statement. If statements may contain an else condition.
The code used within this tutorial can be downloaded at SchoolFreeware.com
I have made a new project program. I’ve already added the code to make the computer stop. I’ll start off with a simple if statement.
if 5 is greater than 2
Then I’ll begin
writeln to the screen telling the user the add 5 is greater
The computer already puts in an end with the semicolon there for me. I’m going to go ahead and run.
The computer responds correctly that 5 is greater than 2
If I were to try the if statement 2 is greater than 5 then I’ll begin
Telling the user that 2 is greater
Go run and
I don’t see 2 is greater than 5
because this condition here would never be met. The messages within Lazarus are telling me that
This code is unreachable because of the mathematical
situation within the condition. Next I’ll have the user enter in two integers.
We’ll have the computer check to see which integer is greater than the other. I’m going to go up here to
the area underneath uses. I’m going to put in var. I’m going to need two integers
I’ll have Num1 as an integer and
Num2 will also be an integer.
I’m going to write line a few spaces to the screen
I’ll write prompting the user to enter first integer
The input into Num1 then I’m going to write to the screen prompting the user to enter
readln the user’s input into num2
I’ll then write line the space
to the screen and start my if statement. If
Num1 is greater than
Then I’m going to begin and writeln to the screen
integer is greater
Now the computer put an end semicolon here
I have to remove the semicolon because I don’t want the if statement to end right here.
I just want this section of the code to end in that the whole if statement
I’m going to remove that semicolon and then put the else if condition
Num2 is greater than Num1
Then we’ll begin and writeln to the screen
Integer is greater
there is a possibility that both numbers are exactly the same.
I’ll have to remove the semicolon here, and I’ll put in an else. I’ll begin
writeln to the screen turn the user that the integers
are the same
The computer ends with an end
semicolon. That’s what we need here. We need to remember to use the semicolon at the very end of the if statement. This statement
actually ends right here, so I’ll go ahead and run
We’ll test this out
Enter first integer for the second one will be
5. The second integer is greater. I will go test another possibility
5 and 4. The first one is greater
It works. We’ll try for the third possibility the integers are the same. Five and five
The integers are the same