Unit 1.3: Expressions and Output¶
Scope: CS Awesome 2, Section 1.3
Learning Goals¶
By the end of this lesson, you should be able to:
- Produce exact output with
printandprintln. - Use literals and escape sequences.
- Evaluate integer and floating-point expressions.
- Apply operator precedence.
- Use division and remainder correctly.
Output Statements¶
System.out.print("Loading ");
System.out.println("complete");
System.out.println("Next task");
Output:
Loading complete
Next task
print stays on the current line. println prints its value and then moves to a new line. Spaces inside a string literal are part of the output.
Literals and Escape Sequences¶
A literal is the code representation of a fixed value.
42 // int literal
3.25 // double literal
true // boolean literal
"station" // String literal
An escape sequence begins with a backslash and represents a character that is difficult to type directly inside a string.
| Escape sequence | Meaning |
|---|---|
\n |
new line |
\" |
double quote |
\\ |
backslash |
System.out.println("Line 1\nLine 2");
System.out.println("She said, \"Go.\"");
System.out.println("C:\\data\\report.txt");
Arithmetic Expressions¶
An expression combines values, variables, and operators and evaluates to one value.
| Operator | Meaning | Example |
|---|---|---|
+ |
addition | 7 + 2 → 9 |
- |
subtraction | 7 - 2 → 5 |
* |
multiplication | 7 * 2 → 14 |
/ |
division | depends on operand types |
% |
remainder | 7 % 2 → 1 |
Integer and Double Arithmetic¶
When both operands are int, the result is an int.
System.out.println(7 / 2); // 3
Integer division discards the fractional part. When at least one operand is double, the result is a double.
System.out.println(7.0 / 2); // 3.5
System.out.println(7 / 2.0); // 3.5
Integer division by zero throws an ArithmeticException at run time. Floating-point division follows different rules, but it is still usually a sign that the input or algorithm should be checked.
Compound Expressions and Precedence¶
Java groups arithmetic operators in this order:
- parentheses;
- multiplication, division, and remainder;
- addition and subtraction.
Operators at the same level are evaluated from left to right.
System.out.println(4 + 3 * 2); // 10
System.out.println((4 + 3) * 2); // 14
System.out.println(20 / 5 * 2); // 8
System.out.println(20 / (5 * 2)); // 2
Use parentheses to make the intended grouping explicit.
The Remainder Operator¶
a % b gives the remainder after dividing a by b.
int minutes = 137;
System.out.println(minutes / 60); // 2 whole hours
System.out.println(minutes % 60); // 17 remaining minutes
Common uses include checking divisibility, extracting digits, and converting units.
int number = 583;
int lastDigit = number % 10; // 3
Practice Missions¶
Mission 1: Exact Output¶
Write output statements that produce exactly:
Path: C:\missions\alpha
Message: "Proceed"
Status:
READY
Use at least two escape-sequence types.
Mission 2: Expression Trace¶
Predict the value and type of each expression without running it:
19 / 4
19.0 / 4
2 + 3 * 5
(2 + 3) * 5
29 % 6
18 / 4 * 2.0
Mission 3: Time Converter¶
Given int totalSeconds = 3672;, calculate and print the whole number of minutes and the remaining seconds. Use / and %.
Mission 4: Fix the Formula¶
The intended result is the average of three integer scores as a decimal. Explain why this expression loses information, then rewrite it:
(score1 + score2 + score3) / 3
Key Summary¶
| Concept | Core idea |
|---|---|
print |
Prints without adding a new line. |
println |
Prints and then starts a new line. |
| Literal | A fixed value written directly in code. |
| Escape sequence | Special character notation beginning with \. |
| Integer division | Discards the fractional part. |
| Double arithmetic | Occurs when at least one operand is double. |
% |
Produces the division remainder. |
| Precedence | Determines how operators are grouped. |
Source scope: CS Awesome 2, Unit 1.3