Types and Arithmetic Operators

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Python November 14, Unit 7. Python Hello world, in class.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 2 Variables.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
COMP Primitive and Class Types Yi Hong May 14, 2015.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
BASIC ELEMENTS OF A COMPUTER PROGRAM
2.5 Another Java Application: Adding Integers
Chapter 7: Expressions and Assignment Statements
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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic Operator Operation Example + addition x + y
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to C++ Programming
Numerical Data Types.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
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.
Chapter 2: Java Fundamentals
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Types and Arithmetic Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1

Data Types Each variable has specific type numeric types (int and float) strings boolean (True or False) Define capabilities of variable Numeric types manipulated with arithmetic operators Strings manipulated with string functions Booleans used in branching statements

Dynamic Data Typing Based on current value stored in variable x = 3  x is an integer x = “Fred”  x is now a string Other languages (C, Java, etc.) require type to be set when variable declared and not later changed Good idea to not change type in middle of program (confusing!) Can use type function to see current type of variable

Numeric Data Types Two different numeric types integer (whole numbers) floating point (numbers with decimals) Based on type of literal assigned to variable No decimal  integer Decimal  float Even if 0 after decimal 2.0 is float

Numeric Data Types and Memory Numeric values must be stored in binary memory No limit to size of integers in Python Other languages may limit to fixed storage size(64 bits, etc.) Problem: Some numbers may require infinite number of digits to store Example: 1/3 = 0.3333333333333333333333… Cannot store with complete accuracy on computer!

Float Type and Memory Python allocates limited number of digits for floats Usually about 16 digits 1/3 = 0.3333333333333333 in Python Affects accuracy of computation 5 * 2/3 ≠ 2/3 * 5 Difference between integer and float numbers integer arithmetic guaranteed to be accurate float arithmetic not guaranteed

Floats and Exponential Notation Exponential notation: digits e exponent Equivalent to digits × 10exponent Used to store arbitrarily large/small floats with limited number of digits 0.0000000000000000000000000000123  1.23e-30 Why called “floating point” numbers

Arithmetic Operators Basic mathematical operators: + addition - subtraction Also have unary minus -number * multiplication / division ** exponentiation // quotient (like division but truncates result to integer) % remainder (what is left over after quotient) Example: x = 23 // 5  4 y = 23 % 5  3

Arithmetic Operators and Types Result type based on operand type integer op integer  integer float op float  float float op integer or integer op float  float Exception: division integer / integer  float Even if the operands are divisible! If need to get a whole number, use // instead

Explicit Type Conversion Can use type(value) to evaluate value as type x = float(3)  3.0 y = int(3.52)  3 Used to manually truncate to integer Not same as rounding! Can convert strings to/from equivalent numbers s = str(3.52)  ‘3.52’ x = float(“3.52”)  3.52 y = int(“3”)  3

Parsing Input to Strings input function always returns a string Even if user inputs a number Must convert to number before manipulating with arithmetic operators Otherwise get runtime error Usual form: Prompt for value (stored in string) Use int or float to get correspond numeric value Manipulate numeric value

Example: Celsius to Fahrenheit

Order of Evaluation x = 7 + 3 * 2 Rules of operator precedence: Is this 20? Is this 13? Depends on order of evaluation of operators Rules of operator precedence: Exponentiation done first Unary minus done next Multiplicative operators (*, /, %, //) done next Additive operators (+, -) done last Operators at same level done left to right

Parentheses and Precedence Can use parentheses to change order Expression in parentheses evaluated before use in rest of expression x = (7 + 3) * 2  20

Incremental Operators Many statements of form variable = variable op value Variable changed in terms of current value Shortcut syntax: variable op= value Examples: x += 1 same as x = x + 1 x *= 2 same as x = x * 2 …