Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Logic & program control part 3: Compound selection structures.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
Selection (decision) control structure Learning objective
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions In Python
Main task -write me a program
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
First tutorial.
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
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.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Branches and Program Design
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Course A201: Introduction to Programming 09/16/2010.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Decision Structures, String Comparison, Nested Structures
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Controlling Program Flow with Decision Structures.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Topic: Conditional Statements – Part 1
Selection and Python Syntax
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
CSC115 Introduction to Computer Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
Control Structures: Selection Statement
Visual Basic – Decision Statements
Chapter 5: Control Structure
CS2011 Introduction to Programming I Selections (I)
Control Structures: Selection Statement
Flow Control I Branching and Looping.
Presentation transcript:

Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1

Multiple Outcomes in Branching Examples: – Grading: numeric value  A, B, C, D, or F – 20 questions: Multiple questions Next question depends on answer to previous questions Tools: – elif statement for chain of “else if” – Nested branches

“Else if” Branching Basic structure – if condition1 do something – else if condition2 do something else – else if condition3 do something else –…–… – else do something Basic idea: – If first n conditions fail, try condition n+1 – Stop at first condition that succeeds – If all conditions fail, do something else

elif Syntax if c1: statements if condition1 true elif c2: statements if c1 false but c2 true elif c3: statements if c1, c2 false but c3 true elif c4: statements if c1, c2, c3 false but c4 true … as many elif’s as needed else: statements if all conditions false

Grade Range Example Translating numeric grade to letter grade – 90 & above  A, 80 up to 90  B, 70 up to 80  C, 60 up to 70  D, < 60  F Idea: Each condition eliminates subrange of grades – grade >= 90  A: Remaining number in range 0 – 90, must be B, C, D, or F – grade >= 80  B: Remaining numbers in range 0 – 80, must be C, D, or F – grade >= 70  C: Remaining numbers in range 0 – 70, must be D or F – grade >= 60  C: Remaining numbers in range 0 – 60, must be F

Grade Range Example

Alternatives to elif Branching Could also use separate if statements, boolean operators if grade >= 90: letter = “A” if grade = 80: letter = “B” if grade = 70: letter = “C” if grade = 60: letter = “D” if grade < 60: letter = “F” elif branching  one and only one branch followed – Easier to write reliable code – Still need to do boundary testing

elif and Menus Menu = list of choices for user – Take different actions depending on user choice Can implement with elif – Present list of choices – Input user choice – Use elif to compare with each legal option Put corresponding action down its branch – If reach else user did not choose legal option

elif and Menus

Randomness and Ranges Many games use random values to make decisions – Some actions more likely than other Example: Lottery program – 50%  payout = 0 – 40%  payout = $10 – 9%  payout = $100 – 1%  payout = $1000 Approach: – Generate random value – Create ranges by summing probabilities already covered by peviously checked outcomes – Use elif to implement the ranges

Lottery Example

Nesting and Branches Often have large list of statements in a branch Than list can contain other if statements (sub-branch) – Only executed if first branch followed – Indentation to appropriate level to show level of nesting if condition1: statements if condition1 true if condition2: statements in condition2 sub-branch statements in condition2 sub-branch statements …

Nesting Example 20 questions (actually, much fewer) – Current questions based on answers to previous questions – Prompts and conditions in appropriate branches animal, vegetable, or mineral? animal vegetable mineral reptile or mammal?big or small? rock reptile iguana bark or meow? mammal barkmeow dogcat bigsmall treeflower

Nesting Pseudocode Prompt for animal, vegetable, or mineral If animal: Prompt for reptile or mammal If reptile: Guess iguana If mammal: Prompt for bark or meow If bark: Guess dog If meow: Guess cat If vegetable: Prompt for big or little If big: Guess tree If small: Guess flower If mineral: Guess rock

20 Questions Code