{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Chapter4PPT - Chapter 4 LOOPS AND FILES 1 THE INCREMENT AND...

This preview shows pages 1–12. Sign up to view the full content.

1 Chapter 4 LOOPS AND FILES

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable means to decrease its value by one. To increment a variable a we could write either of the following statements: a = a + 1; a += 1; We could decrement the variable b using one of the following statements: b = b – 1; b – = 1;
3 THE INCREMENT AND DECREMENT OPERATORS These operations are so common in computer applications that Java offers special operators that can be used to specify these operations more concisely. The increment operator is ++, read plus plus, and the decrement operator is --, read minus minus. Both operators are unary operators used to increase/decrease the value of the variable that is their operand by one.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 THE INCREMENT AND DECREMENT OPERATORS So, instead of writing either of the following to increment the value in the variable a : a = a + 1; a += 1; We could write: a++;
5 THE INCREMENT AND DECREMENT OPERATORS So, instead of writing either of the following to decrement the value in the variable b : b = b – 1; b – = 1; We could write: b – –;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 THE INCREMENT AND DECREMENT OPERATORS The operand of the increment or decrement operator must have an lvalue - it must correspond to a location in memory whose contents may be changed. Example: int a = 7, b = 13; a++; // This is valid - we can write a = a + 1 // We are incrementing the value in the variable a // After this statement is executed a has the value 8
7 THE INCREMENT AND DECREMENT OPERATORS Examples: int a = 7, b = 13; 5--; // This is an error, 5 does not have an lvalue // We cannot write 5 = 5 - 1 (a + b)++; // This is an error a + b does not have an lvalue // We cannot write a + b = a + b + 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 THE INCREMENT AND DECREMENT OPERATORS The increment and decrement operators can be used in the postfix mode , which means the operator appears after the variable. When the operator is used in the postfix mode it means that the variable is incremented after it is used in the expression. The increment and decrement operators can be used in the prefix mode , which means the operator appears before the variable. When the operator is used in the prefix mode it means that the variable is incremented before it is used in the expression.
9 THE INCREMENT AND DECREMENT OPERATORS If you use the increment and decrement operators in simple statements that perform only the increment and decrement operation it does not matter if you use the prefix or the postfix mode of the operators. Example: int x = 5, y = 5; x++; ++y; System.out.println(x); // 6 is displayed on a line on the computer screen System.out.println(y); // 6 is displayed on a line on the computer screen

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 THE INCREMENT AND DECREMENT OPERATORS If you use the increment and decrement operators in more complex expressions, the use of the prefix or postfix mode is significant.
11 THE INCREMENT AND DECREMENT OPERATORS Example: When the first println statement of the segment below is executed, x is incremented and then its value (6) is displayed. In the next statement y is displayed (5) and then incremented.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern