Upload
melina-payne
View
224
Download
0
Embed Size (px)
Citation preview
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
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.
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.
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.
Pascal Programming Today Chapter 4
5
Flowchart of an if-then-else statement
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.
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.
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
Pascal Programming Today Chapter 4
9
Flowchart of an if-then-else statement with compound statements
Pascal Programming Today Chapter 4
10
If-then Statements
Syntax of if-then statements
if <Condition>
then <Statement>
Pascal Programming Today Chapter 4
11
Flowchart of an if-then statement
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
Pascal Programming Today Chapter 4
13
Flowchart of an if-then statement with compound statement
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.
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>
Pascal Programming Today Chapter 4
16
Flowchart of a nested if statement
Pascal Programming Today Chapter 4
17
» Reserved word else should be matched to the nearest preceding if in nested if statements.
Pascal Programming Today Chapter 4
18
Case Statements
» A case statement is preferred for selection involving more than three alternatives.
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).
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>.
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.
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.