CS50 Intro to Programming
π» CS50: Introduction to Computer Science
This course, CS50, is Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. It aims to provide a general-purpose way of thinking and solving problems, applicable across various fields like arts, humanities, social sciences, natural sciences, and STEM.
The course is often described as "drinking from a fire hose" due to the volume of information. Students are encouraged to get comfortable feeling uncomfortable, as this is expected in an introductory class where much of the material will be new.
π Course Overview & Goals
CS50 focuses on teaching students how to program, not just in a specific language, but by building a foundational understanding that allows them to teach themselves new languages in the future.
π Programming Languages Explored
Unlike older versions of the course that focused solely on C, CS50 now covers a range of languages to provide a broader understanding of programming paradigms and applications:
C: An older, foundational language.
Python: A modern language widely used for data science, web applications, and more.
SQL (Structured Query Language): Specifically designed for databases.
JavaScript: Often used with HTML and CSS for web-based and mobile applications.
π― Key Learning Objectives
Problem Solving: The core of computer science.
Representation of Information: Understanding how computers store various types of data.
Algorithms: Developing step-by-step instructions for solving problems.
Code Implementation: Translating algorithms into computer code.
π₯ CS50 Community & Support
CS50 is characterized by its strong community and culture, offering extensive support and engagement opportunities.
π€ Support Structure
Problem Sets: Homework assignments with different tracks for varying comfort levels.
Sections/Recitations: Smaller group sessions, including tracks for those least comfortable with CS or STEM.
Support Staff: Teaching Fellows (TFs), Teaching Assistants (TAs), and Course Assistants (CAs) provide guidance.
π Evaluation Philosophy
The course emphasizes personal growth over relative performance. What ultimately matters is where you end up relative to yourself when you began, rather than relative to your classmates.
π Social & Academic Events
CS50 Lunches: Weekly Chinese lunch gatherings with staff, alumni, and industry friends.
CS50 Puzzle Day: An opportunity for teams to solve logical puzzles, regardless of prior CS experience, with pizza and prizes.
CS50 Hackathon: A 12-hour event where students bond with classmates and work on their final projects.
CS50 Fair: An exhibition where students showcase their final projects to the wider university community, complete with music, industry friends, and candy.
"I Took CS50" T-shirts: A proud tradition for students who complete the course.
π§ Computational Thinking & Problem Solving
Computer science is fundamentally the study of information and, more specifically, about solving problems using certain ideas and techniques. This approach is called computational thinking.
π‘ What is Computational Thinking?
Computational thinking is about thinking like a computer β applying methodical and careful approaches to problems from any field. It helps to clean up thought processes and express ideas more precisely.
βοΈ The Problem-Solving Model
A problem to be solved can be generalized as:
Input: The problem itself.
Output: The solution to the problem.
Algorithm/Code: The "secret sauce" in the middle that converts inputs to outputs.
π’ Representing Information: Binary System
Before solving problems, we must agree on how to represent inputs and outputs. Computers primarily use a binary system (0s and 1s).
β Unary vs. Binary
Unary Notation: Counting where each digit (e.g., finger) represents one unit. Limited in capacity (e.g., 5 fingers = 5 counts).
Binary Notation (Base-2): Uses only two digits, 0 and 1. This maps naturally to electrical states (on/off, electricity present/not present).
A single binary digit is called a bit.
Transistors are tiny electrical switches inside computers that represent these 0s and 1s (on/off states).
π Understanding Binary Values
Similar to the decimal system (Base-10), where digit positions represent powers of 10 (ones, tens, hundreds), binary uses powers of 2:
Decimal: 123=(1Γ102)+(2Γ101)+(3Γ100)
Binary:
0002β=(0Γ22)+(0Γ21)+(0Γ20)=010β
0012β=(0Γ22)+(0Γ21)+(1Γ20)=110β
0102β=(0Γ22)+(1Γ21)+(0Γ20)=210β
0112β=(0Γ22)+(1Γ21)+(1Γ20)=310β
1002β=(1Γ22)+(0Γ21)+(0Γ20)=410β
π Bytes: A Useful Unit of Measure
A byte is a collection of eight bits.
With eight bits, 28=256 possible values can be represented (from 0 to 255).
For example, a byte of all 1s (111111112β) represents the decimal number 255.
βοΈ Representing Letters: ASCII & Unicode
Computers use numbers to represent letters and other characters.
π‘ ASCII (American Standard Code for Information Interchange)
A system that assigns a unique number to each English letter, number, and punctuation mark.
For example, the capital letter 'A' is represented by the number 65.
7 bits (or typically 8 bits) are used, allowing for 256 possible characters, which is sufficient for English.
Character | ASCII Decimal Value | Binary (8-bit) |
|---|---|---|
A | 65 | 01000001 |
B | 66 | 01000010 |
H | 72 | 01001000 |
I | 73 | 01001001 |
! | 33 | 00100001 |
Example: The binary sequence
01001000 01001001 00100001translates to72 73 33in decimal, which spells "Hi!"
π Unicode
A superset of ASCII that addresses the limitation of 256 characters for non-English languages and symbols.
Uses more bits per character: 16, 24, or even 32 bits.
With 32 bits, it can represent up to 4 billion possible characters, accommodating diverse languages and emoji.
Mission: To represent and preserve all human languages digitally, past, present, and future.
Emoji: Although they look like pictures, emoji are technically just characters in the Unicode alphabet, each represented by a specific pattern of 0s and 1s.
Customization: Skin tones for emoji are handled by using the same base character number and adding additional bits to specify the desired tone.
Combinatorics: Some emoji, like "couple with heart," are combinations of multiple Unicode characters joined by zero-width joiners (ZWNJ), special numbers that "glue" emoji together.
Context Matters: The same pattern of 0s and 1s can represent different things (numbers, letters, colors) depending on the context in which they are interpreted by the computer. As programmers, we tell the computer how to interpret these bits.
π¨ Representing Colors, Images, Audio, and Video
π Colors (RGB)
Colors are commonly represented using RGB (Red, Green, Blue) values.
Each pixel (dot on a screen) has three numbers associated with it, one for the intensity of red, one for green, and one for blue.
Each color component (red, green, or blue) is typically represented by one byte (8 bits), allowing values from 0 to 255.
Combining these three values creates every color in the spectrum.
Example: 72 (red), 73 (green), 33 (blue) combine to form a yellowish-brown color.
πΌοΈ Images
Images are composed of many pixels, each storing its RGB color information.
A 3-megabyte photograph might contain 1 million pixels (3 bytes per pixel).
πΆ Audio
Music can be represented digitally by assigning numbers to various aspects:
Pitch/Note: A number for each musical note.
Duration: How long the note is played.
Loudness: The intensity of the note.
π¬ Video
Videos are essentially a rapid sequence of still images (frames).
For example, 30 frames per second (FPS) means 30 distinct pictures are displayed per second, creating the illusion of continuous motion.
βοΈ Algorithms: Step-by-Step Instructions
An algorithm is a set of step-by-step instructions for solving a problem. Code is the implementation of an algorithm in a computer language.
π Phone Book Search Analogy
Consider searching for a name in a physical phone book:
Linear Search (Page by Page):
Algorithm: Start at the beginning, turn one page at a time, check each name.
Correctness: Yes, it will eventually find the name (or confirm its absence).
Efficiency: Very slow. For
npages, it takes up tonsteps. (e.g., 1,000 pages = 1,000 steps).
Linear Search (Two Pages at a Time):
Algorithm: Turn two pages at a time, but if you pass the target, double back one page.
Correctness: Yes, if implemented carefully.
Efficiency: Faster than one page at a time (roughly
n/2 + 1steps), but still linear.
Binary Search (Divide and Conquer):
Algorithm:
Open to the middle of the phone book.
Check the page.
If the name is there, stop.
If the name is alphabetically earlier, discard the right half of the book.
If the name is alphabetically later, discard the left half of the book.
Repeat steps 1-5 with the remaining half.
Correctness**: Yes. Efficiency**: Dramatically faster. For 1,000 pages, it takes roughly 10 steps (because 210β1000). This is a logarithmic relationship.
π Efficiency of Algorithms
Size of Problem (x-axis) vs. Time to Solve (y-axis)
Linear algorithms (like the first two) result in a straight line on the graph; as the problem size increases, the time increases proportionally.
Logarithmic algorithms (like binary search) result in a curve that flattens out; even if the problem size doubles, the time to solve it increases only slightly (e.g., by one more step).
Goal in CS50: Not just to write correct code, but to design efficient algorithms.
Accuracy vs. Efficiency: A graph showing algorithm efficiency does not capture its accuracy. An algorithm could be very fast but incorrect. Correctness is a separate, fundamental requirement.
π Pseudocode: Expressing Algorithms
Pseudocode is an informal, terse, and precise way to describe an algorithm in human language (e.g., English), without adhering to any specific programming language syntax.
π Building Blocks of Algorithms (and Pseudocode)
Functions (Actions/Verbs): Specific tasks to perform (e.g.,
Pick up phone book,Look at page).Conditionals (Forks in the Road): Decisions based on conditions (e.g.,
If person is on page,Else if person is earlier).These use Boolean expressions (questions with a yes/no, true/false, or 1/0 answer).
Loops (Cycles): Repeating a set of instructions (e.g.,
Go back to line 3).
1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If person is on page:
5. Call person
6. Else if person is earlier in book:
7. Open to middle of left half of book
8. Go back to line 3
9. Else if person is later in book:
10. Open to middle of right half of book
11. Go back to line 3
12. Else:
13. Quit (person not found)π€ Artificial Intelligence (AI) & Chatbots
AI, particularly large language models (LLMs) like ChatGPT, builds upon these foundational programming concepts but requires more advanced techniques.
π¦ CS50 Duck: AI-Based Tutor
CS50 will utilize its own AI-based software, the CS50 Duck, as a tutor.
The goal is to provide a helpful, non-judgmental, and patient tutor that guides students to answers rather than simply providing them.
This tool aims to offer the right balance between assistance and fostering independent learning.
π§ How LLMs Work (Conceptually)
Instead of explicitly programming every possible input and output (as in the simple chatbot example), LLMs are trained on vast amounts of human language data (text from the internet, books, etc.).
They infer patterns and learn to predict what a human might say in response to a question, often probabilistically.
While powerful, LLMs can "hallucinate" (make mistakes) or produce similar outputs if not designed with some randomness.
π» Introduction to Programming: Scratch
Before diving into text-based languages, CS50 uses Scratch, a graphical programming language, to visualize programming concepts.
π "Hello, World" in Scratch
Scratch uses puzzle pieces (blocks) that are dragged and dropped to create programs.
The classic "Hello, World" program in Scratch involves:
when green flag clicked: An event block that starts the program.say "hello, world": A function block that displays text.
π¨ Scratch User Interface
Blocks Palette: Left side, categorized by color (Motion, Looks, Sound, Events, Control, Operators, Variables, My Blocks, Extensions).
Script Area: Middle, where blocks are dragged and connected.
Stage: Right side, where the program runs, featuring a sprite (e.g., Scratch Cat).
Green Flag/Red Stop Sign: Control program execution.
πΊοΈ Scratch's Coordinate System
The stage is a Cartesian plane with x and y coordinates.
(0, 0) is the center.
x-axis: -240 (left) to 240 (right).
y-axis: -180 (bottom) to 180 (top).
π§© Key Programming Concepts in Scratch
π£οΈ Functions with Parameters
say "hello": A function."hello": A parameter or argument that customizes the function's behavior.ask "What's your name?" and wait: An ask function that prompts for input and returns a value (the typed answer).answer: The return value from theaskfunction.join "hello " answer: An operator function that combines strings.
π Loops
repeat (10): Repeats actions a specific number of times.forever: Repeats actions indefinitely.Loops help avoid repetition of code, making programs more efficient and easier to modify.
β Conditionals
if (condition) then { ... }: Executes code only if a Boolean expression (condition) is true.if (condition) then { ... } else { ... }: Executes one block of code if the condition is true, and another if it's false.Example:
if (touching mouse pointer?) then { play sound "meow" }
π§± Creating Custom Blocks (Functions)
"My Blocks": Allows users to create their own named functions.
This promotes modularity and abstraction.
Modularity: Breaking a program into smaller, manageable pieces.
Abstraction: Hiding complex implementation details, allowing users to focus on what a function does rather than how it does it.
Example: Creating a
meow (n) timesblock that encapsulates theplay sound "meow"andwait (1) secondsinside arepeat (n)loop.
πΉοΈ Game Development Example: Whac-A-Mole
Simple games can be created using these building blocks.
Video Sensing extension: Allows programs to react to motion detected by the camera.
Variables: Used to keep track of game state, like scores or time remaining.
πΎ Building Complex Programs Incrementally
Complex programs, like the "Oscartime" game, are built by taking baby steps and creating versions.
Start with a simple foundation, then add features incrementally.
Example: First, the stage. Then, trash falling. Then, trash disappearing into the can. Then, Oscar popping out.
Randomness: Incorporating random elements (e.g., trash falling from different positions) makes games more engaging.
Multiple Sprites: Each character or object in a game can be its own sprite, with its own set of code blocks. This allows for parallel execution of different parts of the game.
π‘οΈ Example: Harvard Shield Game
Interactive movement: Using arrow keys to move a sprite.
if (key [up arrow] pressed?) then { change y by (1) }
Collision detection: Using
if (touching [wall]?)to react to boundaries.Abstraction in games: Creating custom blocks like
listen for keyboardandfeel for wallsto organize code.Bugs: Increasing movement speed too much can cause a sprite to "jump" over a wall without registering a collision, revealing a bug.
π Conclusion
CS50 provides a comprehensive introduction to computer science, emphasizing problem-solving, computational thinking, and the art of programming. By understanding how information is represented and how algorithms are constructed using fundamental building blocks like functions, conditionals, and loops, students gain the ability to approach and solve a wide array of problems, not just in technology, but in many other fields. The course's use of Scratch provides a visual and accessible entry point, preparing students for more complex languages like C, Python, SQL, and JavaScript, and ultimately empowering them to teach themselves new technologies and build their own innovative solutions.

Ask anything
We've read this whole study set, so questions about it get the best answers.