0% found this document useful (0 votes)
1K views

Flowchart If - Then - Else: Anan Phonphoem Anan@cpe - Ku.ac - TH

The document discusses flowcharts and conditional logic structures like IF-THEN-ELSE statements. It provides examples of using IF-THEN-ELSE to write Pascal programs that check conditions and execute different code blocks based on whether expressions evaluate to true or false. Nested IF statements and the CASE statement are also covered with examples showing how to write conditional logic with multiple branching points.

Uploaded by

Adi Chandranata
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Flowchart If - Then - Else: Anan Phonphoem Anan@cpe - Ku.ac - TH

The document discusses flowcharts and conditional logic structures like IF-THEN-ELSE statements. It provides examples of using IF-THEN-ELSE to write Pascal programs that check conditions and execute different code blocks based on whether expressions evaluate to true or false. Nested IF statements and the CASE statement are also covered with examples showing how to write conditional logic with multiple branching points.

Uploaded by

Adi Chandranata
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Flowchart

If – Then – Else

อนันต์ ผลเพิม่
Anan Phonphoem
[email protected]

1
Outline
 Flowcharts
 IF – THEN – ELSE

2
Boolean Expression
 Two possible values: True, False
1. Relation Operator
 = , < , > , <> , <= , >=
2. Boolean Operator
 AND , OR , NOT

15 = 34 False
15.05 < 17 True
34 < > 34.00002 True

3
Precedence rules for
arithmetic operators
1. ( ) parentheses
2. Unary + and –
3. *, / , DIV , MOD
4. + –
5. If equal precedence, left to right
Examples
-a+j/-w = (-a) + (j / (-w))
C*23/6+23mod2 = ((C*23)/6) + (23 mod 2)

4
Precedence rules for
Boolean Operators
1. ( ) parentheses
2. NOT
3. and
4. Or
5. < , <= , > , <= , = , <>
6. If equal precedence, left to right
Examples
(count <= 10) AND (Sum <= Limit) OR NOT Stop

5
Flowcharts
 Graphical representation of algorithm
Terminator

Process

Input/output

Decision

Connector
Flow line
6
Flowchart example
Start

Read width
Read length

Total := width + length

If total < > 0 No


Yes
Write total

End
7
IF – THEN
IF condition THEN
condition
statement False
True

Statement

8
IF – Then (sum1.pas)
Program summation1;
Var
Sum, number : integer;
Begin
Sum := 10;
Write (‘Please input a number’);
Readln(number);
if number < 0 then
number := 0;
Sum := Sum + number;
writeln (‘Sum =‘, Sum)
End.

9
IF – THEN – ELSE
IF condition THEN
statement 1
ELSE condition
True False
statement 2

Statement1 Statement2

10
IF – Then – Else (sum2.pas)
Program summation2;
Var
Sum, number : integer;
Begin
Sum := 10;
Write (‘Please input a number’);
Readln(number);
if number < 0 then
number := 0;
else
number := 20;
Sum := Sum + number;
writeln (‘Sum =‘, Sum)
End.

11
Nested Logic
IF cond1 THEN condition1 False
True
statement1
ELSE condition2
IF cond2 THEN Statement1
True False
Statement2
ELSE
statement3 Statement2 Statement3

12
Nested Logic (Sum3.pas)
Begin
Sum := 10;
Write (‘Please input a number’);
Readln(number);
if number > 0 then
number := number +1;
if number > 5 then
number := 5;
else
number := 20;
Sum := Sum + number;
writeln (‘Sum =‘, Sum)
End.

13
Nested Logic (sum4.pas)
Begin
Sum := 10;
Write (‘Please input a number’);
Readln(number);
if number > 0 then
begin
number := number +1;
if number > 5 then
number := 5;
End
else
number := 20;
Sum := Sum + number;
writeln (‘Sum =‘, Sum)
End.

14
Case
Case expression of
const_value1 : statement1;
const_value2 : statement2;
const_value3 : statement3;
Else
statement4;
End;
15
Case Example
Readln (number);
Case number of
1,2,3 : writeln(‘small’);
4,5,6 : writeln(‘medium’);
7,8 : writeln (‘large’);
End;

16

You might also like