learning objectives learn about variables, their types and their values learn about different number representations

Similar documents
learning objectives learn about variables, their types and their values learn about different number representations

iteration & recursion

Type Conversion. and. Statements

CSI33 Data Structures

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CSC Discrete Math I, Spring Sets

Getting Started with Python

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Computer Organization and Levels of Abstraction

COS 140: Foundations of Computer Science

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Text Input and Conditionals

Fundamentals of Programming (Python) Getting Started with Programming

CS558 Programming Languages

CS558 Programming Languages

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

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Bitwise Operator and Typecasting

COMP-520 GoLite Tutorial

Computer Organization and Levels of Abstraction

COS 140: Foundations of Computer Science

TYPES, VALUES AND DECLARATIONS

cs1114 REVIEW of details test closed laptop period

Computer Science II Lecture 1 Introduction and Background

Lecture #23: Conversion and Type Inference

1.3b Type Conversion

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS558 Programming Languages

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Bitwise Data Manipulation. Bitwise operations More on integers

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Control Structures 1 / 17

Introduction to Programming (Java) 2/12

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CSCI2467: Systems Programming Concepts

CSCI 1061U Programming Workshop 2. C++ Basics

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CS558 Programming Languages

Numerical Representations On The Computer: Negative And Rational Numbers

Introduction to Swift. Dr. Sarah Abraham

Scala : an LLVM-targeted Scala compiler

COS 320. Compiling Techniques

C - Basics, Bitwise Operator. Zhaoguo Wang

Number Representation & Conversion

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

Programming for Engineers Iteration

Discrete Mathematics Lecture 4. Harper Langston New York University

Built-in Types of Data

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Numerical Representations On The Computer: Negative And Rational Numbers

Computer System and programming in C

Representing and Manipulating Integers Part I

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Onion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)

Introduction to Programming Using Java (98-388)

ENGR 101 Engineering Design Workshop

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

More Programming Constructs -- Introduction

Conditional Expressions and Decision Statements

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Lecture (04) Boolean Algebra and Logic Gates

Lecture (04) Boolean Algebra and Logic Gates By: Dr. Ahmed ElShafee

4. Number Representations

Pace University. Fundamental Concepts of CS121 1

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

The type of all data used in a C (or C++) program must be specified

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

Coral Programming Language Reference Manual

Chapter 4. Operations on Data

FRAC: Language Reference Manual

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

egrapher Language Reference Manual

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

C++ Modern and Lucid C++ for Professional Programmers

Bits and Bytes January 13, 2005

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS558 Programming Languages

Pierce Ch. 3, 8, 11, 15. Type Systems

CS558 Programming Languages

Language Reference Manual simplicity

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

1.00/1.001 Tutorial 1

Transcription:

programming basics

learning objectives algorithms your software system software hardware learn about variables, their types and their values learn about different number representations learn boolean algebra and conditional branching learn about basic text input and output

what s a variable? in a program, a variable is a symbolic name (also called identifier) associated with a memory location where the value of the variable will be stored wallet Memory 0000 10110110 age schmilblick reference 0001 0010 0011 00010010 10110110 00000001

yes but what type of value? x n + y n = z n 00100101 00101011 00010010 10100100 11001101 00111001 11110011 01010011 1111001101010011 0010010100101011 1100110100111001 1111001101010011 6 & # n. œ. œ. œ. œ # œ >. >?# n œ œ œ œœ œ œ œ œ œ œ œ œ œ œ œ œ œ œ œ # œ œ œ œ n œ œ œ cresc. œ œ œ œ œ nœ œ œ œ œ œ œ œ # œ œ œ œ fij œ œ œ œ œ œ 00100101001010110001001010100100 11001101001110011111001101010011

what s a type? the type of a variable defines what will be stored in the memory location, e.g., a boolean, an integer, a character, etc., i.e., how the bits in the memory location will be interpreted d = 3.14 i = 0 s = "hello" python scala swift var d = 3.14 var i = 0 var s = "hello" var d = 3.14 var i = 0 var s = "hello" 00111110001000000000000000000000 0.15625 1000001 65 1000001 A 0000000 false

explicit typing & type inference as a programmer, you can explicitly define the type of a variable (explicit typing) or let the compiler (or the interpreter) try to infer the type of the variable, typically through initialization (implicit typing) however, there are cases where type inference is not possible, e.g., in recursive functions python scala swift implicit i = 0 f = 3.14 s = "hello" var i = 0 var d = 3.14 var f = 3.14f var s = "hello" var i = 0 var d = 3.14 var s = "hello" explicit no static typing var i : Int = 0 var f : Double = 3.14 var f : Float = 3.14f var s : String = "hello" var i : Int = 0 var f : Double = 3.14 var f : Float = 3.14 var s : String = "hello"

static typing vs dynamic typing the static type designates the type of the variable known at compilation time this allows the compiler to catch a certain number of errors before the execution scala var i : Int = 0 var d = 3.14 var f = 3.14f var s = hello f : Float = d i = d s = d the dynamic type designates the type of the value contained by a variable at run time this allows the runtime to catch errors during the execution python v = 0 v = 3.14 v = hello

type casting when you want to assign a value to a variable but the static type and the dynamic type do not match, you can perform an explicit conversion, also known as a type casting python scala swift d = math.pi i = int(d) f = float(d) s = str(d) 3.141592653589793 3 "3.141592653589793" var d : Double = math.pi var i = 0 var s = "hello" var f = 3.14f f = d.tofloat i = d.toint s = d.tostring 3.1415927 var d : Double.pi var i = 0 var s = "hello" var f : Float = 3.14 var i = Int(d) var f = Float(d) var s = String(d)

number representation unsigned integers bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 8710 = 0 2 7 + 1 2 6 + 0 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 1 2 0 8710 = 0 128 + 1 64 + 0 32 + 1 16 + 0 8 + 1 4 + 1 2 + 1 1 8710 = 0 1 0 1 0 1 1 1 8710 = 010101112 range = [02,111111112] = [010,25510] signed integers with signed magnitude Bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 8710 = 0 1 2 6 + 0 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 1 2 0 8710 = 0 1 0 1 0 1 1 1-8710 = 1 1 64 + 0 32 + 1 16 + 0 8 + 1 4 + 1 2 + 1 1-8710 = 1 1 0 1 0 1 1 1 8710 = 010101112-8710 = 110101112 Bit 7 is the sign bit 0 + 1 - range = [-12710,+12710] two ways to represent zero: +010 = 000000002-010 = 100000002

number representation signed integers with one complement Bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 8710 = 0 1 2 6 + 0 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 1 2 0 8710 = 0 1 0 1 0 1 1 1-8710 = 1 0 1 0 1 0 0 0 8710 = 010101112-8710 = 101010002 Bit 7 is the sign bit 0 + 1 - range = [-12710,+12710] two ways to represent zero: +010 = 000000002 +010 = 111111112

number representation signed integers with two complement Bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 8710 = 0 1 2 6 + 0 2 5 + 1 2 4 + 0 2 3 + 1 2 2 + 1 2 1 + 1 2 0 8710 = 0 1 0 1 0 1 1 1 1 0 1 0 1 0 0 0-8710 = 1 0 1 0 1 0 0 1-1 2 7 0 2 6 + 1 2 5 + 0 2 4 + 1 2 3 + 0 2 2 + 0 2 1 + 1 2 0-8710 = -1 128 + 1 32 + 1 8 + 1 1 +1 8710 = 010101112-8710 = 101010012 Bit 7 is the sign bit 0 + 1 - range = [-12810,+12710] only one way to represent zero: 010 = 000000002

number representation only a small subset of the infinite set of real numbers can be represented in a computer, which has a finite memory space floating point principle 3.14159 = sign mantissa base exponent 1 314159 10 5 in a computer, the base is 2

number representation floating point single precision sign exponent (8 bits) mantissa (23 bits) 0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 sign 2 2 e 2 3

constant a constant is simply a variable that cannot... vary python scala swift no constant val d : Double = math.pi val i = 0 val s = "hello" d = 1.0 i = 1 s = "bye" let d : Double.pi let i = 0 let s = "hello" d = 1.0 i = 1 s = "bye"

logic the intellectual tool for reasoning about the truth and falsity of statements

logic & programming most programming languages, support boolean variables, which can take values {true,false} in some low-level languages, integer numbers are used for the same purpose, e.g,. with: p = false p = 0 q = true q = 1 (sometimes q = true q 0 ) when combined with operators, and, boolean variables constitute an algebra used in conditional branching where: not or and

boolean algebra assume that p, q and r are boolean variables (or statements) and that T = true, F = false, we have: p p p p q p q p q p q F T T F F F F F T F T F F T T T F F F F T T T F T T T T not or and python scala swift a = False b = True c = a and b c = a or b c = not a var a = false var b = true var c = a && b c = a b c =!a var a = false var b = true var c = a && b c = a b c =!a

some rules Associative Rules: (p q) r p (q r) (p q) r p (q r) Distributive Rules: p (q r) (p q) (p r) p (q r) (p q) (p r) Idempotent Rules: p p p p p p Double Negation: p p p DeMorgan s Rules: (p (p q) p p q q (p (p q) p p q q Commutative Rules: p q q p p q q p Absorption Rules: p (p q) p p (p q) p Bound Rules: p 0F 0F p T1 p p T1 1T p 0F pp Negation Rules: p ( p) ( p) 0F p ( p) ( p) T1

from boolean algebra to conditional branching example write a function that checks whether a given year (passed as parameter) is a leap year or not

Leap years are multiples of 4, and they can only be multiples of 100 if they are also multiples of 400

function isleap(year : integer) if year mod 400 = 0 isleap true else if year mod 100 = 0 isleap false else if year mod 4 = 0 isleap true else isleap false conditional branching function isleap(year : integer) if ((year mod 4=0) (year mod 100 =0)) (year mod 400) isleap true else isleap false function isleap(year : integer) isleapyear ((year mod 4=0) (year mod 100 =0)) (year mod 400)

conditional branching python def isleap(year): if year % 400 == 0 : return True elif year % 100 == 0 : return False elif year % 4 == 0 : return True return False def isleap(year): if (year % 4 == 0) and (year % 100!= 0) or (year % 400 == 0) : return True return False def isleap(year): return (year % 4 == 0) and (year % 100!= 0) or (year % 400 == 0)

conditional branching scala def isleap(year : Int) : Boolean = { if (year % 400 == 0) true else if (year % 100 == 0) false else if (year % 4 == 0) true else false } def isleap(year : Int) : Boolean = { if ((year % 4 == 0) && (year % 100!= 0) (year % 400 == 0)) true else false } def isleap(year : Int) : Boolean = ( year % 4 == 0) && (year % 100!= 0) (year % 400 == 0)

conditional branching swift func isleap(year:int) -> Bool { if year % 400 == 0 { return true } else if year % 100 == 0 { return false } else if year % 4 == 0 { return true } else { return false } } func isleap(year:int) -> Bool { if (year % 4 == 0) && (year % 100!= 0) (year % 400 == 0) { return true } else { return false } } func isleap(year:int) -> Bool { return (year % 4 == 0) && (year % 100!= 0) (year % 400 == 0) }

text input/output on the command line when a program is launched on the command line, it can ask the user for text input and provide text output on the terminal python input year = input("give us a year: ) year = int(year) output print("is {0} a leap year? {1}".format(year, isleap(year))) scala import scala.io.stdin.readline val year = readline("choose a year: ").toint println(s"is $year a leap year? ${isleap(year)}") swift var year = Int(readLine()!) print("year \(year!) is leap: \(isleap(year:year!))")

function calls import scala.io.stdin.readline breakpoint here def isleap(year : Int) : Boolean = (year % 4 == 0) && (year % 100!= 0) (year % 400 == 0) def reportleapyear(year : Int) = { if (isleap(year)) println(s"$year is leap") else println(s"$year is not leap") } year val input = readline("choose a year: ") val year = input.toint reportleapyear(year) the call stack contains a stack frame for each function call currently active a stack frame contains all the local variables and parameters of the function being called year 2000 year 2000 input 2000 true "2000" stack frame of isleap stack frame of reportleapyear stack frame of main program returned value for the caller return address in the caller