1 flowchart if – then – else อนันต์ ผลเพิ่ม anan phonphoem...

16
1 Flowchart If – Then – Else ออออออ อออออออ Anan Phonphoem [email protected]

Upload: ami-gibson

Post on 16-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

1

FlowchartIf – Then – Else

อนั�นัต์� ผลเพิ่��มAnan Phonphoem

[email protected]

Page 2: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

2

Outline Flowcharts IF – THEN – ELSE

Page 3: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

3

Boolean Expression Two possible values: True, False1. Relation Operator

= , < , > , <> , <= , >=2. Boolean Operator

AND , OR , NOT

15 = 34 False

15.05 < 17 True

34 < > 34.00002 True

Page 4: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

4

Precedence rules for arithmetic operators

1. ( ) parentheses2. Unary + and –3. *, / , DIV , MOD4. + –5. If equal precedence, left to rightExamples-a+j/-w = (-a) + (j / (-w))

C*23/6+23mod2 = ((C*23)/6) + (23 mod 2)

Page 5: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

5

Precedence rules for Boolean Operators

1. ( ) parentheses2. NOT3. and4. Or5. < , <= , > , <= , = , <>6. If equal precedence, left to rightExamples(count <= 10) AND (Sum <= Limit) OR NOT

Stop

Page 6: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

6

Flowcharts Graphical representation of algorithm

Terminator

Process

Input/output

Decision

Connector

Flow line

Page 7: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

7

Flowchart exampleStart

Read widthRead length

Total := width + length

If total < > 0

Write total

End

No

Yes

Page 8: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

8

IF – THEN

IF condition THENstatement condition

False

True

Statement

Page 9: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

9

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.

Page 10: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

10

IF – THEN – ELSE IF condition THEN

statement 1ELSE

statement 2condition

FalseTrue

Statement2Statement1

Page 11: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

11

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.

Page 12: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

12

Nested Logic

condition1 FalseTrue

Statement1condition2

FalseTrue

Statement3Statement2

IF cond1 THEN statement1ELSE IF cond2 THEN Statement2 ELSE statement3

Page 13: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

13

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.

Page 14: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

14

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;

Endelse

number := 20;Sum := Sum + number;writeln (‘Sum =‘, Sum)

End.

Page 15: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

15

Case

Case expression of const_value1 : statement1;

const_value2 : statement2;const_value3 : statement3;

Elsestatement4;

End;

Page 16: 1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th

16

Case Example

Readln (number);Case number of

1,2,3 : writeln(‘small’);4,5,6 : writeln(‘medium’);7,8 : writeln (‘large’);

End;