1. Home
  2. Explore
  3. Computer Science
  4. CS50 Intro to Programming

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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)123 = (1 \times 10^2) + (2 \times 10^1) + (3 \times 10^0)123=(1Γ—102)+(2Γ—101)+(3Γ—100)

  • Binary:

    • 0002=(0Γ—22)+(0Γ—21)+(0Γ—20)=010000_2 = (0 \times 2^2) + (0 \times 2^1) + (0 \times 2^0) = 0_{10}0002​=(0Γ—22)+(0Γ—21)+(0Γ—20)=010​

    • 0012=(0Γ—22)+(0Γ—21)+(1Γ—20)=110001_2 = (0 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) = 1_{10}0012​=(0Γ—22)+(0Γ—21)+(1Γ—20)=110​

    • 0102=(0Γ—22)+(1Γ—21)+(0Γ—20)=210010_2 = (0 \times 2^2) + (1 \times 2^1) + (0 \times 2^0) = 2_{10}0102​=(0Γ—22)+(1Γ—21)+(0Γ—20)=210​

    • 0112=(0Γ—22)+(1Γ—21)+(1Γ—20)=310011_2 = (0 \times 2^2) + (1 \times 2^1) + (1 \times 2^0) = 3_{10}0112​=(0Γ—22)+(1Γ—21)+(1Γ—20)=310​

    • 1002=(1Γ—22)+(0Γ—21)+(0Γ—20)=410100_2 = (1 \times 2^2) + (0 \times 2^1) + (0 \times 2^0) = 4_{10}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=2562^8 = 25628=256 possible values can be represented (from 0 to 255).

  • For example, a byte of all 1s (11111111211111111_2111111112​) 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 00100001 translates to 72 73 33 in 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:

  1. 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 n pages, it takes up to n steps. (e.g., 1,000 pages = 1,000 steps).

  2. 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 + 1 steps), but still linear.

  3. Binary Search (Divide and Conquer):

    • Algorithm:

    1. Open to the middle of the phone book.

    2. Check the page.

    3. If the name is there, stop.

    4. If the name is alphabetically earlier, discard the right half of the book.

    5. If the name is alphabetically later, discard the left half of the book.

    6. Repeat steps 1-5 with the remaining half.

      Correctness**: Yes. Efficiency**: Dramatically faster. For 1,000 pages, it takes roughly 10 steps (because 210β‰ˆ10002^{10} \approx 1000210β‰ˆ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 the ask function.

  • 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) times block that encapsulates the play sound "meow" and wait (1) seconds inside a repeat (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 keyboard and feel for walls to 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent