Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Making Decisions In Python
Exceptions COMPSCI 105 S Principles of Computer Science.
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.
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
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.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
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.
Xiaojuan Cai Computational Thinking 1 Lecture 7 Decision Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Lesson 06: Functions Class Participation: Class Chat:
Lesson 03: Variables and Types
Exam #1 You will have exactly 30 Mins to complete the exam.
Decision Structure ISYS 350.
Lesson 10: Dictionaries Topic: Introduction to Programming, Zybook Ch 9, P4E Ch 9. Slides on website.
Making Choices with if Statements
Lesson 05: Iterations Class Chat: Attendance: Participation
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CSc 110, Spring 2017 Lecture 8: input; if/else
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lesson 04: Conditionals Class Chat: Attendance: Participation
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Exception Handling.
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Computers & Programming Languages
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
CSc 110, Autumn 2016 Lecture 9: input; if/else
HAPPY NEW YEAR! Lesson 7: If-statements unplugged
Lesson 06: Functions Class Chat: Attendance: Participation
Conditions and Boolean Expressions
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
3. Decision Structures Rocky K. C. Chang 19 September 2018
Lesson 03: Variables and Types
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
15-110: Principles of Computing
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Lesson 10: Dictionaries Class Chat: Attendance: Participation
By Hector M Lugo-Cordero September 3, 2008
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Introduction to Programming
Control Statements.
Relational and Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Relational and Logical Operators
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
IST256 : Applications Programming for Information Systems
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.

Wake Up Call (As of 2:30pm) 4 Students Haven’t taken the quiz due TODAY! 4 Students have a perfect score of 40 pts. 16 Students have >= 36 pts or above (A- or higher) GOOD WORK! 7 Students have 30 >= grade <= 35 pts (between a B- and B+) First exam is in 2 weeks! Zybook 17 Students have completed less than 50% of this weeks reading 7 of those 17 never even looked at it. 2 of those 7 got a 3 or less on the quiz after 2 attempts.

Agenda Non-Linear Code Execution Relational and Logical Operators Different types of non-linear execution. Run-Time error handling You’ve Read: Zybook Ch3 P4E Ch3

Connect Activity A Boolean value is a/an ______? True or False value Zero-based value Non-Negative value Alphanumeric value

What is a Boolean Expression? A Boolean expression evaluates to a Boolean value of True or False. Boolean expressions ask questions. GPA >3.2  Is GPA greater than 3.2? The result of which is True or False based on the evaluation of the expression: GPA = 4.0  GPA > 3.2  True GPA = 2.0  GPA > 3.2  False

Program Flow Control with IF The IF statement is used to branch your code based on a Boolean expression. if boolean-expression: statements-when-true else: statements-when-false

Python’s Relational Operators What it does Example > Greater than 4 > 2 (True) < Less than 4 < 2 (False) == Equal To 4 == 2 (False) != Not Equal To 4 != 2 (True) >= Greater Than or Equal To 4 >=2 (True) <= Less Than or Equal To 4 <=2 (True) Expressions consisting of relational operators evaluate to a Boolean value

Watch Me Code 1 Do you need more milk? When the Lyga family has less than 1 gallon of milk, we need more!

Python’s Logical Operators What is does Example and True only when both are True. 4>2 and 4<5 (True) or False only when both are False 4<2 or 4==4 (True) not Negation (Opposite) not 4 == 2 (True) in Set Operator 4 in [2,4,7] (True)

Check Yourself: Logical Operators Choose the line number in which the Boolean expression is True (choose only from lines: 4, 5, 6 or 7).

Multiple Decisions: IF ladder Use elif to make more than one decision in your if statement if boolean-expression1: statements-when-exp1-true elif boolean-expression2: statements-when-exp2-true else: statements-when-false

End-To-End Example, Part 1 Tax Calculations! The country of “Syracusia” determines your tax rate from the number of dependents: 0  30% 1  25% 2  18% 3 or more 10% Write a program to prompt for number of dependents (0-3) and annual income. It should then calculate your tax rate and tax bill. Format numbers properly!

Handle Bad Input with Exceptions Exceptions represent a class of errors which occur at run-time. We’ve seen these before when run a program and it crashes due to bad input. And we get a TypeError or ValueError. Python provides a mechanism try .. except to catch these errors at run-time and prevent your program from crashing. Exceptions are exceptional. They should ONLY be used to handle unforeseen errors in program input.

Watch Me Code 2 The need for an exception handling: Bad input try except finally Good practice of catching the specific error

Try…Except…Finally try: statements-which-might throw-a-runtime-error except exceptionType: code-to-run-when-error-occurs finally: code-to-run-after-try-or-except Optional

Check Yourself: Conditionals What prints on line 9 when you input the value '-45s'? 'a' 'b' 'c'

End-To-End Example, Part 2 Tax Calculations! Modify “Fudgebonia” tax calculations to handle bad inputs so that it will not generate run-time errors.

In Class Coding Lab: The goals of this lab are to help you to understand: Relational and Logical Operators Boolean Expressions The if statement Try / Except statement How to create a program from a complex idea.