COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
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.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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 =
Operators and Expressions
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
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
LESSON 6 – Arithmetic Operators
CSC Programming I Lecture 5 August 30, 2002.
Chapter 2: Using Data.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 2 Variables.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Mathematical Calculations in Java Mrs. C. Furman.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSE 220 – C Programming Expressions.
Chapter 12 Variables and Operators
Side Effect Operators Changing the value of variables
Nahla Abuel-ola / WIT.
Multiple variables can be created in one declaration
Chapter 12 Variables and Operators
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Assignment
Data Types and Expressions
In this class, we will cover:
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating Expressions –Order of Operations –Data Type Conversion Lecture 3

Java Statements (section 2.2) Java program comprised of the following –Source Code Analogous with a document such as Word –Blocks of statements Analogous with a paragraph of a document –Statements Analogous with a sentence within a paragraph –Keywords, variables, constants and function calls Analogous with the words in a sentence

Java Statements (section 2.2) 4 types of statements in a Java program –Null statement Performs no action ; // Has no affect on the program –Expression statement Performs one of the following actions –Declare a variable int count; –Assign a value count = 0; –Call a function count = getCount(); –Return statement Returns a value from a function –Value returned to the calling statement return 0;

Java Statements (section 2.2) 4 types of statements in a Java program –Compound statements A collection of multiple statements –On a single line int count = 0; count = 2 + 1; displayCount(count); –1 statement “nested” within another statement displayCount(getCount()); –Block of statements that complete a specific task »Such as a the statements within a method public static double calcArea(int length, int width) { double area; area = length * width; return area; } A semicolon “;” indicates the end of most statements –Curly braces “{ }” indicate the start and end of a block of statements

Java Expressions (section 2.4) Expressions consists of 2 items –1 or more operands Variables Constants Function calls –1 or more operators i.e., =, +, -, *, /, %, ( and ) The “=“ operator is referred to as the assignment operator –It is used to “assign” a value to a variable Examples: int ans = * 5 / 1 – 4; // ans = 3 int ans = (2 + 1) * 5 / (1 – 4); // ans = -5

Java Expressions (section 2.4) Compound assignments –A common operation is to update the value of a variable Examples int count = 0; count = count + 10; // count = 10 count = count - 5; // count = 5 count = count * 3; // count = 15 count = count / 4; // count = 3 count = count % 2; // count = 1 (determines remainder) –Java shorthand expressions for the above examples Examples int count = 0; count += 10; // count = 10 count -= 5; // count = 5 count *= 3; // count = 15 count /= 4; // count = 3 count %= 2; // count = 1

Postfix Expressions (section 2.4) A postfix expression –Is similar to the “+=“ and “-=“ compound assignment operators Limited to increasing/decreasing value by 1 Examples int count = 0; count = count + 1; count += 1; count++; The postfix expression occurs in 2 steps (when used in an expression) –Evaluates the current value of the variable –Increases/decreases the value of the variable by 1 Examples of use int ans = 0, count = 0; ans = 5 + count++; // ans = 5, count = 1 count++; // count = 2 System.out.println(“The current count is” + count); // Prints 2 All 3 are identical

Prefix Expressions (section 2.4) A prefix expression –Is similar to the “+=“ and “-=“ compound assignment operators Limited to increasing/decreasing value by 1 Examples int count = 0; count = count + 1; count += 1; ++count; The prefix expression occurs in 2 steps (when used in an expression) –Increases/decreases the value of the variable by 1 –Evaluates the new value of the variable Examples of use int ans = 0, count = 0; ans = ++count + 5; // ans = 6, count = 1 ++count; // count = 2 System.out.println(“The current count is” + count); // Prints 2 All 3 are identical

Basic Math Functions (section 3.5) Includes additional functions –Beyond the basic math operators (+,-,*,/,etc.) –Example functions Ceiling – returns next highest whole number Math.ceil(3.14); // 4 Floor – returns next lowest whole number Math.floor(3.14); // 3 Round – rounds value to closest whole number Math.round(3.14); // 3 Max – returns the larger of two values int a = 3, b = 5; Math.max(a, b); // 5 Min – returns the smaller of two values int a = 3, b = 5; Math.min(a, b); // 3

Evaluating Expressions (section 2.4) Rules for evaluating expressions int ans = * 2; –Only 1 variable allowed on the left side of an expression 1 or more variables, constants and functions allowed on the right side –The right side of the expression must be reduced to a single value Once reduced, it can be assigned to a variable –Evaluation follows “Order of Precedence” rules Similar to the precedence rules used in math courses Parentheses can be used to override the precedence rules int ans = * 2; // ans = 14 int ans = (4 + 5) * 2; // ans = 18

Order of Operations (appendix D) Order of operations chart OperatorDescription Example Variable Constant Parentheses count 3.14 (a + b) [], f(x), ++, --Array, Function, Postfix ary[i], doIt(x, y), a++, a-- ++, --, !, &, * Prefix, Not, Address Pointer Reference ++a, --a, !a, &a, *a ()Casting (int)a *, /, % Multiply, divide, modulus a * b, a / b, a% b +, -Add, subtract a + b, a - b =, >Comparison a = b, a > b ==, !=Equal, not equal a == b, a != b &&Logical and a && b ||Logical or a || b =, +=, -=, *=, /=, %=Assignment a = b, a += b, a -= b, a *= b, a /= b, a %= b

Data Type Conversion (section 2.5) Data type conversion –Deals with converting one data type to another Also referred to as “casting” The contents of box C will completely fit in boxes A or B The contents of box B will completely fit in box A, but not box C The contents of box A will not completely fit in boxes B or C A A B B C C

Data Type Conversion (section 3.5) Data type conversion