The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
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,
1 Chapter 2: Elementary Programming Shahriar Hossain.
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.
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)
Shorthand operators.
The character data type char
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.
LESSON 6 – Arithmetic Operators
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.
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.
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.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
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.
Building java programs, chapter 3 Parameters, Methods and Objects.
Software Technology - I Tools to do operations.....
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.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CompSci 230 S Programming Techniques
Today’s topic: Arithmetic expressions.
Operators and Expressions
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Computing Adjusted Quiz Total Score
OPERATORS (2) CSC 111.
The Boolean (logical) data type boolean
Numerical Data Types.
Java Intro.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
class PrintOnetoTen { public static void main(String args[]) {
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Assignment Operators Topics Increment and Decrement Operators
The for-statement.
Assignment Operators Topics Increment and Decrement Operators
Review of Previous Lesson
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

The ++ and -- expressions

The ++ and -- operators You guessed it: The ++ and -- are operators that return a value

The ++ and – operators (cont.) Pre- and post operators: There are two versions of the ++ and the -- operators: Pre-operation: the ++ and -- operator appear before the variable ++var pre-increment operator --var pre-decrement operator

The ++ and – operators (cont.) Pre-operation: the ++ and -- operator appear before the variable var++ post-increment operator var-- post-decrement operator

The ++ and – operators (cont.) Difference between a ++var and var++: The above also apply for --var and var--. (Except that that instead of incrementing the variable var by 1, the -- operators will decrement by 1) Both operations (++var and var++) will increment the variable var by 1 (so no different here) The only difference between ++var and var++ is: the value that is returned by the expression.

The result returned by the assignment operators Result returned by the ++ and -- operators: Value in aOperation Value in a after operation Returned value 4 ++a55 4 a a33 4 a--34

The result returned by the assignment operators (cont.) In other words: The pre-operations ++a and --a will: Apply the increment/decrement operation before (pre) returning the value in the variable a

The result returned by the assignment operators (cont.) The post-operations a++ and a-- will: Apply the increment/decrement operation after (pre) returning the value in the variable a (The computer will saved the original value of the variable a, perform the operation and then returned the saved value)

The result returned by the assignment operators (cont.) Example: public class Increment01 { public static void main(String[] args) { int a; a = 4; System.out.println(++a); // Prints 5 System.out.println(a); // Prints 5 a = 4; System.out.println(a++); // Prints 4 System.out.println(a); // Prints 5 a = 4; System.out.println(--a); // Prints 3 System.out.println(a); // Prints 3 a = 4; System.out.println(a--); // Prints 4 System.out.println(a); // Prints 3 }

The result returned by the assignment operators (cont.) Example Program: (Demo above code) –Prog file: ncrement01.java How to run the program: Right click on link and save in a scratch directory To compile: javac Increment01.java To run: java Increment01

Priority of the ++ and -- operators Priority of the ++ and -- operators: The ++ and -- operators have higher priority than any arithmetic operators in Java

Priority of the ++ and – operators (cont.) Example: public class AssignExpr05 { public static void main(String[] args) { int a, b; a = 4; b = ++a + 1; // ++a evaluates to 5, so: = 6 System.out.println(a); // Prints 5 System.out.println(b); // Prints 6 a = 4; b = a++ + 1; // a++ evaluates to 4, so: = 5 System.out.println(a); // Prints 5 System.out.println(b); // Prints 5 }

Priority of the ++ and – operators (cont.) Explanation: b = ++a + 1; is evaluated as follows: b = ++a + 1; higher priority ^^^ ++a evaluates to 5 Reduces to: b = 5 + 1; = 6; b = a++ + 1; is evaluated as follows: b = a++ + 1; higher priority ^^^ ++a evaluates to 4 Reduces to: b = 4 + 1; = 5;

Priority of the ++ and – operators (cont.) Example Program: (Try it out yourself) –Prog file: AssignExpr05.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr05.java To run: java AssignExpr05

Exercise What will the following Java program print: public class AssignExpr06 { public static void main(String[] args) { int a, b; a = 4; b = 2 * --a + 1; System.out.println(a); System.out.println(b); a = 4; b = 2 * (a-- + 1); System.out.println(a); System.out.println(b); }

Exercise (cont.) Answer:

Exercise (cont.) Example Program: (Verify for yourself…) –Prog file: AssignExpr06.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr06.java To run: java AssignExpr06

Associativity of the ++ and -- operators The following expressions are syntactically incorrect: a or a Error: unexpected type required: variable found : value a ++ --; ^ Why it is syntactically incorrect: a ===> (a ++) needs to operate on a variable ^^^^^^ is a value

Associativity of the ++ and – operators (cont.) Because we don't encounter these constructs, I will omit the discussion of the associativity rules of the ++ and -- operators. I can refer you to a website with the information: