Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Types and Arithmetic Operators
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.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
Chapter 2: Using Data.
Chapter 2: Basic Elements of C++
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
JavaScript, Third Edition
Chapter 2: Basic Elements of C++
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Chapter 2 Variables.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
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.
Test Review Computer Science History
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
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 Primitive Data Types and Operations
Presentation transcript:

Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined using operators ► Arithmetic expressions manipulate numeric variables following the rules of algebra and have numeric values.  Integral expressions manipulate integer values  Floating point or decimal expressions manipulate floating point numbers ► Relational expressions compare numerical values and have logical (boolean) values ► Logical expressions combine boolean variables or expressions and yield boolean values

Expressions ► An expression is a series of variables combined using unary and/or binary operations ► An algebraic expression has a numeric value, a relational or logical expression has a boolean value ► Example  X*Y is an algebraic expression  If the variables X and Y have different types then they must be converted to a common type before the expression X*Y is evaluated  The type of the value of the entire expression is the same as the common type the variables are converted to

Rules for implicit conversion ► The value of the expression in an assignment statement may be converted to the type of the resultvariable ► The value of one of the operands of a binary operation may be converted before the operation is performed ► Some conversions are done implicitly. These conversions are the widening conversions that always have valid results  byte to int  float to double  short to int  int to long  int to float

Explicit conversion: The cast operation ► In Java you can explicitly convert the type of a variable or expression within a larger expression using a cast operator  The value of the variable or expression is not changed  The value used in the larger expression is converted to the requested type ► Sample expressions including casts  (int)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2

Integral Expressions ► All operands are integers ► Examples: * x – y/7 x + 2 * (y – z) + 18

Floating-point Expressions ► All operands are floating-point numbers ► Examples: 12.8 * 17.5 – x * y

Mixed Expressions ► Operands of different types ► Examples: / ► Integer operands yield an integer result; floating-point numbers yield floating-point results ► If both types of operands are present, the result is a floating-point number ► Precedence rules are followed

Initialization ► Memory locations associated with defined constants must be initialized when the constants are defined ► Memory locations associated with variables may be initialized anywhere in the program  Initialization may occur in the declaration statement  Initialization may be done after declaration using an assignment statement  Initialization may also be done by ‘reading’ the value ► Memory locations associated with variables should have their values defined before they are used.  It is good programming practice to initialize variables at the start of your program

Values and Memory Allocation for Integral Data Types

The class String ► Used to manipulate strings ► String  Sequence of zero or more characters  Enclosed in double quotation marks  Null or empty strings have no characters  Numeric strings consist of integers or decimal numbers  Length is the number of characters in a string

Strings and the Operator + ► Operator + can be used to concatenate two strings or a string and a numeric value or character ► Example:

Parsing Numeric Strings ► ► String to int Integer.parseInt(strExpression) ► ► String to float Float.parseFloat(strExpression) ► ► String to double Double.parseDouble(strExpression) *strExpression: expression containing a numeric string

Input ► Standard input stream object: System.in ► Input numeric data to program  Separate by blanks, lines, or tabs ► To read a line of characters: 1. Create an input stream object of the BufferedReader 1. Create an input stream object of the class BufferedReader 2. Use the method readLine

Input ► Standard input stream object: System.in ► To read a line of characters: Create an input stream object of the BufferedReader ► To read a line of characters: Create an input stream object of the class BufferedReader InputStreamReader charReader = new InputStreamReader(System.in); BufferedReader keyboard = new BufferedReader(charReader): new BufferedReader(charReader): ► Read the data into a variable of type string inputStr = keyboard.readLine(); ► Parse the string

Output ► Standard output object: System.out ► Methods  print  println  flush ► Syntax System.out.print(stringExp);System.out.println(stringExp);System.out.flush();

Commonly used escape sequences