Download presentation
Presentation is loading. Please wait.
Published byΜιλτιάδης Αλεξάκης Modified over 5 years ago
1
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS Copyright: Illinois Institute of Technology- George Koutsogiannakis
2
NEW TOPICS Shortcut Operators.
3
Incrementing Numbers A common operation in programming is to create a counter that starts from a specific value and keeps incrementing. For instance: int count=0; count=count+1; In this case we ask the machine to go to memory location count, get the value stored, increment it by one and store it back in the same memory location called count
4
Incrementing Numbers Therefore count has the number 1 now stored in it ( 0+1=1). What if we repeated the statement again? count=count+1; What value is stored in count now? The value stored in count is 2 now (1+1=1)
5
Incrementing Numbers- Using Shortcut Operator ++
Another way of accomplishing the same result is to use the shortcut operator ++ (double plus symbol) Thus programming count=count+1; is the same as programming count++; Notice that there is no need to assign it back to its identifier explicitly. It will be incremented and assigned back to its identifier automatically.
6
Shortcut Operator -- We can decrement a number by using:
count=count-1; or by using count--; In both cases we asking: Go to memory location count and retrieve the number stored there. Then, subtract 1 from that number and store the result back in the same memory location .
7
Shortcut Operators What if we wanted to increment (or decrement) a number in steps higher than 1? count=0; count=count+3; is the same as count+=3;
8
Shortcut Operators count=0; count=count-3; is the same as count-=3;
Similar operators exist for multiplication, division and modulus i.e. What is the meaning of a*=4;
9
Shortcut Operators Each of the shortcut operators can have a postfix and prefix form. The examples we did thus far used the postfix form. In prefix form the shortcut operator precedes the identifier: ++count instead of count++ --count instead of count--
10
Prefix versus Postfix It only makes a difference if the operators are used in expression. i.e. int a= 6; System.out.println(“The value is”+a) The output is: The value is 6 System.out.println(“The value is”+ ++a); The output is: The value is 7 System.out.println(“The value is”+a++); System.out.println(“The value is”+a); The output is: The value is 8
11
Prefix versus Postfix In step 1 obviously the current value in memory location a is outputted (6) In step 2 (prefix for shortcut operator) the current value 6 is retrieved and incremented to value 7 and saved in location a, then it is outputted. In step 3 the current value 7 is retrieved from memory location and outputted. Then, the value is incremented to 8 and saved in memory location a. In step 4 the current value stored in a (that is 8) is outputted.
12
Prefix versus Postfix- Example applies to all shortcut operators
Using ++a or a++ by itself to increment will result in the same outcome either way! Suppose we start with: int a=0 Initial value in memory location a Next we increment either with ++a or a++ Next value in memory location a Memory a 1 Memory a
13
Prefix versus Postfix- Example applies to all shortcut operators
If the shortcut operator is part of another operation then prefix versus postfix makes a difference!! Suppose we have int a=1; int b=0; int c=0; c= a+ (++b); The outcome is: c= 2, b=1 If, however, we had the statement c= a+ (b++); The outcome is: c=1, b=1 Why?
14
Binary versus Unary Operators
When an operator operates on two operands it is called a binary operator. i.e. Consider the addition operator + x + y first operand is x second operand is y When an operator operates on a single operand it is called a unary operator i.e Shortcut operators are unary operators because they operate on a single operand ++x for example.
15
Study Guide Text Chapter 2 Section 2.4.7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.