Lecture 6(6)
Lecture 6(6)
Lecture 6: Expressions
John Hennessy
President, Stanford University
• Knows conditional
statements
• Knows loops
• Has senses
• Has memory
(variables)
• Doesn’t have vision
yet
Letterboxes in the US
• A flag on the side of US mailbox is meant
to be used by the owner and a mail delivery
person: It is meant to signal that something
is inside that needs picking up by either a
mail delivery person or the mailbox owner.
• Boolean variables are used in a much the
same way: as flags that must first be set and
can be further tested to make right
decisions
Clara Postie
boolean b = true;
boolean p = b;
Flags
• A flag is a boolean variable that monitors some
condition in a program.
• When a condition is true, the flag is set to true.
• The flag can be tested to see if the condition has
changed.
if (average >= 85)
highScore = true;
• Later, this condition can be tested:
if (highScore)
System.out.println("That′s a high score!");
Clara Postie
Console Output
• Use System.out.println() to print a String
value and change to new line.
Console Output
• Use System.out.print() to print a String
value without changing to new line.
Arithmetic Operators
Operator Meaning Type Example
Arithmetic Operators
• The operators are called binary operators because
they must have two operands.
• Each operator must have a left and right operator.
• The arithmetic operators work as one would
expect in algebra (with small exceptions).
• It is an error to try to divide any number by zero.
• When working with two integer operands, the
division operator requires special attention.
Subtraction
• n1 – n2
• Works similar to mathematics
• Can also represent unary minus
• -n2 + n1
Remainder
• Only works with integer values
• Returns a remainder of division between the two
operands
2 R 1
7%2= 1
2|5
7 % 20 = 7
Division
• All other operators work the way you would
expect
• Division works slightly differently depending on
the type of the arguments you divide
• Division of real numbers is simple:
Integer Division
• Division can be tricky.
In a Java program, what is the value of 1/2?
• You might think the answer is 0.5…
• But, that’s wrong.
• The answer is simply 0.
• Integer division will truncate any decimal remainder.
int x = 5;
double y = x / 2;
Variable casting
• To deal with this problem we must employ type
casting on one of the operands in the right hand side
int x = 5;
double y = (double) x / 2;
• You can also do the opposite:
The result is ?
double y = 2.5;
z = 2;
int z = (int) y;
Operator Precedence
• Mathematical expressions can be very complex.
• There is a set order in which arithmetic operations will
be carried out.
1 2
4
Dr. Anton Bogdanovych, School of Computing, Engineering and Mathematics
Programming Fundamentals (300580)
The + Operator
• The + operator can be used in two ways.
– as a concatenation operator
– as an addition operator
• If either side of the + operator is a string,
the result will be a string.
System.out.println("Hello " + "World");
System.out.println("The value is: " + 5);
System.out.println("The value is: " + value);
System.out.println("The value is: " + ‘/n’ + 5);
String Concatenation
• Java commands that have string literals must be
treated with care.
• A string literal value cannot span lines in a Java
source code file.
System.out.println("This line is too long and now
it has spanned more than one line, which will
cause a syntax error to be generated by the
compiler. ");
String Concatenation
• The String concatenation operator can be used to fix
this problem.
System.out.println("These lines are " +
"are now ok and will not " +
"cause the error as before.");
• String concatenation can join various data types.
System.out.println("We can join a string to " +
"a number like this: " + 5);
String Concatenation
• The Concatenation operator can be used
to format complex String objects.
System.out.println("The following will be printed "
+
"in a tabbed format: " +
\n\tFirst = " + 5 * 6 + ", " +
"\n\tSecond = " (6 + 4) + "," +
"\n\tThird = " + 16.7 + ".");
Example: Average
num1+num2
average =
2
Mid-Semester Test
Mid-Semester Test
Question Examples
• What’s wrong with the program below?
Question Examples
• What’s wrong with the program below?
Question Examples
a) 17
b) 13.5
c) 13
d) 22.5
Question Examples
a) 17
b) 13.5
c) 13
d) 22.5
Question Examples
a) 0 1 2 3 4 5 6 7 8 9
b) 1 2 3 4 5 6 7 8 9 10
c) Syntax error
d) 9
Question Examples
a) 0 1 2 3 4 5 6 7 8 9
b) 1 2 3 4 5 6 7 8 9 10
c) Syntax error
d) 9
Question Examples
int i = 0;
{
for (;i < 10; i ++)
System.out.print(i + " ");
System.out.print( (i+1) + " ");
}
a) 0 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8 9 11
c) Syntax error
d) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
Question Examples
int i = 0;
{
for (;i < 10; i ++)
System.out.print(i + " ");
System.out.print( (i+1) + " ");
}
a) 0 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8 9 11
c) Syntax error
d) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
Question Examples
Question Examples
Textbook