CSc 110, Autumn Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

Similar documents
CSc 110, Spring Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges

Lecture 4: Conditionals

AP Computer Science. if/else, return values. Copyright 2010 by Pearson Education

Returns & if/else. Parameters and Objects

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Building Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Building Java Programs

Topic 12 more if/else, cumulative algorithms, printf

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy

CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs Chapter 4

CSc 110, Autumn Lecture 26: Assertions. Adapted from slides by Marty Stepp and Stuart Reges

Topic 11 Scanner object, conditional execution

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

Building Java Programs

CSc 110, Autumn Lecture 13: Cumulative Sum and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

Checking Multiple Conditions

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topic 11 Scanner object, conditional execution

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topic 12 more if/else, cumulative algorithms, printf

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

CSc 110, Spring Lecture 13: Random numbers and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

Making Decisions In Python

Python Programming: An Introduction to Computer Science

Week 4. Strings, if/else, return, user input

CS Boolean Statements and Decision Structures. Week 6

Decision Structures CSC1310. Python Programming, 2/e 1

Python - Week 3. Mohammad Shokoohi-Yekta

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

CSc 110, Autumn Lecture 14: Strings. Adapted from slides by Marty Stepp and Stuart Reges

Problem 1.1 (3 pts) :Python uses atomic data types and builds up from there. Give an example of: a. an int b. a double c. a string

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

Introduction to Computer Programming for Non-Majors

CSc 110, Autumn Lecture 30: Methods. Adapted from slides by Marty Stepp and Stuart Reges

Programming for Engineers in Python. Autumn

Advanced if/else & Cumulative Sum

CMSC201 Computer Science I for Majors

Making Decisions In Python

Introduction to Computer Programming for Non-Majors

LAB 4.1 Relational Operators and the if Statement

CSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

CSc 110, Autumn Lecture 11: Strings. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring Lecture 14: Booleans and Strings. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn 2016 Lecture 7: Graphics. Adapted from slides by Marty Stepp and Stuart Reges

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Introduction to Computer Programming for Non-Majors

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Selections. CSE 114, Computer Science 1 Stony Brook University

5. Selection: If and Switch Controls

CSc 110, Spring Lecture 10: Strings. Adapted from slides by Marty Stepp and Stuart Reges

Control flow statements

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Conditional Expressions and Decision Statements

Tanita Health Ware Help

A453 Task 1: Analysis: Problem: Solution:

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Topic 13 procedural design and Strings

Building Java Programs

Creating New Variables in JMP Datasets Using Formulas Exercises

Selection Statement ( if )

More Complex Versions of the if Statement. Class 13

Lecture 02 Making Decisions: Conditional Execution

CS1100: Excel Lab 1. Problem 1 (25 Points) Filtering and Summarizing Data

Building Java Programs

CS 1301 Exam 1 Answers Fall 2009

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Introduction to Computer Programming for Non-Majors

Python Programming: An Introduction to Computer Science

Math 101 Final Exam Study Notes:

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

File processing and decision structures

Introduction to Computer Programming for Non-Majors

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

cs1114 REVIEW of details test closed laptop period

Comp 150 Exam 2 Overview.

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Algorithmic Thinking: Loops and Conditionals

Multi-Way Decisions. The newest program is great, but it still has some quirks! This program finds the real solutions to a quadratic

Control Structures in Java if-else and switch

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Lab - 8 Awk Programming

Computer Science & Engineering 150A Problem Solving Using Computers

Boolean evaluation and if statements. Making decisions in programs

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs

Summary of the Lecture

GUIDED NOTES 3.1 FUNCTIONS AND FUNCTION NOTATION

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Control Structures in Java if-else and switch

BIL101E: Introduction to Computers and Information systems Lecture 8

Lecture 19: Recursion

Decision Making in C

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Transcription:

CSc 110, Autumn 2017 Lecture 11: if / else Adapted from slides by Marty Stepp and Stuart Reges

The if statement statement... statement Executes a block of statements only if a test is true Example: gpa = float(input("gpa? ")) if gpa >= 2.0: print("application accepted.")

The if/else statement else: Executes one block if a test is true, another if false Example: gpa = float(input("gpa? ")) if gpa >= 2.0: print("welcome to Mars University!") else: print("application denied.")

Relational expressions if statements use logical tests. if i <= 10:... These are boolean expressions Tests use relational operators: Operator Meaning Example Value == equals 1 + 1 == 2 True!= does not equal 3.2!= 2.5 True < less than 10 < 5 False > greater than 10 > 5 True <= less than or equal to 126 <= 100 False >= greater than or equal to 5.0 >= 5.0 True

Misuse of if What's wrong with the following code? percent = float(input("what percentage did you earn? ")) if percent >= 90: print("you got an A!") if percent >= 80: print("you got a B!") if percent >= 70: print("you got a C!") if percent >= 60: print("you got a D!") if percent < 60: print("you got an F!")...

Nested if/else el else: Chooses between outcomes using many tests Example: if x > 0: print("positive") elif x < 0: print("negative") else: print("zero")

Nested if/elif/elif If it ends with else, exactly one path must be taken. If it ends with if, the code might not execute any path. el el Example: if place == 1: print("gold medal!") elif place == 2: print("silver medal!") elif place == 3: print("bronze medal.")

Nested if structures exactly 1 path (mutually exclusive) el else: 0 or 1 path (mutually exclusive) el el 0, 1, or many paths (independent tests; not exclusive)

Which nested if/else? (1) if/if/if (2) nested if/else (3) nested if/elif/elif Whether a user is lower, middle, or upper-class based on income. (2) nested if / elif / else Whether you made the dean's list (GPA 3.8) or honor roll (3.5-3.8). (3) nested if / elif Whether a number is divisible by 2, 3, and/or 5. (1) sequential if / if / if Computing a grade of A, B, C, D, or F based on a percentage. (2) nested if / elif / elif / elif / else

Nested if/else question Write a program that produces output like the following: This program reads data for two people and computes their basal metabolic rate and burn rate. Enter next person's information: height (in inches)? 73.5 weight (in pounds)? 230 age (in years)? 35 gender (male or female)? male Enter next person's information: height (in inches)? 71 weight (in pounds)? 220.5 age (in years)? 20 gender (male or female)? female Person #1 basal metabolic rate = 2042.3 high resting burn rate Person #2 basal metabolic rate = 1868.4 moderate resting burn rate Basal Metabolic Rate Formula: male BMR = 4.54545 x (weight in lb) + 15.875 x (height in inches) - 5 x (age in years) + 5 female BMR = 4.54545 x (weight in lb) + 15.875 x (height in inches) - 5 x (age in years) - 161 BMR Burn Level below 12000 low 1200 to 2000 moderate above 2000 high

Nested if/else answer # This program finds the basal metabolic rate (BMR) for two # individuals. This variation includes several functions # other than main. # introduces the program to the user def give_intro(): print("this program reads data for two") print("people and computes their basal") print("metabolic rate and burn rate.") print() # prompts for one person's statistics, returning the BMI def get_bmr(person): print("enter person", person, "information:") height = float(input("height (in inches)? ")) weight = float(input("weight (in pounds)? ")) age = float(input("age (in years)? ")) gender = input("gender (male or female)? ") bmr = bmr_for(height, weight, age, gender) print() return bmr...

Nested if/else, cont'd. # this function contains the basal metabolic rate formula for # converting the given height (in inches), weight # (in pounds), age (in years) and gender (male or female) into a BMR def bmr_for(height, weight, age, gender): bmr = 4.54545 * weight + 15.875 * height - 5 * age if gender.lower() == "male": bmr += 5 else: bmr -= 161 return bmr # reports the overall bmr values and status def report_results(bmr1, bmr2): print("person #1 basal metabolic rate =", round(bmr1, 1)) report_status(bmr1) print("person #2 basal metabolic rate =", round(bmr2, 1)) report_status(bmr2) # reports the burn rate for the given BMR value def report_status(bmr): if bmr < 1200: print("low resting burn rate"); elif bmr <= 2000: print("moderate resting burn rate") else: # bmr1 > 2000 print("high resting burn rate") def main(): give_intro() bmr1 = get_bmr(1) bmr2 = get_bmr(2) print(bmr1, bmr2) report_results(bmr1, bmr2) main()