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

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem
Flow Control Analysis & Design Tool: Flowcharts
Subject: Information Technology Grade: 10
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Ordinal Data Types CSCE 110. Data Types simple structured pointer ordinalreal predefined booleancharinteger programmer-defined enumeratedsubrange arrayrecord.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Chapter 6 Control Structures.
Chapter 4: Control Structures: Selection
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Selection in C.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Chapter 1 Pseudocode & Flowcharts
Chapter 2 - Algorithms and Design
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
The Select Case Statement. Slide 4- 2 Select Case Statement Similar to If…Then…ElseIf  Performs a series of tests  Conditionally executes the first.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Software Life Cycle What Requirements Gathering, Problem definition
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Flowcharts.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Visual Basic Programming
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang
Week 4 Program Control Structure
Calvin College Controlling Behavior The if, switch and for Statements.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
COMP Loop Statements Yi Hong May 21, 2015.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Decision making If.. else statement.
INTRODUCTION TO PROBLEM SOLVING
Decisions Chapter 4.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
SELECTION STATEMENTS (2)
Decision making If statement.
Faculty of Computer Science & Information System
Decision I (if Statement)
Week 3 – Program Control Structure
Flowcharts and Pseudo Code
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Chapter 4: Selection Structures: if and switch Statements
Control Structures.
Presentation transcript:

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