Lecture 4 Ed
Lecture 4 Ed
1
What is the output of the following statements?
}
2
Escape Sequences
➢ What if we wanted to print a double quote character?
➢ The following line would confuse the compiler
because it would interpret the second quote as the
end of the string
System.out.println ("I said "Hello" to you.");
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash
4
Write a program to display the following.
**
****
******
******
****
**
Write a program to display the following.
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
8 / 12 equals? 0
14 % 3 equals? 2
8 % 12 equals? 8
10
Operator Precedence
➢ Operators can be combined into complex
expressions
result = total + count / max - offset;
order
Operator Precedence
➢ What is the order of evaluation in the following
expressions?
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
12
Assignment Revisited
➢ The assignment operator has a lower precedence
than the arithmetic operators
13
Assignment Revisited
➢ The right and left hand sides of an assignment
statement can contain the same variable
count = count + 1;
14
Augmented Assignment Operators
➢ Example
double a = 6.5;
a += a + 1;
//a=a+(a+1)
Increment and Decrement Operators
24
Data Conversions
➢ Casting is the most powerful, and dangerous,
technique for conversion
• Both widening and narrowing conversions can be
accomplished by explicitly casting a value
• To cast, the type is put in parentheses in front of the value
being converted
25
Selection statements
➢ The program can decide which statements to execute
based on a condition.
27
Selection statements
➢ Java provides six relational operators (also known as
comparison operators).
28
if Statements
➢ An if statement is a construct that enables a program
to specify alternative paths of execution.
➢ Java has several types of selection statements: one-
way if statements, two-way if-else statements, nested
if statements, multi-way if-else statements, switch
statements, and conditional expressions.
➢ A one-way if statement executes an action if and only
if the condition is true.
➢ The syntax for a one-way if statement is:
if (boolean-expression)
{
statement(s);
} 29
Two-Way if-else Statements
➢ An if-else statement decides the execution path
based on whether the condition is true or false.
➢ A one-way if statement performs an action if the
specified condition is true. If the condition is false,
nothing is done.
➢ But what if you want to take alternative actions when
the condition is false?
➢ You can use a two-way if-else statement. The actions
that a two-way if-else statement specifies differ
based on whether the condition is true or false.
30
Two-Way if-else Statements
➢ syntax for a two-way if-else statement:
if (boolean-expression)
{
statement(s)-for-the-true-case;
}
else
{
statement(s)-for-the-false-case;
}
31
Two-Way if-else Statements
➢ Write a program that checks if a number is odd or
even.
32