Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 nd Semester 2004 1 204111 Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.

Similar presentations


Presentation on theme: "2 nd Semester 2004 1 204111 Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer."— Presentation transcript:

1 2 nd Semester 2004 1 204111 Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 2 nd Semester 2004 2 Outline  How to Write Flowchart  Selection Statement IF Statement Case Statement  Selection Statement Example

3 2 nd Semester 2004 3 Do you remember what we learn last week?  Basic Input/Out/Assignment Statement  Arithmetic/Boolean Expression

4 2 nd Semester 2004 4 Outline  How to Write Flowchart  Selection Statement IF Statement Case Statement  Selection Statement Example

5 2 nd Semester 2004 5 Problem Example 1  Problem Write a program for find the area of the circle. r AREA := Pi * Radius * Radius; a = ¶r 2 CONSTANT VARIABLE VARIABLE

6 2 nd Semester 2004 6 Our View: Problem 1  Problem Checklist 1 Input 2 How to process Input (Algorithm) 3 Output Our Program 1 2 3

7 2 nd Semester 2004 7 Start Writing Program!!!  Method for start Writing Program Think in your BRAIN and WRITE program immediately First Think in Paper, WRITE/RUN/DEBUG Program After You Clear  Show your idea in the Instruction FORM  Show your idea in the Flowchart FORM

8 2 nd Semester 2004 8 Tools for Represent IDEA  Flowchart - Graphical representation of algorithm Terminator Process Input/output Decision Connector Flow line

9 2 nd Semester 2004 9 Flowchart EXAMPLE 1  Write program for finding the area of rectangle. START END ASK USER FOR WIDTH, HIGH READ WIDTH READ HIGH AREA := WIDTH * HIGH; SHOW AREA WIDTH HIGH AREA := WIDTH * HIGH;

10 2 nd Semester 2004 10 Flowchart EXAMPLE 2  Problem: FAT/SLIM???

11 2 nd Semester 2004 11 Outline  How to Write Flowchart  Selection Statement IF Statement Case Statement  Selection Statement Example

12 2 nd Semester 2004 12 IF STATEMENT  3 Types One Alternative Two Alternatives Multiple Alternatives  More than one IF STATEMENT

13 2 nd Semester 2004 13 IF STATEMENT with One Alternative IF – THEN IF condition THEN statement1; statement3; TRUE Statement3 condition False True Statement1 FALSE

14 2 nd Semester 2004 14 IF STATEMENT with One Alternative Example  I will discount you 5% if you order the books more than 1000 bath Ex. I order the books 1200 bath, I have to pay 1140 bath Program how_i_pay(input,output); Var Order_Cost :Real; Begin Write( ' How much you order:'); Readln(Order_cost); If (Order_Cost > 1000) then Order_Cost := Order_Cost * (95/100); Writeln( ' You have to pay ',Order_Cost:5:0); End.

15 2 nd Semester 2004 15 IF STATEMENT with Two Alternative IF – THEN – ELSE IF condition THEN statement1 ELSE statement2; statement3; condition FalseTrue Statement2Statement1 TRUE FALSE Statement3

16 2 nd Semester 2004 16 IF STATEMENT with Two Alternative Example  Is variable X is Odd or Even? If X is Odd  display X is Odd If X is Even  display X is Even Program x_is_odd_even(input,output); Var X:Integer; Begin Write('Please enter X:'); Readln(x); If ((X mod 2) = 0) then Writeln('X is Even') Else Writeln('X is Odd'); Write('------------'); End.

17 2 nd Semester 2004 17 IF STATEMENTS with Compound Statements thenelse  IF STATEMENT have compound statements following then or else IFTHEN IF CONDITION THENElse Compound STATEMENT BeginEnd; Statement1;Statement2;…Statementn;

18 2 nd Semester 2004 18 IF STATEMENTS with Compound Statements Example  Swap the value of X with Y if X > Y VAR X,Y,SWAP_TEMP:INTEGER; BEGIN WRITE(’Please Enter X and Y’); READLN(X, Y); {SWAP X and Y} IF (X > Y) THEN Begin End; WRITE(’X=’,X,’ Y=’,Y); END.

19 2 nd Semester 2004 19 IF STATEMENT with Multiple Alternative nestedstatement  Use nested if statement for multiple alternatives. if (X > 0) then NumPos := NumPos + 1 else if (X<0) then NumNeg := NumNeg + 1 else NumZero := NumZero +1; X > 0 FALSE TRUE FALSE X<0 TRUE 1 1 2 3 2 3

20 2 nd Semester 2004 20 IF STATEMENT with Multiple Alternative SYNTAX condition 1 If condition 1 then statement 1 condition 2 else if condition 2 then statement 2 condition 3 else if condition 3 then statement 3. condition n else if condition n then statement n else statement n+1;

21 2 nd Semester 2004 21 PROGRAM EXAMPLE Use if Statement  Display GRADE from the Subject score SUBJECTSCORE YOUR GRADE 0 – 49YOU GET F (FAIL) – See you next term 50 – 59YOU GET D 60 – 69YOU GET C 70 – 79YOU GET B 80 – 100 YOU GET A

22 2 nd Semester 2004 22 Outline  How to Write Flowchart  Selection Statement IF Statement Case Statement  Selection Statement Example

23 2 nd Semester 2004 23 CASE STATEMENT (ordinal data type)  Selector for a data type whose values may all be listed (ordinal data type) A data type having the finite set of value that can be listed in order from the first to the last …  Example Integer - VALID Integer - VALID Boolean - VALID Boolean - VALID Char – VALID Char – VALID Real – INVALID Real – INVALID

24 2 nd Semester 2004 24 CASE STATEMENT SYNTAX selector case selector of label 1 statement 1 label 1 : statement 1 ; label 2 statement 2 label 2 : statement 2 ;... label n statement n label n : statement n ; end; selector case selector of label 1 statement 1 label 1 : statement 1 ; label 2 statement 2 label 2 : statement 2 ;. label n statement n label n : statement n ; else statement n+1 statement n+1 ; end; With default case  else No default case

25 2 nd Semester 2004 25 Example: Display DAY from Number Template VAR Day_input : INTEGER; BEGIN WRITE(’Please Enter Day: ’ ); READLN(Day_Input); {Case Selection Here!!!} END.  Day_Input Interpretation 1 Mean Monday 2 Mean Tuesday 3 Mean Wednesday 4 Mean Thursday 5 Mean Friday 6 Mean Saturday 7 Mean Sunday

26 2 nd Semester 2004 26 CASE STATEMENT EXAMPLE without default case case day_input of 1 1: writeln(‘Monday’); 2 2: writeln(‘Tuesday’); 3 3: writeln(‘Wednesday’); 4 4: writeln(‘Thursday’); 5 5: writeln(‘Friday’); 6 6: writeln(‘Saturday’); 7 7: writeln(‘Sunday’); end; day_input :=4; Thursday day_input :=7; Sunday day_input :=0;

27 2 nd Semester 2004 27 CASE STATEMENT EXAMPLE without default case case day_input of 1 1: writeln(‘Monday’); 2 2: writeln(‘Tuesday’); 3 3: writeln(‘Wednesday’); 4 4: writeln(‘Thursday’); 5 5: writeln(‘Friday’); 6 6: writeln(‘Saturday’); 7 7: writeln(‘Sunday’); else writeln(‘Wrong Input Date’); end; day_input :=4; Thursday day_input :=7; Sunday day_input :=0; Wrong Input Date day_input :=15; Wrong Input Date

28 2 nd Semester 2004 28 EXAMPLE PROGRAM use Case statement  Write Case statement for classifying value in variable CH1 INPUT CHAR OUTPUT MESSAGE ‘ A ’ – ‘ Z ’ and ‘ a ’ –’ z ’ Letter ‘ 0 ’ – ‘ 9 ’ Digit Other CharUnknown

29 2 nd Semester 2004 29 Conclusions  Flowchart  IF statement One Alternative – If then Two Alternatives – If then else Multiple Alternatives – Nested If statement  Case statement Case statement without default case Case statement with default case

30 2 nd Semester 2004 30 PROGRAM EXAMPLE  TAX PAYMENT Calculate the TAX that have to pay from the salary… SALARY RANGE BASE TAX 0<=SAL<150000.0015% 1500<=SAL<3000225.0016% 3000<=SAL<5000465.0018% 5000<=SAL<8000825.0020% SAL > 8000.00 1425.0025% PERCENTAGE OF EXCESS

31 2 nd Semester 2004 31 What will we learn in Next Class? Looping Techniques For-loopRepeat-UntilWhile-do


Download ppt "2 nd Semester 2004 1 204111 Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer."

Similar presentations


Ads by Google