control structures c2 c3
TRANSCRIPT
Pascal Programming Language
Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department
• How was last class? :D
• Happy with the course ?
• Any Q ?
• Conditional Sentences
• If / else Sentence
• Nested if Sentence
• Combination Tools
• Loop :D
• 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) thenbegin
…end ;
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln ( ‘ x is bigger than 10 ’ ) ;writeln ( ‘ x is too big ’ ) ;
end;Readln ;
End.
• 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) thenbegin
…end
elsebegin
…end;
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln ( ‘ x is bigger than 10 ’ ) ;end
elsebegin
writeln (‘ x is smaller than 10 or equal to 10’);end
Readln ;
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln ( ‘ x is bigger than 10 ’ ) ;end
elsebegin
writeln (‘ x is smaller than 10 or equal to 10’);end
Readln ;
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln ( ‘ x is bigger than 10 ’ ) ;end
elsebegin
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
• 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) thenbegin
…if (condition2) then
begin…
end…
end
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);if (x > 10) thenbegin
writeln (‘ x is bigger than 10’);
if (x > 20) then
writeln (‘x is bigger than 20’);
end;
End.
• 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) thenbegin
….end
2. OR Operation: The block will not be implemented unless one of the two (or more) conditions are valid.
General Form:
3. Not Operation: The block will be implemented if the reverse condition is valid.
General Form:
if (condition1) or (condition2) thenbegin
….end
if ( not condition1) thenbegin
….end
1. We can ignore the (begin-end) instructions if the block has just one instruction.
2. We can’t write a (;) before (else).
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);
if (x > 10) and (x < 20) then
writeln (‘x is between 10 and 20’);
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);
if (x = 10) or (x = 20) thenbegin
writeln (‘x is equal to 10 or is equal to 20’);
end;
End.
Program Lesson2_Program1 (input,output);
Var
x : integer;Begin
read (x);
if ( not ( x > 10 ) ) thenbegin
writeln (‘x is smaller than 10 or equal to 10’);
end;
End.
Loop is used to repeat the execution of a set of instructions for at least one time.
Repeat
..(code)
..(code)
..(code)
Until conditional statement;
General Form:
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.
The for loop is a sort of
repeat-until loop.
But repeats a set of instructionsfor a number of times.
For {variable} := {original value} to / downto {final value} do
{code…..}
{code…..}
General Form:
Program Lesson2_Program1 (input,output);
Var
Counter : Integer; Begin
For Counter := 1 to 7 do {it's easy and fast!}writeln ('for loop test :D ‘, Counter);
Readln;
End.
Program Lesson2_Program1 (input,output);
Var
Counter : Integer; Begin
For Counter := 7 downto 1 do {it's easy and fast!}writeln ('for loop test :D ‘, Counter);
Readln;
End.
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
Program Lesson2_Program1 (input,output);
Begin
For i:=1 to 1 do
Writeln(‘I’m Learning Pascal :D');
End.
I’m Learning Pascal :D
For i:= 1 to 's' do
For i:= 10 to 3 do
For i:= 5 downto 10 do
Program Lesson2_Program1 (input,output);
Begin
For i:=1 to 10 doWriteln('wajdy');Writeln('essam');
End.
U tell me what is the Output ;)
The while loop 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.
While <condition is true> do
instruction 1;
instruction 2;
instruction 3;
End;
General Form:
Program Lesson2_Program1 (input,output);
Var
Ch : Char;Begin
Writeln ( ‘ Press ''q'' to exit... ‘ ) ;Readln (Ch);While ( Ch <> ‘ q ‘ ) doBegin
Writeln ( ‘ I told you press ''q'' to exit!! ‘ ) ;Read(Ch);
End;End.
اذا كان زوجي ويطبع yesاكتب برنامج ندخل عدد فيقوم بطباعة رسالة
Noاذا كان فردي
وعالماتهم وحساب المتوسط لعالماتهم nالطالب قراءة عدد