37
Pascal Programming Language Omar ElSabek & Fayez G hazzawi IT Engineering 3 th year UNKNOWN Department

Control structures c2 c3

Embed Size (px)

Citation preview

Page 1: Control structures c2 c3

Pascal Programming Language

Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department

Page 2: Control structures c2 c3

• How was last class? :D

• Happy with the course ?

• Any Q ?

Page 3: Control structures c2 c3

• Conditional Sentences

• If / else Sentence

• Nested if Sentence

• Combination Tools

• Loop :D

Page 4: Control structures c2 c3

• 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 ;

Page 5: Control structures c2 c3

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.

Page 6: Control structures c2 c3

• 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;

Page 7: Control structures c2 c3

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.

Page 8: Control structures c2 c3

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.

Page 9: Control structures c2 c3

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. .

Page 10: Control structures c2 c3

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

Page 11: Control structures c2 c3

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.

Page 12: Control structures c2 c3

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.

Page 13: Control structures c2 c3

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.

Page 14: Control structures c2 c3

• 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

Page 15: Control structures c2 c3

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

Page 16: Control structures c2 c3

1. We can ignore the (begin-end) instructions if the block has just one instruction.

2. We can’t write a (;) before (else).

Page 17: Control structures c2 c3

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.

Page 18: Control structures c2 c3

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.

Page 19: Control structures c2 c3

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.

Page 20: Control structures c2 c3
Page 21: Control structures c2 c3
Page 22: Control structures c2 c3

Loop is used to repeat the execution of a set of instructions for at least one time.

Page 23: Control structures c2 c3

Repeat

..(code)

..(code)

..(code)

Until conditional statement;

General Form:

Page 24: Control structures c2 c3

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.

Page 25: Control structures c2 c3

The for loop is a sort of

repeat-until loop.

But repeats a set of instructionsfor a number of times.

Page 26: Control structures c2 c3

For {variable} := {original value} to / downto {final value} do

{code…..}

{code…..}

General Form:

Page 27: Control structures c2 c3

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.

Page 28: Control structures c2 c3

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.

Page 29: Control structures c2 c3

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

Page 30: Control structures c2 c3

Program Lesson2_Program1 (input,output);

Begin

For i:=1 to 1 do

Writeln(‘I’m Learning Pascal :D');

End.

I’m Learning Pascal :D

Page 31: Control structures c2 c3

For i:= 1 to 's' do

For i:= 10 to 3 do

For i:= 5 downto 10 do

Page 32: Control structures c2 c3

Program Lesson2_Program1 (input,output);

Begin

For i:=1 to 10 doWriteln('wajdy');Writeln('essam');

End.

U tell me what is the Output ;)

Page 33: Control structures c2 c3

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.

Page 34: Control structures c2 c3

While <condition is true> do

instruction 1;

instruction 2;

instruction 3;

End;

General Form:

Page 35: Control structures c2 c3

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.

Page 36: Control structures c2 c3

اذا كان زوجي ويطبع yesاكتب برنامج ندخل عدد فيقوم بطباعة رسالة

Noاذا كان فردي

وعالماتهم وحساب المتوسط لعالماتهم nالطالب قراءة عدد

Page 37: Control structures c2 c3