Chapter 2 (pp. 17–64) of Computer Science I.
Topics
| Section | Page | Content |
|---|---|---|
| Control Flow | 17 | Flowcharts; sequence of execution. |
| Variables | 18 | Naming rules/conventions, types, dynamic vs static typing, scoping. |
| Operators | 33 | Assignment, numerical, string concatenation, precedence, common numerical errors. |
| Basic Input/Output | 41 | Standard I/O, GUIs, printf()-style formatting, command-line input. |
| Debugging | 46 | Types of errors (syntax, runtime, logic); strategies. |
| Examples | 50 | Temperature conversion; quadratic roots. |
Key Ideas
- Static vs dynamic typing introduced here — contrasted concretely in the language parts (C static, PHP dynamic).
- Scoping governs variable lifetime/visibility.
- Common numerical errors (overflow, integer division, float precision) flagged early; revisited in searching & sorting (arithmetic overflow in midpoint calc).
Recurring Examples
Temperature conversion and quadratic roots — reappear in every language part.
Examples
Declaring and using variables
integer a ← 7
integer b ← 3
integer sum ← a + b // 10
print sum
Integer division pitfall
print 7 / 2 // 3, not 3.5 — integer division truncates
print 7.0 / 2 // 3.5 — force a real operand
A classic numerical error: watch operand types.
In Java
int a = 7, b = 3;
int sum = a + b; // 10
System.out.println(7 / 2); // 3 — integer division truncates
System.out.println(7.0 / 2); // 3.5 — one real operand promotes the result
Java is statically typed: the declared type fixes the operator’s behavior.
Citations
[1] Computer Science I, Ch. 2, pp. 17–64.