{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
1 Chapter 4 LOOPS AND FILES
Image of page 1

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

View Full Document Right Arrow Icon
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;
Image of page 2
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.
Image of page 3

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

View Full Document Right Arrow Icon
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++;
Image of page 4
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 – –;
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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.
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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.
Image of page 10
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.
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern