22
Pascal Programming Today Chapte r 4 1 » Conditional statements allow the execution of one of a number of possible operations. » Conditional statements include: if statements case statements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Embed Size (px)

Citation preview

Page 1: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

1

» Conditional statements allow the execution of one of a number of possible operations.

» Conditional statements include:–if statements

–case statements

Page 2: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

2

If-then-else Statements

Syntax of if-then-else statements

if <Condition>

then <Statement A>

else <Statement B>

May be a Boolean constant, Boolean variable or Boolean expression.

Page 3: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

3

If-then-else Statements

Syntax of if-then-else statements

if <Condition>

then <Statement A>

else <Statement B>

Will be executed if <Condition> is TRUE.

Page 4: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

4

If-then-else Statements

Syntax of if-then-else statements

if <Condition>

then <Statement A>

else <Statement B>

Will be executed if <Condition> is FALSE.

Page 5: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

5

Flowchart of an if-then-else statement

Page 6: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

6

» Sometimes, more than one statement is required to be executed under the condition.

» In such case, a compound statement should be used.

Page 7: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

7

» A compound statement consists of two or more simple statements enclosed by a pair of reserved words begin and end.

Page 8: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

8

Syntax of if-then-else statements with compound statements

if <Condition>

then begin

<Statement A1>;

<Statement AN>

end

else begin

<Statement B1>;

<Statement BN>

end

Compound statement

Compound statement

Page 9: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

9

Flowchart of an if-then-else statement with compound statements

Page 10: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

10

If-then Statements

Syntax of if-then statements

if <Condition>

then <Statement>

Page 11: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

11

Flowchart of an if-then statement

Page 12: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

12

Syntax of if-then statements with compound statements

if <Condition>

then begin

<Statement 1>;

<Statement 2>;

<Statement N>

end

Page 13: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

13

Flowchart of an if-then statement with compound statement

Page 14: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

14

Nested If Statements

» An if statement may be nested by another if statement to form a nested if statement.

Page 15: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

15

Syntax of nested if statements

if <Condition C1>

then if <Condition C2>

then <Statement A1>

else <Statement A2>

else if <Condition C3>

then <Statement B1>

else <Statement B2>

Page 16: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

16

Flowchart of a nested if statement

Page 17: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

17

» Reserved word else should be matched to the nearest preceding if in nested if statements.

Page 18: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

18

Case Statements

» A case statement is preferred for selection involving more than three alternatives.

Page 19: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

19

Syntax of case statements

case <Selector> of

<Case label 1> : <Statement 1>;

<Case label 2> : <Statement 2>;

<Case label N> : <Statement N>

end

A variable or an expression of ordinal data types (integer, char or Boolean).

Page 20: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

20

Syntax of case statements

case <Selector> of

<Case label 1> : <Statement 1>;

<Case label 2> : <Statement 2>;

<Case label N> : <Statement N>

end

Lists of possible values of <Selector>.

Page 21: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

21

Syntax of case statements

case <Selector> of

<Case label 1> : <Statement 1>;

<Case label 2> : <Statement 2>;

<Case label N> : <Statement N>

end

Simple or compound statements.

Page 22: Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:

Pascal Programming Today Chapter 4

22

» Only one selection on the list of statements will be executed.

» No statements will be executed if no case label match the selector.