COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING FLOATING-POINT NUMBERS Jehan-François Pâris
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Computer Science 111 Fundamentals of Programming I Number Systems.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
C++ Programming: Basic Elements of C++.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Python Conditionals chapter 5
ICT Introduction to Programming Chapter 4 – Control Structures I.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Control flow Ruth Anderson UW CSE 160 Winter
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
William Stallings Computer Organization and Architecture 8th Edition
Control Structures I Chapter 3
Chapter 3 Selection Statements
Morgan Kaufmann Publishers
Topics Designing a Program Input, Processing, and Output
Introduction to Python
BASIC ELEMENTS OF A COMPUTER PROGRAM
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Chapter 4 – Fundamental Data Types
More important details More fun Part 3
Tokens in C Keywords Identifiers Constants
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Fundamentals & Ethics of Information Systems IS 201
University of Gujrat Department of Computer Science
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Data Structures Mohammed Thajeel To the second year students
Java Programming: From Problem Analysis to Program Design, 4e
Ruth Anderson UW CSE 160 Winter 2017
Chapter 2 Bits, Data Types & Operations Integer Representation
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Line Continuation, Output Formatting, and Decision Structures
Arithmetic operations, decisions and looping
Chapter 2 - Introduction to C Programming
And now for something completely different . . .
Fundamentals of Programming I Number Systems
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
Types, Truth, and Expressions (Part 2)
Chapter-3 Operators.
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
Chapter 2 - Introduction to C Programming
Summary Two basic concepts: variables and assignments Basic types:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
SSEA Computer Science: Track A
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Primitives
Introduction to Primitives
Chapter 2 - Introduction to C Programming
Module 2 Variables, Data Types and Arithmetic
Types, Truth, and Expressions
Introduction to C Programming
Types, Truth, and Expressions
Presentation transcript:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu Fall 2017 1

THE ONLINE BOOK CHAPTER VII SELECTIONS

Motivation We will learn how programs can make choices: Boolean variables Boolean expressions if and if/else

Boolean type Two values: True and False Created as Result of a comparison: ==, !=, >, >=, <, <= Conversion using bool(…) function Zero values and anything empty converts into a False Anything else converts into a True value

Examples >>> a = 3 >>> b = 5 >>> p = a < b >>> q = b <= a >>> print("p =", p, "and q =", q) p = True and q = False

A Boolean function def isDivisible(x, y): return x % y == 0 Returns True if x is divisible by y

Boolean operations (I) AND: True if both operands are true This is called a truth table a b a and b F T

Boolean operations (II) OR: True unless both operands are false a b a or b F T

Boolean operations (III) NOT: True if operand is false a not a F T

Boolean operations (IV) Equality/Mutual Implication: True when both operands are equal Represented by a == sign a b a==b F T

Boolean operations (V) Exclusive OR (XOR): True when operands are not equal Represented by a != sign a b a!=b F T

Notes XOR is used for Generating parity bits Hardware hashing

Examples Assuming p is True and q is False >>> p or q True >>> p and q False >>> not p >>> p != q

Online Demo

Precedence Rules Level Category Operators 7(high) exponent ** 6 multiplication *, /, //, % 5 addition +, - 4 relational ==, !=, <=, >=, >, < 3 logical not not 2 logical and and 1(low) logical or or

Examples a < b + c or c > 0 same as (a < (b+c)) or c >0) a and b or c same as (a and b) or c a and not b or c same as (a and (not b)) or c

Conversely We must write a = (p or q) and s b = (c and d) != (e or f) …

Online demo

Binary selection if BOOLEAN EXPRESSION : STATEMENTS_1 # when condition is True else: STATEMENTS_2 # when condition is False Observe The columns after if and else The indentation

Binary selection If condition: something else: other stuff False True other stuff something If condition: something else: other stuff

Flowchart conventions Graphic representation of flow of control Loops represented by loops Ifs have two branches ??? False True

Online demo

Omitting the else clause if BOOLEAN EXPRESSION: STATEMENTS_1 # if condition is True # Continue normal flow Observe The column after if The indentation

Omitting the else clause If condition : something If tank_empty : get_gas True condition something False

Main usage if + else To make a choice between two options if To handle a special case Zero value, negative value Work is done

A problem with letter grades Semester Average Letter Grade ≥ 90 A ≥ 80 but < 90 B ≥ 70 but < 80 C ≥ 60 but < 70 D < 60 F

An ugly solution if average >= 90 : grade = 'A' if average >= 80 and average < 90 : grade = 'B' if average >= 70 and average < 80 : grade = 'C' if average >= 60 and average < 70 : grade = 'D' if average < 60 : grade = 'F'

The right way if average >= 90 : grade = 'A' else : if average >= 80: grade = 'B' else : if average >= 70 : grade = 'C' else : if average >= 60 : grade = 'D' else: grade = 'F'

Using an elif (I) Too many nested ifs else … if cond-1 : … else if cond-2 : … else if cond-3 : … else …

Using an elif (II) With elif, lines align better if cond-1 : … elif cond-2 : … elif cond-3 : … else : …

The right way if average >= 90 : grade = 'A' elif average >= 80: grade = 'B' elif average >= 70 : grade = 'C' elif average >= 60 : grade = 'D' else: grade = 'F'

A beginner's trap if average >= 90 : grade = 'A' elif average >= 80 and average < 90 : grade = 'B' elif average >= 70 and average < 80 : grade = 'C' elif average >= 60 and average < 70: grade = 'D' elif average < 60: grade = 'F'

Useless comparisons if average >= 90 : grade = 'A' elif average >= 80 and average < 90 : grade = 'B' elif average >= 70 and average < 80 : grade = 'C' elif average >= 60 and average < 70: grade = 'D' elif average < 60: # use else instead grade = 'F'

Indenting advice Python attaches great importance to indenting You cannot indent anything that is not inside An if, a while, a for, … A function declaration or a main function Your indentation must be consistent Do not mix spaces and tabs Four spaces is neither too small nor too big

Floating point values Differ from integer values Cannot handle extremely large or extremely small values Limited precision Can manipulate trillions of dollars but results are not exact to the cent Must specify number of decimals in outputs

Limited precision # precision.py """ floating-point computations can be inaccurate """ biggie = 5E9 epsilon = 5E-9 sum = biggie + epsilon delta = sum - biggie print ('epsilon =', epsilon, 'but delta =', delta)

The output epsilon = 5e-9 but delta = 0.0 Why? Computations are carried out with a limited number of significant bits Result has a limited number of significant digits Be careful when computing very small differences between two numbers

Note Limitations on floating point number size and precision are Hardware dependent Shared by most programming languages Unlike Python, most programming languages do not support arbitrary-size integers Python is better

Displaying floating point numbers Easiest way to specify number of decimal places is string interpolation 'Answer is %.nf ' %x does two things It inserts the value of x inside the string It specifies that the string representation of x should have n decimal places

Example (I) pi = 3.1415917 print('pi equals %.1f' % pi ) print('pi equals %.2f' % pi ) print('pi equals %.3f' % pi ) print('pi equals %.4f' % pi )

Example (II) Program prints pi equals 3.1 pi equals 3.14 pi equals 3.142 pi equals 3.1416 Values are nicely rounded