CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

30/04/ Selection Nested If structures & Complex Multiple Conditions.
Subject: Information Technology Grade: 10
 Control structures  Algorithm & flowchart  If statements  While statements.
Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.
CP Week 10 Modularising programs using Procedures.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
Computer Science 1620 Programming & Problem Solving.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 Chapter 8 Designing Small Programs. 2 A ‘Procedure’ v A set of instructions which describe the steps to be followed in order to carry out an activity.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.
CP Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.
Selection in C.
ALGORITHMS AND FLOWCHARTS
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Algorithms In general algorithms is a name given to a defined set of steps used to complete a task. For example to make a cup of tea you would fill the.
Computer Science Selection Structures.
Chapter 3 Making Decisions
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
End Show Writing a computer program involves performing the following tasks. 1. Understanding the problem 2. Developing an Algorithm for the problem 3.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
Problem Solving with Decisions
CHAPTER 3 SELECTION STRUCTURE.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
ITEC113 Algorithms and Programming Techniques
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
22/11/ Selection If selection construct.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Problem Solving with Decisions
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
31/01/ Selection If selection construct.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Concepts of Algorithms CSC-244 Unit Zero Pseudo code, Flowchart and Algorithm Master Prince Computer College Qassim University K.S.A.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Controlling Program Flow with Decision Structures.
Algorithms and Pseudocode
Decision Making and Branching
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and Flowcharts -
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Selection Using IF THEN ELSE CASE Introducing Loops.
Algorithms and Flowcharts
SELECTION CONTROL STRUCTURE Prepared by: Careene McCallum-Rodney.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
ANALYSIS AND ALGORITHM DESIGN - III
Selection Control Structure
VB.Net Programming Console Application
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Introduction To Flowcharting
ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
If selection construct
` Structured Programming & Flowchart
If selection construct
Review of Previous Lesson
Control Structures.
Presentation transcript:

CP Week 4 Making Decisions

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice that you do not have much petrol left. You will need to fill up soon, and approaching you can see a petrol station. The price is reasonable, but you do not have a lot of time to spare, so don't want to have to queue to fill-up. What would you do? ?

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions in Problem Solving z"If the queue at the petrol station is short then I will stop there to fill up" zIf (the queue at the petrol station is short) Then stop there and fill up Stop? No! Yes

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions in Problem Solving No! Yes z"If the queue at the petrol station is short then I will stop there to fill up" zIf (the queue at the petrol station is short) Then stop there and fill up Decisions...decisions..

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner You may decide: "if the queue at the petrol station is short then I will stop there to fill up". We could write the algorithm for this decision as: If queue at petrol station is short then stop there and fill up

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Further Examples if kettle has boiled then make tea if temperature less than 18 C. then turn on central heating

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner General form of IF statements IF condition THEN ENDIF IF, THEN and ENDIF are RESERVED words condition is the “test”, if the answer is YES then we carry out the

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner An example program REM program : to demonstrate the IF statement REM written by : S. Garner REM date written 8/3/00 DIM iAge AS INTEGER CLS ' clear the screen INPUT "Please enter your age "; iAge REM test the condition IF iAge > 17 THEN PRINT "You may vote at the next election" END IF END

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Two way decisions We frequently need to do either one thing or another, depending on some condition If age is greater than 65 then retire gracefully else keep working

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Basic IF..THEN..ELSE IF condition THEN ELSE ENDIF

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Example program REM program : to demonstrate the IF statement DIM iMark AS INTEGER CLS ' clear the screen INPUT "Please enter your mark(0-100) "; iMark REM check mark for pass or fail IF iMark < 40 THEN PRINT "You have failed" ELSE PRINT "You have passed" PRINT "Well Done!" END IF END

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Testing We now have more than one possible “route” through our code We must TEST each of these! We should also test the “boundary”

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Test Data MarkExpected ActualResult 25You have failed You have failed 60You have passed You have passed 40You have passed You have passed 39You have failed You have failed

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The condition statement Usually we check a value. The symbols used are: = equal<>not equal <less than <=less than or equal >greater than >=Greater than or equal

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Example Conditions NOTE: Brackets help to clarify! (iMark < 0) (iAge >=18) (iValueA = iValueB) (iAge >= 16 AND iAge < 65)

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner More Complex Decisions 1 zProblem: You are looking for new employees for your company. One of the criteria is that the employee must be no younger than 16 and no older than 65. zNote: We have two conditions to satisfy: y condition 1is the candidate at least 16 years old? y condition 2is the candidate no older than 65? zBoth condition 1 AND condition 2 must be satisfied "TRUE" in order to accept the candidate

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner More Complex Decisions 2 zAlgorithm: Step 1Get age of candidate 2If ( age at least 16) AND (age less than 65 ) 2.1Then candidate is eligible 2.2Else reject candidate zAND implies that both conditions must be true

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The code INPUT “How old is the candidate > ”; iCandidatesAge If (iCandidatesAge >= 16) And (iCandidatesAge <= 65) Then Print “You are eligible to apply” Else Print “You are outside the age range!” End If

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The OR condition zAlternatively test for ineligible candidates: Step 1Get age of candidate 2If ( age less than 16) OR (age greater than 65 ) 2.1Then reject candidate 2.2Else candidate is eligible zOR implies that either one (or both) of the conditions needs to be satisfied

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Another Example REM program : to demonstrate the IF statement REM written by : I Coulson REM date written: 8/3/00 DIM iCandAge AS INTEGER CLS ' clear the screen INPUT "Please enter your age "; iCandAge IF ( iCandAge 65) THEN PRINT ”Sorry you are outside the age range" ELSE PRINT “You are eligible to apply” END IF END

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Problem - Student Grades zWhen a piece of work is marked, it is given a percentage mark which needs converting to a FAIL, PASS, MERIT or DISTINCTION. zA Fail Upto 40 zA Pass zA Merit zA Distinction80 +

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Mark to Grade Conversion Algorithm zAlgorithm: Step 1 Get a student's mark 2If (mark is greater than 0) AND (mark less than 40) 2.1Then Grade is Fail 3If (mark greater than or equal to 40) AND (mark is less than 60) 3.1Then Grade is Pass 4If (mark is greater or equal to 60) AND (mark less than 80) 4.1Then Grade is Merit 5If (mark is greater or equal to 80) AND (mark is no more than 100) 5.1Then Grade is Distinction 6Display Grade

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Mark to Grade Conversion Improved Algorithm zFurther improved algorithm: Step 1Get a student's mark 2If (mark is less than 40%) 2.1Then Grade is Fail 2.2Else If (mark is less than 60%) Then Grade is Pass 2.2.2ElseIf (mark is less than 80%) Then Grade is Merit Else Grade is Distinction 3Display Grade zThis is known as nesting decisions

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The Program Rem Author I Coulson DIM iPercentage AS INTEGER INPUT ”What percentage did you get "; iPercentage IF iPercentage < 40 THEN Print “Fail” ELSEIF iPercentage < 60 THEN Print “Pass” ELSEIF iPercentage < 80 THEN Print “Merit” ELSEIF iPercentage >= 80 THEN Print “Distinction” END IF

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Questions 1 Write an algorithm to decide if a salesman should get a bonus - he needs to have sold at least £3000 worth of goods in the month. 2 Alter the algorithm such that that the salesman earns 15% commission on all sales if sells more than £3000 worth of goods in a month, but only 5% if he sells less than that. 3 Write the code to print the appropriate comment to a runner finishing a race: 1st place - “well done you are the winner” 2nd place - “congratulations you are runner up” 3rd place- “good, you have finished third” unplaced - “You’ve finished, well done”

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner End of lecture