Expressions: Eric Roberts CS 106A April 11, 2012
Expressions: Eric Roberts CS 106A April 11, 2012
Anteater: Reductionism is the most natural thing in the world to grasp. Its simply the belief that a whole can be understood completely if you understand its parts, and the nature of their sum. No one in her left brain could reject reductionism.
Expressions
This program adds two numbers. Enter n1: 17 Enter n2: 25 The total is 42.
Expressions in Java
The heart of the Add2Integers program from Chapter 2 is the line
int total = n1 + n2;
The n1 + n2 that appears to the right of the equal sign is an example of an expression, which specifies the operations involved in the computation.
An expression in Java consists of terms joined together by operators. Each term must be one of the following:
A constant (such as 3.14159265 or "hello, world") A variable name (such as n1, n2, or total) A method call that returns a value (such as readInt) An expression enclosed in parentheses
This type is used to represent integers, which are whole numbers such as 17 or 53.
double
This type is used to represent numbers that include a decimal fraction, such as 3.14159265.
char
total minus sign, as in 0, 42, -1, or 1000000. Floating-point constants include a decimal point, as in 3.14159265 or (contains an int) 42 10.0. Floating-point constants can also be expressed in scientific notation by has the following attributes: Each variableadding the letter E and an exponent after the digits of the number, so that 5.646E-8 represents the number 5.646 x 10-8. A name, which enables you to differentiate one variable from another. The two constants of type boolean are true and false. A type, which specifies what type of value the variable can contain. Character and string constants are discussed in detail in Chapter 8. A value, which represents the current contentsaof the variable. consists For the moment, all you need to know is that string constant of a sequence of characters enclosed in double quotation marks, such The name and type of a variable are fixed. The value changes as "hello, world".
Variable Declarations
In Java, you must declare a variable before you can use it. The declaration establishes the name and type of the variable and, in most cases, specifies the initial value as well. The most common form of a variable declaration is
type name = value;
where type is the name of a Java primitive type or class, name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value. Most declarations appear as statements in the body of a method definition. Variables declared in this way are called local variables and are accessible only inside that method.
Variables may also be declared as part of a class. These are called instance variables and are covered in Chapter 6.
seems as if it should have the value 2.8, but because both operands are of type int, Java computes an integer result by throwing away the fractional part. The result is therefore 2. If you want to obtain the mathematically correct result, you need to convert at least one operand to a double, as in
(double) 14 / 5
The conversion is accomplished by means of a type cast, which consists of a type name in parentheses.
1.8
9.0 (double) 9 / 5 * c + 32
4 0 7
The result of the % operator make intuitive sense only if both operands are positive. The examples in the book do not depend on knowing how % works with negative numbers. The remainder operator turns out to be useful in a surprising number of programming applications and is well worth a bit of study.
Precedence
If an expression contains more than one operator, Java uses precedence rules to determine the order of evaluation. The arithmetic operators have the following relative precedence:
unary * /
(type cast)
%
highest
lowest
Thus, Java evaluates unary - operators and type casts first, then the operators *, /, and %, and then the operators + and -.
Precedence applies only when two operands compete for the same operator. If the operators are independent, Java evaluates expressions from left to right. Parentheses may be used to change the order of operations.
32
32
0
3 30
4
8
( 1 + 2 ) % 3 * 4 + 5 * 6 / 7 * ( 8 % 9 ) + 10
Assignment Statements
You can change the value of a variable in your program by using an assignment statement, which has the general form:
variable = expression;
The effect of an assignment statement is to compute the value of the expression on the right side of the equal sign and assign that value to the variable that appears on the left. Thus, the assignment statement
total = total + value;
adds together the current values of the variables total and value and then stores that sum back in the variable total. When you assign a new value to a variable, the old value of that variable is lost.
Shorthand Assignments
Statements such as
total = total + value;
where op is any of Javas binary operators. The effect of this statement is the same as
variable = variable op (expression);
The effect of this statement is to add one to the value of x, which means that this statement is equivalent to
x += 1;
The -- operator (which is called the decrement operator) is similar but subtracts one instead of adding one.
The ++ and -- operators are more complicated than shown here, but it makes sense to defer the details until Chapter 11.
Extending Add2Integers
The next few slides extend the Add2Integers program from Chapter 2 to create programs that add longer lists of integers. These slides illustrate three different strategies:
Adding new code to process each input value Repeating the input cycle a predetermined number of times Repeating the input cycle until the user enters a sentinel value
This strategy, however, is difficult to generalize and would clearly be cumbersome if you needed to add 100 values.
The information patternatic patterns repetitions is specified by As is true for allabout the number of in this book, the italicized the first line in the parts which is called the header change for words indicate the pattern,of the pattern you need to line. each statements to To use this pattern, for example, you the for The application. be repeated are called the body of need to replace repetitions with an expression giving the number of statement and are indented with respect to the header line. repetitions and include the statements to be repeated inside the A control statement that repeats a section of code is called a loop. curly braces. Each execution of the body of a loop is called a cycle.
You should choose a sentinel value that is not likely to occur in the input data. It also makes sense to define the sentinel as a named constant to make the sentinel value easy to change.
This program adds a list of integers. Enter values, one per line, using 0 to signal the end of the list. ? 1 ? 2 ? 3 ? 0 The total is 6.
skip simulation
The End