CS50 Lecture 0 – Comprehensive Notes
Course Intro & Professor’s Personal Story
- David J. Malan (a former CS50 student himself, now Senior Lecturer) opens Lecture 0 in Sanders Theatre.
- Lived in Matthews Hall freshman year, once Matt Damon’s room.
- Initially majored in Government; feared CS50’s reputation.
- Sophomore year, followed friends to CS50 (then taught by Brian Kernighan) and “found his people.”
- Moral: Your starting comfort level with computers does not dictate your final trajectory.
Course Ethos & Structure
- Majority of students have never taken computer science before.
- Grading philosophy: measure you “relative to yourself,” not classmates.
- Support system: Teaching Fellows, Course Assistants, Malan himself.
- Key Traditions
- CS50 Puzzle Day (logic-puzzle social kickoff with pizza & prizes).
- CS50 Hackathon (overnight coding sprint for final projects).
- CS50 Fair (campus-wide expo; demo something we didn’t explicitly teach you—proof you can learn on your own).
- “I took CS50” T-shirt.
What Is Computer Science?
- Study of information: representation & processing.
- Emphasis on computational thinking → applying CS ideas to fields from arts to natural sciences.
- Ultimate abstraction: Problem → (processing/algorithm) → Solution.
- “Input ➔ Black box ➔ Output” mental model.
Binary & Numeric Systems
- Unary (base-1): count with fingers 1,2,3,4,5.
- Binary (base-2): each finger/light bulb has 2 states (off/on).
- One hand can show 25−1=31 different numbers (0–31).
- Demo with 3 light bulbs: 000<em>2=0</em>10,111<em>2=7</em>10.
- Columns carry weights 20,21,22,… analogous to decimal’s 100,101,102.
- Nomenclature: binary digit → bit. 8 bits → byte.
- Highest with one byte: 28−1=255 (values 0–255 ⇒ 256 possibilities).
ASCII & Unicode
- ASCII: 7/8-bit standard mapping; e.g.,
- 6510 → ‘A’, 66 → ‘B’, 72 → ‘H’, 73 → ‘I’, 33 → ‘!’.
- Limit: 256 characters ≠ enough for world languages.
- Unicode: extended (8-,16-,24-,32-bit) superset; goal: every human symbol + emoji.
- Example: “😂” (face with tears of joy) = 403699110610 (32-bit code point).
Colors, Images, Video, Sound
- RGB: 3 bytes per pixel (red, green, blue).
- Example pixel [72,73,33] ≈ medium-yellow.
- Images = grids of pixels ⇒ megabytes.
- Video = ~24–30 images/second ⇒ gigabytes (motion picture illusion).
- Sound: store pitch, volume, duration (and instrument) as numbers; MIDI-like.
Algorithms & Problem Solving
Phone-Book Example (Searching for “John Harvard”)
- Linear search: page 1 → n (worst-case n).
- Jump 2 pages at a time + back 1 (≈n/2).
- Binary Search: open middle, discard half each time (worst-case log2n ≈ 10 steps in 1,000-page book).
Big-O Visualization
- Linear: O(n) (red).
- Half-linear: O(n/2) (yellow).
- Logarithmic: O(logn) (green; flattest).
Pseudocode Building Blocks
- Functions (actions/verbs) e.g., pickUpPhone().
- Conditionals: if / else if / else.
- Boolean expressions (yes/no): touching? equal? < ?
- Loops: go back to / repeat / while / forever.
- Indentation conveys structure; beware infinite loops → must progress.
AI & Large Language Models
- Hand-coding every chatbot answer is infeasible → train on data.
- Neural networks: layers of “neurons,” probabilities decide output.
- CS50’s mascot: a giant rubber duck (debugging tradition: explain problem aloud).
- cs50.ai: course-specific LLM assistant (allowed; ChatGPT & generic models disallowed).
- IDE: Cloud version of Visual Studio Code (vscode.dev + CS50 add-ons).
- Built-in Duck assistant and other course plug-ins.
Programming Basics with Scratch
Environment Overview
- Palette of colored blocks (Motion blue, Looks purple, Sound pink, Control orange, Events yellow, etc.).
- Sprites = movable characters (default: orange cat).
- Stage (x,y): center (0,0), corners x=±240,y=±180.
- Example: ‘say “Hello World”’: input = text; side effect = speech bubble.
- Mapping to paradigm:
- Input: “Hello World”
- Algorithm: say block
- Output: visible bubble.
Variables & Return Values
- ask “name?” + wait → returns
answer (stored value). join concatenates "Hello, " + answer.
Loops, Conditionals, Events
- repeat n, forever.
- if then …
- Event blocks: when green-flag-clicked, when sprite clicked, when key pressed, when video motion > x, etc.
Custom Blocks (User-Defined Functions)
- ‘make a block’: define meow n times with parameter
n. - Reuse without duplicating code → easier maintenance.
Practical Demos
- Hello, World! speech bubble.
- Ask → Say personalized greeting.
- Meowing cat:
- Sound block in loop; added wait; replaced with custom block
meow n times.
- Event-driven “pet the cat”:
- Forever → if touching mouse → meow.
- Video sensing: webcam motion triggers meow (demonstrated live).
Building Larger Projects in Scratch
Incremental Development Philosophy
- Build feature 0 → test → add feature 1, etc. Avoid overwhelming complexity.
Example 1: “Oscar Time” (Malan’s childhood-song game)
- Sprites: Oscar, trash can lid, multiple trash objects, lamppost background.
- Mechanics
- Trash set to random x, start y = 180, draggable, falls y=y−1 per frame.
- If touching Oscar → score++
- Variables:
score displayed on stage. - Sound layer: Sesame-Street “I Love Trash.”
- Added sprites & complexity gradually: +1 trash, +score, +music, +lid animation.
- Levels escalate: walls, Yale sprite bouncing horizontally, MIT sprite chasing player, random Princeton dangers.
- Techniques used:
- Custom blocks:
listenForKeyboard, feelForWalls. - Harvard sprite
- If arrow key pressed → change x/y by ±1.
- If touching wall → negate movement.
- Yale sprite
- Start centered, forever move, if wall touch → turn 180∘.
- MIT sprite
- Forever point toward Harvard, move step size
s (difficulty via s). - Level progression via touching goal sprite.
- Don’t Repeat Yourself (DRY): avoid copy-pasting; abstract with loops & functions.
- Modularity: hide “implementation details” under custom blocks; simplifies main script.
Key Takeaways & Mindset
- Computers understand only 0 and 1; layers of abstraction let us think in letters, images, music, and games.
- Algorithms matter: choosing O(logn) over O(n) can be life-changing at scale.
- Programming primitives (functions, variables, conditionals, loops) exist in every language; syntax differs.
- Approach large projects incrementally; test early and often.
- Embrace debugging: explain problems to a rubber duck (or cs50.ai) and identify your own mistakes.
- CS50’s goal: by term’s end, remove the “training wheels” so you can teach yourself any new tech.