2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
2 nd Semester Outline How to Write Flowchart Selection Statement IF Statement Case Statement Selection Statement Example
2 nd Semester Do you remember what we learn last week? Basic Input/Out/Assignment Statement Arithmetic/Boolean Expression
2 nd Semester Outline How to Write Flowchart Selection Statement IF Statement Case Statement Selection Statement Example
2 nd Semester 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
2 nd Semester Our View: Problem 1 Problem Checklist 1 Input 2 How to process Input (Algorithm) 3 Output Our Program 1 2 3
2 nd Semester 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
2 nd Semester Tools for Represent IDEA Flowchart - Graphical representation of algorithm Terminator Process Input/output Decision Connector Flow line
2 nd Semester 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;
2 nd Semester Flowchart EXAMPLE 2 Problem: FAT/SLIM???
2 nd Semester Outline How to Write Flowchart Selection Statement IF Statement Case Statement Selection Statement Example
2 nd Semester IF STATEMENT 3 Types One Alternative Two Alternatives Multiple Alternatives More than one IF STATEMENT
2 nd Semester IF STATEMENT with One Alternative IF – THEN IF condition THEN statement1; statement3; TRUE Statement3 condition False True Statement1 FALSE
2 nd Semester 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.
2 nd Semester IF STATEMENT with Two Alternative IF – THEN – ELSE IF condition THEN statement1 ELSE statement2; statement3; condition FalseTrue Statement2Statement1 TRUE FALSE Statement3
2 nd Semester 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.
2 nd Semester 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;
2 nd Semester 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.
2 nd Semester 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
2 nd Semester 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;
2 nd Semester 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
2 nd Semester Outline How to Write Flowchart Selection Statement IF Statement Case Statement Selection Statement Example
2 nd Semester 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
2 nd Semester 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
2 nd Semester 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
2 nd Semester 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;
2 nd Semester 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
2 nd Semester 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
2 nd Semester 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
2 nd Semester PROGRAM EXAMPLE TAX PAYMENT Calculate the TAX that have to pay from the salary… SALARY RANGE BASE TAX 0<=SAL< % 1500<=SAL< % 3000<=SAL< % 5000<=SAL< % SAL > % PERCENTAGE OF EXCESS
2 nd Semester What will we learn in Next Class? Looping Techniques For-loopRepeat-UntilWhile-do