This document provides an overview of the Pascal programming language and examples of common programming structures in Pascal such as conditional statements, loops, and functions. Key topics covered include if/else statements, nested if statements, for loops, while loops, and repeat loops. Examples are given to demonstrate how to use these structures to write Pascal programs that perform conditional logic and iterate through loops.
4. • We use if to add a Condition to the operation.
• The General Form:
• So here … the Compiler will check the validity of the condition:
1. If true : the instructions inside the block will be implemented :)
2. Else : the instructions inside the block will not be seen :(
if (condition) then
begin
…
end ;
5. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln ( ‘ x is bigger than 10 ’ ) ;
writeln ( ‘ x is too big ’ ) ;
end;
Readln ;
End.
6. • We use else after (if) condition to add another condition to the operation
if the (if) condition is not valid.
• The General Form:
• So there…The Compiler will check the validity of the condition:
1. If true : the instructions inside the (if) block will be implemented :)
2. Else : the instructions inside the (else) block will be implemented :)
if (condition) then
begin
…
end
else
begin
…
end;
7. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln ( ‘ x is bigger than 10 ’ ) ;
end
else
begin
writeln (‘ x is smaller than 10 or equal to 10’);
end
Readln ;
End.
8. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln ( ‘ x is bigger than 10 ’ ) ;
end
else
begin
writeln (‘ x is smaller than 10 or equal to 10’);
end
Readln ;
End.
9. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln ( ‘ x is bigger than 10 ’ ) ;
end
else
begin
writeln (‘ x is smaller than 10 or equal to 10’);
end
Readln ;
End.
Note that if the 'else' term is included with an if statement, then
there should be No semi-colon before the 'else' term
just as seen in the above example. .
10. 10
• Nested if is the case that there is an (if) inside another (if) ,we use this case to
check a condition after checking a previous one.
• The General Form is
• Here …The Compiler will check
the validity of condition1.
if condition1 is valid ,the compiler
will check the validity of condition2.
if (condition1) then
begin
…
if (condition2) then
begin
…
end
…
end
11. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
12. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
13. Program Lesson2_Program1 (input,output);
Var
x : integer;
Begin
read (x);
if (x > 10) then
begin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
14. • We use these tools between the conditions in the same (if) sentence
1. AND operation:The block will not be implemented unless the two (or more) conditions
are valid.
General Form:
if (condition1) and (condition2) then
begin
….
end
15. 2.OROperation:The block will not be implemented unless one of the two (or
more) conditions are valid.
General Form:
3.NotOperation:The block will be implemented if the reverse condition is
valid.
General Form:
if (condition1) or (condition2) then
begin
….
end
if ( not condition1) then
begin
….
end
16. 1. We can ignore the (begin-end) instructions if the block has just
one instruction.
2. We can’t write a (;) before (else).
24. Program Lesson2_Program1 (input,output);
Var
YN : String;
Begin
Writeln ( ‘Y (YES) or N(NO)? ‘ ) ;
Repeat {repeat the code for at least one time}
Readln(YN) ;
If YN = 'y' then Writeln(‘Welcome :D’);
If YN = 'n' then Writeln('Why not? Exiting...');
Until (YN = 'n');
End.
25. The for loop is a sort of
repeat-until loop.
But repeats a set of instructions
for a number of times.
29. 1. The for loop execute from the first value to the last value
2. In case the start and end values was same the loop execute only
ones :D
3. The start and end values must be from the same type
33. The whileloop is executed while the
condition is true.
It is different from the 'Repeat-Until' loop
since the loop might not be executed for at
least one time.
34. While <condition is true> do
instruction 1;
instruction 2;
instruction 3;
End;
General Form:
35. Program Lesson2_Program1 (input,output);
Var
Ch : Char;
Begin
Writeln ( ‘ Press ''q'' to exit... ‘ ) ;
Readln (Ch);
While ( Ch <> ‘ q ‘ ) do
Begin
Writeln ( ‘ I told you press ''q'' to exit!! ‘ ) ;
Read(Ch);
End;
End.