05 Expressions
05 Expressions
05 Expressions
Eric Roberts
CS 106A
January 13, 2010
Once upon a time . . .
Holism vs. Reductionism
In his Pulitzer-prizewinning book,
computer scientist Douglas Hofstadter
identifies two concepts—holism and
reductionism—that turn out to be
important as you begin to learn about
programming.
Hofstadter explains these concepts using
a dialogue in the style of Lewis Carroll:
Achilles: I will be glad to indulge both of you, if you will first oblige me, by telling
me the meaning of these strange expressions, “holism” and
“reductionism”.
Crab: Holism is the most natural thing in the world to grasp. It’s simply the
belief that “the whole is greater than the sum of its parts”. No one in his
right mind could reject holism.
Anteater: Reductionism is the most natural thing in the world to grasp. It’s 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
The Add2Integers Program
class Add2Integers extends ConsoleProgram {
public void run() {
println("This program adds two numbers.");
int n1 = readInt("Enter n1: ");
int n2 = readInt("Enter n2: ");
int total = n1 + n2;
println("The total is " + total + ".");
}
} n1 n2 total
17
17 25
25 42
Add2Integers
double c = 100;
double f = 9 / 5 * c + 32;
9 / 5 * c + 32
The Pitfalls of Integer Division
You can fix this problem by converting the fraction to a double,
either by inserting decimal points or by using a type cast:
double c = 100;
double f = (double) 9 / 5 * c + 32;
1.8
9.0
(double) 9 / 5 * c + 32
The Remainder Operator
• The only arithmetic operator that has no direct mathematical
counterpart is %, which applies only to integer operands and
computes the remainder when the first divided by the second:
14 % 5 returns 4
14 % 7 returns 0
7 % 14 returns 7
+ -
lowest
42
32
0 32
0 4
3 30 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;
x += 1;
The
As isinformation
true for allabout the number
idiomatic patternsofinrepetitions
this book,isthe
specified by
italicized
the firstindicate
words line in the
thepattern,
parts ofwhich is calledyou
the pattern the need
header
to line.
change for
each application. To use this pattern, for example, you need to
The statements to be repeated are called the body of the for
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
curly statement that repeats a section of code is called a loop.
braces.
Each execution of the body of a loop is called a cycle.
The AddNIntegers Program
The for
This body
program
loop
of the
inuses
this
loopthe
example
consists
Repeat-N-Times
works
of twocorrectly
statements.
idiom
onlytoThe
ifcompute
the
first
variable
reads
the
an
sum integer
totalof is
a predetermined
initialized
from the userto 0 into
before
number
theexecuting
variable
of integer
thevalues,
valueloop.
, andspecified
the secondby
addsnamed
the that value
constant
to the
N.variable total.
while (true) {
prompt user and read in a value
if (value == sentinel) break;
rest of loop body
}
AddIntegerList