Data Types and Expressions

Slides:



Advertisements
Similar presentations
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.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
LESSON 6 – Arithmetic Operators
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CompSci 230 S Programming Techniques
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Relational Operations
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.
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
With Assignment Operator
A First Book of ANSI C Fourth Edition
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions and Assignment
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
Chapter 3 Operators and Expressions
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 Primitive Data Types and Operations
Operator King Saud University
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Presentation transcript:

Data Types and Expressions Module 2 - Part 2 Data Types and Expressions 11/11/2019 CSE 1321 Module 2

Topics Expressions Operator precedence Shorthand operators Data/Type Conversion 11/11/2019 CSE 1321 Module 2

Expressions An expression is a combination of one or more operators and operands It has both a data type and a value that results from the expression evaluation Operands may be: literals constants variables 11/11/2019 CSE 1321 Module 2 3

Arithmetic Operators + addition - subtraction * multiplication Four operators you know, and one new one! + addition - subtraction * multiplication / division % modulus (say what?) 11/11/2019 CSE 1321 Module 2

Integer Division & Modulus When dividing two integer values: the quotient is an integer the remainder is truncated (discarded) To get the remainder, use the modulus operator with the same operands 25 / 3 = 8 vs. 25 % 3 = 1 (integer division) 3 / 25 = 0 vs. 3 % 25 = 3 (integer division) 25.0 / 5 = 5.0 vs. 10 / 3 = 3.33 (floating point division) 11/11/2019 CSE 1321 Module 2 5

Operator Precedence Right-to-left operations Left-to-right operations - unary plus and unary minus (example, +1 or -1) - assignment Left-to-right operations - multiplication, division and modulus - addition and subtraction (example 3 + 3) - string concatenation (“Hello, ” + “world!”) 11/11/2019 CSE 1321 Module 4

Operator Precedence 11/11/2019 CSE 1321 Module 2 7

Variable = expression; Assignment Revisited Variable = expression; 1) the expression on the right side of the = operator is evaluated Expression has data type and value 2) the result is stored in the variable on the left if the types are compatible (overwriting the original value) Note: some of you will lose points for putting the expression on the left and variable on the right 11/11/2019 CSE 1321 Module 2 8

answer = sum / 4 + MAX * lowest; Assignment Example answer = sum / 4 + MAX * lowest; The result is stored in the variable on the left hand side of the assignment operator. 11/11/2019 CSE 1321 Module 2 9

Shortcut Operators ++ increment by 1 -- decrement by 1 Example: count++; // count = count + 1 count--; // count = count - 1 11/11/2019 CSE 1321 Module 2 10

More Shortcut Operators Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10; 11/11/2019 CSE 1321 Module 2 11

Assignment Operators The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore: result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num); 11/11/2019 CSE 1321 Module 2 12

Common Error Trap No spaces allowed between the arithmetic operator and equals sign Note that the correct sequence is +=, not =+ Example: add 2 to a a = +2; // PROBABLY WRONG! assigns +2 to a a += 2; // Probably CORRECT a = a + 2; // Same as a+= 2 11/11/2019 CSE 1321 Module 2 13

Assigning the Values of Other Variables Syntax: dataType variable2 = variable1; Rules: variable1 needs to be defined before this statement appears in the source code the precision of variable1 must be lower than or equal to that of variable2. called “implicit type cast” when this happens 11/11/2019 CSE 1321 Module 2 14

Two Types of Data Conversion Widening conversion (promotion) safe, do not lose data goes to a “wider” (more bits) data type may lose precision (long to float) Narrowing conversion (demotion) may lose data may lose precision should be avoided compiler error unless specific cast done 11/11/2019 CSE 1321 Module 2 15

Conversion Techniques Assignment conversion Example: assign an int to a long Promotion Example: divide an int by a double Casting Example: assign a double to int 11/11/2019 CSE 1321 Module 2 16

Explicit Type Casting Syntax: (dataType)(expression) Note: parentheses around the expression are optional if the expression consists of 1 variable Useful for calculating averages: double result = (double) 25/3; double result = (double) total / count; 11/11/2019 CSE 1321 Module 2 17

Summary An expression is a combination of one or more operators and operands. Expression evaluation follow operator precedent rule (order of operations). Type conversion can be either widening conversion (promotion) or narrowing conversion (demotion). 11/11/2019 CSE 1321 Module 2