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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 4 LOOPS AND FILES 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. 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 ; 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 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 10 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 11 THE INCREMENT AND DECREMENT OPERATORS Example: When the first...
View Full Document

This note was uploaded on 04/14/2008 for the course CS 1336 taught by Professor Thompson during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 60

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

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online