Vous êtes sur la page 1sur 20

Computer and Information Technology for (HKCEE) Module A2

5.1Condition
5.2 Selection
5.3 Iteration
Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.1 Condition
Condition
a logical expression (e.g. x>0) which evaluates
to either true or false
Input Num

False True
Condition

Process 1 Process 2

© Longman Hong Kong Education Page2


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.1 Condition
Logical operators / Boolean operators
=,>,<,>=,<=,<>,NOT
AND operator
true only if both operands are true
OR operator
true if at least one of the operands are true

© Longman Hong Kong Education Page3


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.1 Condition

(Note: Assume x = 3)
© Longman Hong Kong Education Page4
Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.1 Condition

© Longman Hong Kong Education Page5


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.1 Condition
Negation of equality
NOT(p=q)is equivalent to p<>q
Negation of comparison operators

© Longman Hong Kong Education Page6


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.2 Selection
IF-THEN-ELSE statement
IF ( condition ) THEN
statement(s)
ELSE
statement(s)
IF-THEN statement
IF ( condition ) THEN
statement(s)

© Longman Hong Kong Education Page7


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.2 Selection
Nested control structure
consist of one control statement embedded within
another control statement

© Longman Hong Kong Education Page8


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.2 Selection
Nested IF statement
IF ( condition ) THEN
IF ( condition ) THEN
IF ( condition ) THEN
statement(s)
ELSE
statement(s)
ELSE
statement(s)
ELSE
statement(s)
© Longman Hong Kong Education Page9
Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.2 Selection
CASE statement
a composite statement used to make a decision
between many alternatives
CASE ( expression ) OF
1: statement(s)
2: statement(s)
3: statement(s)
ELSE
statement(s)

© Longman Hong Kong Education Page10


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
Loop
a control structure that causes a set of
statements to be repeated
The while loop
WHILE (condition) DO
BEGIN
statement(s)
END
where condition is for testing if the loop should continue

© Longman Hong Kong Education Page11


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
WHILE-DO loop performs
initialise a value
test the loop condition
increment/decrement the value
Increment
i := i +1;
increment = 1

© Longman Hong Kong Education Page12


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
Counter-controlled loop
a loop that executes a specified number of times
Incrementation
Count := 1; {Initialisation}
WHILE Count <= 10 DO {Test}
BEGIN
(statements) {Repeated
actions}
Count := Count + 1 {Incrementation}
END

© Longman Hong Kong Education Page13


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
Counter-controlled loop
Decrementation
Count := 10; {Initialisation}
WHILE Count > 0 DO {Test}
BEGIN
(statements) {Repeated
actions}
Count := Count - 1 {Decrementation}
END

© Longman Hong Kong Education Page14


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
The FOR-TO-DO statement
control variable is incremented by 1 for each loop
FOR <variable identifier>:= <initial value> TO
<finial value> DO
statement(s)
The FOR-DOWNTO-DO statement
control variable is decremented by 1 for each loop
FOR <variable identifier>:= <initial value> DOWNTO
<finial value> DO
statement(s)

© Longman Hong Kong Education Page15


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
Nested loops
BEGIN
FOR <variable identifier>:= <initial value> TO

<finial value> DO
BEGIN
FOR <variable identifier>:= <initial value>

TO <finial value> DO
statement(s)
END
END
© Longman Hong Kong Education Page16
Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
WHILE-DO loop
test a condition at the beginning of the loop
pretest loop
WHILE (entry condition) DO
statement(s)

© Longman Hong Kong Education Page17


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.3 Iteration
WHILE-DO loop

True
condition loop body

False

© Longman Hong Kong Education Page18


Computer and Information
Technology for (HKCEE)
Module A2: Part B

5.2 Iteration
REPEAT-UNTIL loop
execute at least once
posttest loop
REPEAT
loop body
statement(s)
UNTIL(conditions) False
condition
True

© Longman Hong Kong Education Page19


Computer and Information Technology for (HKCEE) Module A2

END

Vous aimerez peut-être aussi