Problem Solving with Decisions

Slides:



Advertisements
Similar presentations
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
Advertisements

Selection (decision) control structure Learning objective
Understanding the Three Basic Structures
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Lesson 5 - Decision Structure By: Dan Lunney
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:
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Chapter 4: Control Structures: Selection
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Problem Solving with Decisions Problem Solving and Programming Concepts 9 th Edition By Maureen Sprankle.
ALGORITHMS AND FLOWCHARTS
Programming Logic and Design Sixth Edition
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Computer Science Selection Structures.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Programming Logic and Design, Second Edition, Comprehensive
Conditionals & boolean operators
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Problem Solving with Decisions
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Chapter 7 Problem Solving with Loops
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
ICS124 Session 9 Flowcharting 1. By the end of this section the student will be able to:  Name the three structures of the Structure Theorem  Identify.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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.
Problem Solving with Decission Structure
Java Project 3 In Class Practice. IF Statements Create a simple program that allows the user to enter a test score as an integer. If the test score is.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Chapter 4 MATLAB Programming
Algorithms and Flowcharts
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Lecture 2: Logical Problems with Choices
ALGORITHMS AND FLOWCHARTS
2-1 Making Decisions Sample assignment statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Understanding the Three Basic Structures
1) C program development 2) Selection structure
ALGORITHMS AND FLOWCHARTS
3. Decision Structures Rocky K. C. Chang 19 September 2018
Algebra: Variables and Expressions
Chapter 3: Selection Structures: Making Decisions
Structural Program Development: If, If-Else
Presentation transcript:

Problem Solving with Decisions Chapter 6

The decision logic structure Uses the If/Then/Else instruction . It tells the computer that If a condition is true , then execute a set of instructions, or Else execute another set of instructions. The else part is optional As there is not always a set of instruction if the condition are false .

Common Forms of decision structure If <condition (s)> Then <True instructions> Else <False instructions> T F A condition can be one of four things : Logical expression: (AND, OR NOT) relational expression(>, <=, <, >=,…..) Variable logic data type. Combination of Relational , logic and mathematical operators. Note: the condition part can combine more than one condition using the logical operators.

Examples . A < B ( a, b the same data type numeric or character ) X + 5 > = Z ( X , Z are numeric ) E < 5 OR F < 10 DataOk ( is logical datum )

The decision logic structure Single condition : A simple decision with only one condition and one action or set of actions . Multiple condition : Decisions in which you have multiple conditions that lead to one action or set of actions . You will use logical operators to connect the conditions .

Single condition one possible action or set of action write an algorithm and corresponding flowchart that read a student grade on a test which is out of 10, then alerts the student if his grade is under 4.

Print “Alert: You must study hard” Flowchart Algorithm StudentAlert() Integer grade Enter grade If grade < 4 Print “Alert: You must study hard” end StudentAlert() Integer grade Enter grade F If grade < 4 T Print “Alert: You must study hard” End

Single Condition one Possible Actions or Sets of Actions assume you are calculating pay at an hourly rate , and overtime pay ( over 40 hours ) at 1.5 times the hourly rate .

Single Condition Two Possible Actions or Sets of Actions

The Multiple If/Then/Else Three type of decision logic used to for solution consisting for more than one decision: Straight-through Logic Positive Logic Negative Logic

The Multiple If/Then/Else 1. Straight-through logic : All of the decisions are processed sequentially , one after the other. 2. positive logic : Not all of the instruction are processed the flow of the processing continues through the module when the resultant of a decisions is true. If the resultant is false another decision is processed until the resultant is true or there is no decision to be processed .

The Multiple If/Then/Else 3. negative logic : the flow of the processing continues through the module when the resultant of a decisions is false . when the resultant is true , another decision is processed until the resultant is false or there is no decision to be processed .

The Nested Decision In algorithms containing multiple decisions, you may write nested If/Then/Else instructions. Decisions using positive and negative logic use nested If/Then/Else instructions. Decisions using straight-through logic do not. Nested If/Then/Else instructions in which each level of a decision is embedded in a level before it. Use the nested If/then/Else when one of several possible actions or set of action are to be processed .

Nested If/Then/Else Instructions

1.Straight-through All decisions (Conditions) are processed sequentially, one after another. There is no else part of the instructions . is required when all the decisions have to be processed , and thy are independent of each other . The false branch always goes to the next decision. The true branch goes to the next decision after the instruction for the true have been processed . Least efficient, why? Because all the decisions must be processed .

1- Straight-Through Logic Example 1 Write an algorithm to change the value of X to 0 when X becomes greater then 100, and to change the value of Y to 0 when Y becomes greater then 250. X > 100 X=0 Y > 250 Y=0

Straight-Through Logic – Example 2

1- Straight-Through Logic Example 2 Write an algorithm to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5

Straight-Through Logic

2. Positive Logic It is way we think If the condition is true  The easiest type to use. Why??!! It is way we think Positive logic works in the following manner: If the condition is true  the computer follows a set of instructions & continues processing. If the condition is false  the computer process another decision until the resultant is true, or there are no more decisions to process.

2. Positive Logic Example 1 Write an algorithm and corresponding flowchart to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5

Positive Logic – Example 1

2. Positive Logic Example 2 Design an algorithm that Calculate the commission rate of a sales person, given the amount of sales. When the salesperson has sold less than or equal to $2000, the commission is 2%. When the sales total is more than $2000, and less than or equal to $4000, the commission is 4%. When the sales total is more than $4000, and less than or equal to $6000, the commission is 7%. When the person has sold more than $6000, the commission is 10%. SALES Commission ≤ 2000 .02 2001 - 4000 .04 4001 – 6000 .07 > 6000 .10

Positive Logic – Example 2

The Conditions in example-2 Set Up in a Different Way

3. Negative Logic If the condition is true  Negative logic works in the following manner: If the condition is true  The computer process another decision until the resultant is false, or there are no more decisions to process. If the condition is false  The computer follows a set of instructions & continues processing. It is often advantageous to use negative logic to decrease the number of tests or to improve the readability of a program .

3. Negative Logic Example 1 Write an algorithm and corresponding flowchart to find the amount to charge people of varying ages for a concert ticket. When a person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10. Age Charge Age < 16 7 Age >= 16 AND Age < 65 10 Age >= 65 5

Negative Logic – Example 1 age =˃ 16 age =˃ 65

2. Negative Logic Example 2 Design an algorithm that Calculate the commission rate of a sales person, given the amount of sales. When the salesperson has sold less than or equal to $2000, the commission is 2%. When the sales total is more than $2000, and less than or equal to $4000, the commission is 4%. When the sales total is more than $4000, and less than or equal to $6000, the commission is 7%. When the person has sold more than $6000, the commission is 10%. SALES Commission ≤ 2000 .02 2001 - 4000 .04 4001 – 6000 .07 > 6000 .10

Negative Logic – Example 2

The Conditions in example-2 Set Up in a Different Way

Logic Conversion If there are no instruction for the true section of decision instruction , Then it is better to convert the logic type : Change all < to >= Change all <= to > Change all > to <= Change all >= to < Change all = to <> Change all <> to = Interchange all of the Then set of instructions with the corresponding Else set of instructions.

Conversion from Positive Logic to Negative Logic

Conversion from Positive Logic to Negative Logic