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

Similar documents
Conditional Execution

Python - Conditional Execution

Conditional Execution

Decision Structures Zelle - Chapter 7

Unit 3. Operators. School of Science and Technology INTRODUCTION

Conditionals and Recursion. Python Part 4

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

STEAM Clown Productions. Python - Conditional. STEAM Clown & Productions Copyright 2018 STEAM Clown. Last Updated: Thursday, January 24, 2019.

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Control Structures in Java if-else and switch

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Selection Statement ( if )

Lecture 3 Tao Wang 1

More Complex Versions of the if Statement. Class 13

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Conditional Expressions and Decision Statements

Chapter 4: Control Structures I (Selection)

Flow Control. So Far: Writing simple statements that get executed one after another.

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Computer Programming: C++

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

REVIEW. The C++ Programming Language. CS 151 Review #2

CS1 Lecture 5 Jan. 26, 2018

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Operators. Java operators are classified into three categories:

Ruby: Introduction, Basics

Chapter 2, Part III Arithmetic Operators and Decision Making

Beyond Blocks: Python Session #1

Python for Informatics

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

4. Inputting data or messages to a function is called passing data to the function.

Selection the If Statement Try Catch and Validation

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

CS1 Lecture 5 Jan. 25, 2019

cs1114 REVIEW of details test closed laptop period

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Ruby: Introduction, Basics

Full file at C How to Program, 6/e Multiple Choice Test Bank

ENGR 101 Engineering Design Workshop

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Lecture 8. Conditionals & Control Flow

Decisions, Decisions. Testing, testing C H A P T E R 7

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Text Input and Conditionals

Conditionals & Loops /

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Operators & Expressions

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Chapter 4: Basic C Operators

CSCE 110 Programming I

Chapter 3: Operators, Expressions and Type Conversion

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

COMP 110 Introduction to Programming. What did we discuss?

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Formatting & Style Examples

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Fundamentals of Programming (Python) Getting Started with Programming

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Comp 151. Control structures.

Lecture 3 Operators MIT AITI

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 4: Conditionals and Recursion

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Object Oriented Programming with Java

A B C ((NOT A) AND B) OR C

UNIT- 3 Introduction to C++

Information Science 1

Chapter 8. Statement-Level Control Structures

COS 126 General Computer Science Spring Written Exam 1

Control Structures in Java if-else and switch

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Course Outline. Introduction to java

Programming for Engineers Introduction to C

LECTURE 02 INTRODUCTION TO C++

Algorithms and Data Structures

Arithmetic type issues

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

Lecture 4 CSE July 1992

Relational operators (1)

Transcription:

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

The modulus operator It works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. >>> quotient = 7 / 3 >>> print quotient 2 >>> remainder = 7 % 3 >>> print remainder 1 2

Use case (1) q You can check whether one number is divisible by another if x % y is zero, the x is divisible by y >>> isdivisible = 6 % 3 >>> print isdivisible 0 >>> isnotdivisible = 7 % 3 >>> print isnotdivisible 1 3

Use case (2) q You can extract the right-most digit or digits from a number. q For example, the last two digits in 123491 is 91. >>> lasttwodigits = 123491 % 100 >>> print lasttwodigits 91 >>> lastonedigit = 123491 % 10 >>> print lastonedigit 1 4

Boolean expressions A boolean expression is an expression that is either true or false. x == y x is equal to y x!= y x is not equal to y x > y x is greater than y x < y x is less than y x >= y x is greater than or equal to y x <= y x is less than or equal to y >>> x = 5 >>> y = 7 >>> x >= y False 5

Logical operators There are three logical operators: and, or, and not. q and expression is true if and only if both operands are true q or expression is true if either one of operands is true q not: reverse the value >>> x = 5 >>> y = 7 >>> x+y>10 and x-y<0 True >>> x+y>10 or x-y<0 True >>> not (x+y>10) False 6

Conditional execution x = 5 x < 10? Yes Program: No x > 20? No print 'Smaller' Yes print 'Bigger' x = 5 if x < 10: print 'Smaller if x > 20: print 'Bigger' print 'Finis' Output: Smaller Finis print 'Finis' 7

If statement Syntax: if expression: statement 1 statement 2 statement N If the expression is true, statements within the if statement body will be executed, otherwise the entire if statement will be ignored if statement indention >>> x = 5 >>> y = 7 >>> if x+y > 10:... Print sum > 10 sum > 10 8

If statement x = 5 print 'Before 5' if x == 5 : print 'Is 5' print 'Is Still 5' print 'Third 5' print 'Afterwards 5 print 'Before 6 if x == 6 : print 'Is 6' print 'Is Still 6' print 'Third 6' print 'Afterwards 6' Before 5 Is 5 Is Still 5 Third 5 Afterwards 5 Before 6 Afterwards 6 x == 5? No Yes print 'Is 5' print 'Still 5' print 'Third 5' 9

Indention Increase indent indent after an if statement (after : ) Maintain indent to indicate the scope of the block (which lines are affected by the if) Reduce indent back to the level of the if statement to indicate the end of the block Blank lines are ignored - they do not affect indentation Comments on a line by themselves are ignored with regard to indentation

Indention Increase / maintain after if Decrease to indicate end of block x = 5 if x > 2 : print 'Bigger than 2' print 'Still bigger' print 'Done with 2

Think about begin/end blocks x = 5 if x > 2 : print 'Bigger than 2' print 'Still bigger' print 'Done with 2

Nested if-statements x = 42 if x > 1 : print 'More than one' if x < 100 : print 'Less than 100' print 'All done' x > 1 no yes print 'More than one' x < 100 no yes print 'Less than 100' print 'All Done'

if-else statement Sometimes we want to do one thing if a logical expression is true and something else if the expression is false. It is like a fork in the road we must choose one or the other path X = 4 no x > 2 yes print 'Not bigger' print bigger' print 'All Done' but not both 14

if-else statement Syntax: if expression: statement 1 statement 2 if-else statement indention else: indention statement N statement 1 statement 2 statement N if body else body If the expression is true, statements within the if statement body will be executed, otherwise statements within else body will be executed. 15

if-else statement x = 4 if x > 2 : print 'Bigger' else : print 'Smaller' X = 4 no x > 2 print 'Not bigger' yes print bigger' print 'All done' print 'All Done' 16

if-else statement x = 4 if x > 2 : print 'Bigger' else : print 'Smaller' X = 4 no x > 2 print 'Not bigger' yes print bigger' print 'All done' print 'All Done' 17

Multi-way x < 2 no x < 10 no print large' yes print small' print medium' print 'All Done' 18

if-elif-else statement Syntax: if expression 1: if-elif-else statement indention statements (1) elif expression 2: indention else: indention statements (2) statements (3) if body elif body else body If the expression 1 is true, statements (1) within the if statement body will be executed, otherwise if expression 2 is true, statements (2) within elif body will be executed, otherwise, statements (3) within else body will be executed. 19

Variations (1) Syntax: if expression 1: statements (1) elif expression 2: statements (2) statements after if-elif # No Else x = 5 if x < 2 : print 'Small' elif x < 10 : print 'Medium' print 'All done' 20

Variations (2) Syntax: if expression 1: statements (1) elif expression 2: statements (2) elif expression 3: statements (3) else: statements (N) if x < 2 : print 'Small' elif x < 10 : print 'Medium' elif x < 20 : print 'Big' elif x < 40 : print 'Large' elif x < 100: print 'Huge' else : print 'Ginormous' 21

Puzzles Which will never print? if x < 2 : print 'Below 2' elif x >= 2 : print 'Two or more' else : print 'Something else' if x < 2 : print 'Below 2' elif x < 20 : print 'Below 20' elif x < 10 : print 'Below 10' else : print 'Something else' 22

Nested cases All statements in the body of if, elif, and else can also be conditional statements. if x < 2 : print 'Small' elif x < 10 : print 'Medium' elif x < 20 : print 'Big elif x < 100: print 'Huge' else : print 'Ginormous' if x < 100 : if x < 20 : if x < 10 : print Small else: print Big else: print 'Huge' else : print 'Ginormous' 23

Nested cases All statements in the body of if, elif, and else can also be conditional statements. if x < 2 : print 'Small' elif x < 10 : print 'Medium' elif x < 20 : print 'Big elif x < 100: print 'Huge' else : print 'Ginormous' if x < 2 : print 'Small' else: if x < 10 : print 'Medium' elif x < 20 : print 'Big elif x < 100: print 'Huge' else : print 'Ginormous' 24