Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Python Programming Language
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
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.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Geography 465 Assignments, Conditionals, and Loops.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Q and A for Section 4.4 CS 106, Fall Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Introduction to Decision Structures and Boolean Variables
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Line Continuation, Output Formatting, and Decision Structures
Python: Control Structures
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Flow of Control.
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Transition to Code Upsorn Praphamontripong CS 1111
Topics The if Statement The if-else Statement Comparing Strings
Conditionals & Boolean Expressions
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
Class 12.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Conditions and Boolean Expressions
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Decision Structures and Boolean Variables
CS 1111 Introduction to Programming Spring 2019
Topics The if Statement The if-else Statement Comparing Strings
Types, Truth, and Expressions (Part 2)
Python Programming Language
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Logic of an if statement
Boolean Expressions to Make Comparisons
Class 13 function example unstring if if else if elif else
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
CprE 185: Intro to Problem Solving (using C)
Types, Truth, and Expressions
Lecture 9: Implementing Complex Logic
Chapter 2, Part III Arithmetic Operators and Decision Making
Introduction to Python
Presentation transcript:

Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016 Decision Making Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016

What is a Decision Statement? A statement that evaluates some conditions to true or false. A condition (or expression) Must always evaluate to true or false, i.e., “Boolean expression” Use relational operators: >, <, >=, <=, ==, != if condition: statement statement … age 19 1Part of a McDonald’s commercial from 1998 CS 1110

Calculations that Evaluate to Boolean Values < ≤ > ≥ all evaluate to true or false 3 < 2 is false ==, != also evaluate to true or false 3 == 3 is true 3 == 4 is false “jello” != “blue” is true age > 21 True False “Boolean value” CS 1110

Decision Structure Simple structure Dual Structure if condition: statements1 if condition: statements1 else: statements2 Indent the block of statement condition statements1 True False condition statements1 True False statements2 CS 1110

Two Types of Decisions Sequence Decision Nested Decision if condition: block of statements ... else: if condition: block of statements ... elif: else: Independent decisions do not remember the results of decisions made before them Nested decisions remember the results of decisions made before them (in the same nesting) CS 1110

if Statements in Python operation “subtraction” if is a keyword, the if statement must end in a colon What belongs to a particular if statement is indented CS 1110

elif Statements in Python operation “multiplication” elif is a keyword; it stands for else if elif is attached to an if or elif before it, and indicates this elif is nested You cannot have a standalone elif CS 1110

if versus elif The one on the left returns 6 result 6 result 6 5 4 operation “addition” operation “addition” The one on the left returns 6 if-elif statements are nested, linked, and mutually exclusive. The one on the right returns 4 The plain if statements are not mutually exclusive, don’t know about each other, and thus all if statements get executed CS 1110

else statements int(input("Enter number1: ")) int(input("Enter number2: ")) else is a keyword, linked to an if or elif, and get executed if the if/elif above it is false CS 1110

else statements (2) operation “foo” result operation undefined else only gets executed if none of the if or elif before it are true CS 1110

Indentation Matters num1 1 num2 1 3 result “num1 is 1” “num1 is 1 num2 <= 3 finished num1” “num1 is 1 num2 <= 3” “num1 is 0” “” This is another type of “nesting”, and is usually referred to as “nested if-else statements” ( ) ( ) ( ) ( ) CS 1110

Indentation Matters num1 1 num2 2 result “num1 is 1 num2 <= 3 finished num1" “” “num1 is 1 " “num1 is 1 num2 <= 3" ( ) This is another type of “nesting”, and is usually referred to as “nested if-else statements” ( ) ( ) ( ) CS 1110

Indentation Matters Indentation – group things num1 2 num2 1 result “” “num1 is not 0 or 1” Indentation – group things if, elif, and else are mutually exclusive ( ) ( ) ( ) ( ) This is another type of “nesting”, and is usually referred to as “nested if-else statements” CS 1110

Unreachable statements num1 1 num2 5 result “” “num1 is 1 ” “num1 is 1 num2 > 3 finished num1” “num1 is 1 num2 > 3” print template(1, 5) CS 1110

Programming TRAP Assignment statement x = “CS1110” Boolean expression x == “CS1110” x CS1110 CS 1110

Boolean Types The code above returns True True and False are both keywords and types in Python Capitalization !! not is a keyword that negates a boolean value The code above returns True CS 1110

Boolean Values and Calculations x y x and y False True x y x or y False True A boolean value must evaluate to true or false Two boolean values can be compared with and or or Use parentheses if you want to combine and or or to disambiguate; e.g., (x and y) or z or x and (y or z) You can use any logical operators: and or or or not F T x=F, y=T, z=T T F CS 1110

Exercise Write a program that accepts six project scores. All scores are out of 100 points, except the first (which is out of 30), and the fifth (which is out of 50). The program provides the highest average grade for all the projects by dropping the one with the lowest percentage. For example, if a student got a 65% on the first project (out of 30) and a 55% on the fifth project (out of 50), that fifth project would be dropped from the average calculation. Do not use built-in functions/methods such as max() or sort() CS 1110