Shorthand operators.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

Java Planning our Programs Flowcharts Arithmetic Operators.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Lecture 5. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Using the Java programming language compiler. Review of relevant material from previous lectures From previous lectures: A computer can only execute machine.
CSCI S-1 Section 3. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
The character data type char
DAT602 Database Application Development Lecture 5 JAVA Review.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
1 Operators and Expressions Instructor: Mainak Chaudhuri
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
The Assignment operator tMyn1 The Assignment Operator The result of a calculation can be stored in a variable using the assignment operator =. Because.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CompSci 230 S Programming Techniques
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Operators and Expressions
The Boolean (logical) data type boolean
Building Java Programs
Java Intro.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Expressions and Assignment
Building Java Programs
The for-statement.
Assignment Operators Topics Increment and Decrement Operators
Building Java Programs
Presentation transcript:

Shorthand operators

Shorthand operators Shorthand operator: A shorthand operator is a shorter way to express something that is already available in the Java programming language Shorthand operations do not add any feature to the Java programming language (So it's not a big deal).

Shorthand operators +=, -=, *=, /= and *= A frequent construct is the following: Java has a shorthand operator for these kinds of assignment statements x is a variable in the program x = x + value ; // Add value to the variable x x = x - value ; // Subtract value to the variable x x = x * value ; // Increase the variable x by value times and so on...

Shorthand operators +=, -=, *=, /= and *= (cont.) Operator assignment short hands:  Operator symbol   Name of the operator  Example   Equivalent construct  +=   Addition assignment  x += 4;  x = x + 4; -=   Subtraction assignment  x -= 4;  x = x - 4; *=   Multiplication assignment  x *= 4;  x = x * 4;   /=   Division assignment  x /= 4;  x = x / 4; %=   Remainder assignment  x %= 4;  x = x % 4;

Shorthand operators +=, -=, *=, /= and *= (cont.) Exercise: what is printed by the following program public class Shorthand1 { public static void main(String[] args) int x; x = 7; x += 4; System.out.println(x); x -= 4;

Shorthand operators +=, -=, *=, /= and *= (cont.) x = 7; x *= 4; System.out.println(x); x /= 4; x %= 4; }

Shorthand operators +=, -=, *=, /= and *= (cont.) Example Program: (Demo above code) Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Shorthand1.java How to run the program:             Right click on link and save in a scratch directory To compile:   javac Shorthand1.java To run:          java Shorthand1 (You will see the answers on the terminal)

Increment and decrement shorthand operators Two very commonly used assignment statements are: Java has shorthand operators to increment and decrement a variable by 1 (one). 1. x = x + 1; and 2. x = x - 1; x is a variable in the program

Increment and decrement shorthand operators (cont.) Increment and decrement operators:  Operator symbol    Name of the operator  Example   Equivalent construct  ++ Increment  x++; x = x + 1; -- Decrement x--; x = x - 1;

Increment and decrement shorthand operators (cont.) Exercise: what is printed by the following program public class Shorthand2 { public static void main(String[] args) int x; x = 7; x++; System.out.println(x); x--; }

Increment and decrement shorthand operators (cont.) Example Program: (Demo above code) Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Shorthand2.java How to run the program:             Right click on link and save in a scratch directory To compile:   javac Shorthand2.java To run:          java Shorthand2 (You will see the answers on the terminal)