Free Pascal Program Tutorial 5 – If Statements – Lazarus


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
than
2
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
writeln
Telling the user that 2 is greater
than 5.
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
then readln
The input into Num1 then I’m going to write to the screen prompting the user to enter
second integer
then
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
Num2
Then I’m going to begin and writeln to the screen
the first
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
else if
Num2 is greater than Num1
Then we’ll begin and writeln to the screen
the second
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
Yes
It works. We’ll try for the third possibility the integers are the same. Five and five
The integers are the same

13 Replies to “Free Pascal Program Tutorial 5 – If Statements – Lazarus”

  1. it's easier doing this:

    NUM1,NUM2:integer;

    instead of this:

    Num1:integer;
    Num2:integer;

    Anyway this was helpful, you are way better than my teacher at teaching.
    thanks a lot! 😀

  2. i am trying to right a bit of code that pretty much says do you understand (yes/no)?: 
    but i do not know how to right an if statement for whether the person writes yes or no. can you please help me out?

  3. I'm a Pascal beginner and I already know formats that this channel demonstrated wrongly, nay, have alternatives.
    For instance,
    Writing – Num1 : integer;
    Num2 : integer – is pointless. Just write
    Num1, Num2 : integer;
    If you're only doing something simple like writing a line after an If statement, you don't necessarily have to use begin and end;, unless you're actually going to type a whole chunk of program inside the If statement

  4. How can i use this for multiple conditions?
    Like when i want to check if the number is even/odd AND greater than 8 (just writing and wont do the job for me ):

  5. when you only want on statement when the condition holds you an just write
    if <condition> then <statement>
    rather than
    id <condition> then begin <statement> end

Leave a Reply

Your email address will not be published. Required fields are marked *