1. Home
  2. Explore
  3. Computer Science
  4. Introduction to Computer Science

Study methods

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

Introduction to Computer Science

💡 Introduction to Computer Science & Programming

This course, CS50, is Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. It emphasizes the applicability of computer science to various fields, including arts, humanities, social sciences, and natural sciences. The course aims to equip students with computational thinking and practical programming skills.

The journey will be challenging, likened to "drinking from a fire hose" due to the volume of new information. However, the goal is to foster an understanding of computer science and programming, and most importantly, how to teach yourself new technologies. Most CS50 students have no prior computer science experience, and success is measured by personal growth from the beginning of the course, not by comparison to others.


🤝 The CS50 Community & Traditions

CS50 fosters a collective shared community experience alongside academic support. This is highlighted through several traditions:

  • CS50 Puzzle Day: An early-term event focused on problem-solving through logic puzzles, emphasizing that computer science is more about problem-solving than specific programming languages. It's a collaborative event with friends, pizza, and prizes.

  • CS50 Hackathon: Towards the end of the semester, an overnight opportunity to work on final projects.

  • CS50 Fair: A campus-wide exhibition of student final projects (web apps, mobile apps, etc.) for the university community. The objective is to create something not explicitly taught in the course, signaling readiness for independent learning.

  • CS50 T-shirt: A proudly held tradition for all who complete the course.


🧠 What is Computer Science?

Computer science is fundamentally the study of information – how to represent it and how to process it. More broadly, it teaches computational thinking, which is the application of computer science ideas to solve problems of interest, both within and outside the classroom.

At its core, computer science is about problem-solving. This can be visualized as:

  • Input: The problem to solve.

  • Process: How the input is transformed.

  • Output: The solution.

The key challenge is how to process the input to produce the desired output, especially when using computers that require standardized representation of information.


🔢 Representing Information: Bits & Binary

Before processing information, we must agree on how to represent it in a standardized way that computers can understand.

Unary vs. Binary Notation

  • Unary Notation (Base 1): Counting by simple aggregation (e.g., 1, 2, 3, 4, 5 fingers up). Limited in scope.

  • Binary Notation (Base 2): Using two distinct states (e.g., finger up or down). This allows for counting much higher with the same number of "digits." For example, with one hand (5 fingers), you can count up to 31 in binary.

0s and 1s: The Language of Computers

Computers fundamentally understand and "speak" in binary, using only zeros and ones.

  • Binary Digit (Bit): A single 0 or 1. Bi implies two possibilities.

  • Decimal System: The base-10 system humans use (0-9, 10 possibilities).

  • Computer Representation: Computers use bits because they rely on physical components like transistors, which act as tiny light switches that can be either off (0) or on (1).

Counting with Bits

To count beyond 0 or 1, multiple bits are used. Each bit represents a power of 2, similar to how decimal places represent powers of 10.

Decimal System (Base 10)

Binary System (Base 2)

Hundreds (10210^2102)

Fours (222^222)

Tens (10110^1101)

Twos (212^121)

Ones (10010^0100)

Ones (202^020)

Example with 3 bits:

  • 000 = 0×4+0×2+0×1=00 \times 4 + 0 \times 2 + 0 \times 1 = 00×4+0×2+0×1=0

  • 001 = 0×4+0×2+1×1=10 \times 4 + 0 \times 2 + 1 \times 1 = 10×4+0×2+1×1=1

  • 010 = 0×4+1×2+0×1=20 \times 4 + 1 \times 2 + 0 \times 1 = 20×4+1×2+0×1=2

  • 011 = 0×4+1×2+1×1=30 \times 4 + 1 \times 2 + 1 \times 1 = 30×4+1×2+1×1=3

  • 100 = 1×4+0×2+0×1=41 \times 4 + 0 \times 2 + 0 \times 1 = 41×4+0×2+0×1=4

  • 101 = 1×4+0×2+1×1=51 \times 4 + 0 \times 2 + 1 \times 1 = 51×4+0×2+1×1=5

  • 110 = 1×4+1×2+0×1=61 \times 4 + 1 \times 2 + 0 \times 1 = 61×4+1×2+0×1=6

  • 111 = 1×4+1×2+1×1=71 \times 4 + 1 \times 2 + 1 \times 1 = 71×4+1×2+1×1=7

With 3 bits, you can represent 23=82^3 = 823=8 distinct patterns (0-7). To count higher, simply add more bits. For example, 1000 (binary) is 8 (decimal).

Bytes

  • Byte: A group of 8 bits.

  • With 8 bits (1 byte), you can represent 28=2562^8 = 25628=256 distinct possibilities, typically from 0 to 255.

  • Numbers like 255 and 256 are common in computing due to the use of bytes.


🔠 Representing Text: ASCII & Unicode

Computers use numbers (binary digits) to represent non-numeric information like letters.

ASCII

  • ASCII (American Standard Code for Information Interchange): A system that assigns a unique number to each character (uppercase letters, lowercase letters, numbers, punctuation, etc.).

  • Example:

    • Capital A is represented by 01000001 (binary), which is 65 (decimal).

    • Capital B is 66.

    • Lowercase a is 97.

  • ASCII originally used 7 bits, later extended to 8 bits, allowing for 256 different characters. This is sufficient for English but not for all human languages.

  • When you send a text message like "HI!", your phone sends three bytes (one for H, one for I, one for !). The receiving phone interprets these bytes as their corresponding ASCII characters and displays them.

Unicode

  • Unicode: A newer, more comprehensive standard designed to represent all human languages, past, present, and future, including symbols and emojis.

  • It is backwards compatible with ASCII (retains ASCII's number assignments).

  • Unicode uses more bits than ASCII:

    • 8 bits for English letters.

    • 16, 24, or even 32 bits for other languages and complex symbols.

    • With 32 bits, Unicode can represent up to 4 billion possible characters.

  • Emojis are characters represented by specific Unicode patterns of zeros and ones. Different devices or software might render the same emoji differently because their artists interpret the descriptions (e.g., "face with tears of joy") uniquely.

  • New emojis are released annually by the Unicode Consortium. If a device's software is not updated, newer emojis might appear as empty squares or generic placeholders.


🌈 Representing Multimedia: Colors, Images, Videos, Sound

All forms of multimedia are ultimately broken down into numbers (zeros and ones) for computer processing.

Colors

  • RGB (Red Green Blue): A common system for representing colors.

  • A single dot on the screen, called a pixel, is represented by three numbers (typically three bytes, or 24 bits total per pixel).

  • Each byte represents the intensity of red, green, or blue light (0-255).

  • Example: (72, 73, 33) might represent a specific shade of yellow.

    • 72 (out of 255) for red.

    • 73 (out of 255) for green.

    • 33 (out of 255) for blue.

  • Images are composed of millions of pixels, each with its own RGB values. This is why image files (e.g., photos) can be megabytes in size.

Videos

  • Videos are essentially sequences of still images (frames) displayed rapidly.

  • Typically, 24 or 30 frames per second are shown to create the illusion of continuous motion.

  • Since images are represented by bytes, videos are just many, many images, leading to gigabyte file sizes.

Sound

  • Sound can be represented by digitizing its characteristics.

  • Each musical note or sound can be assigned a number.

  • Multiple numbers (e.g., three bytes like RGB) could represent:

    1. Pitch/Frequency: The note itself.

    2. Volume: How loud the sound is.

    3. Duration: How long the note lasts.

  • Additional bytes could specify the instrument or other sound qualities.

Context is Key: The same pattern of zeros and ones (e.g., 72, 73, 33) can be interpreted as:

  • The text "HI!" in a word processor.

  • A yellow pixel in an image editor.

  • A sequence of numbers in a spreadsheet. The software and its context determine how the bits are interpreted and displayed to the user. Programmers define this interpretation through code.


⚙️ Algorithms: Step-by-Step Problem Solving

An algorithm is a precise, step-by-step description of how to solve a problem. Precision is crucial for algorithms to be effective.

Phone Book Search Algorithms

Consider the problem of finding a name in a phone book:

  1. Algorithm 1: Linear Search

    • Start at the beginning and check each page one by one.

    • Correct: Yes, if the name is there, you'll find it.

    • Efficiency: Very slow. In the worst case (name is at the very end), it takes N steps (where N is the number of pages). This is represented by a linear growth curve.

  2. Algorithm 2: Linear Search (Faster)

    • Check two pages at a time.

    • Correct: No, you might miss the name if it's on an odd-numbered page between two checked pages. (Can be fixed by checking one page back if you pass the section).

    • Efficiency: Faster than Algorithm 1 (roughly N/2 steps), but still linear.

  3. Algorithm 3: Binary Search

    • Open to the middle of the phone book.

    • Determine if the name is in the left half or the right half.

    • Discard the half that doesn't contain the name.

    • Repeat the process on the remaining half.

    • Correct: Yes.

    • Efficiency: Much faster. Each step halves the problem size. For a 1000-page phone book, it takes approximately 10 steps (log⁡21000≈9.96\log_2 1000 \approx 9.96log2​1000≈9.96). This is represented by a logarithmic growth curve, which is significantly flatter and scales much better with larger problem sizes.

Algorithmic Efficiency: The choice of algorithm can dramatically affect the time required to solve a problem, especially with big data. Better algorithms lead to faster, more competitive, and more usable software.

Pseudocode: Bridging English and Code

Pseudocode is a way to express algorithms using a mix of natural language (like English) and programming-like constructs. It's not a formal language but aims for precision and finiteness.

Example: Phone Book Search Pseudocode

  1. Pick up phone book.

  2. Open to middle of phone book.

  3. Look at page.

  4. If person is on page:

  5. Then call person.

  6. Else if person is earlier in book:

  7. Then open to middle of left half of book.

  8. Go back to line 3.

  9. Else if person is later in book:

  10. Then open to middle of right half of book.

  11. Go back to line 3.

  12. Else (person not found):

  13. Then quit.

Fundamental Constructs of Programming

Pseudocode illustrates four key characteristics found in most computer programs:

  1. Functions (Verbs/Actions):

    • Bite-sized tasks or actions a computer can perform.

    • Examples: Pick up phone book, Open to middle, Look at page, Call person, Quit.

    • Functions can take inputs (arguments) and sometimes produce outputs (return values) or side effects (visual changes).

  2. Conditionals (Forks in the Road):

    • If, else if, else statements that allow a program to make decisions.

    • The path taken depends on a Boolean expression.

    • Boolean Expression: A question with a yes/no or true/false answer (e.g., person is on page, person is earlier in book).

  3. Loops (Cycles):

    • Go back to instructions that cause a set of steps to repeat.

    • Loops are crucial for tasks that need to be performed multiple times.

    • It's important to design loops that eventually terminate to avoid infinite loops (where the program gets stuck repeating forever).

  4. Logical Significance of Indentation:

    • Indentation in pseudocode (and many programming languages) visually represents the hierarchy and scope of code. It indicates which actions are performed under specific conditions or within specific loops.


🤖 Artificial Intelligence (AI) and Large Language Models

AI, particularly chatbots and large language models (LLMs) like ChatGPT, relies on different principles than simple conditional logic.

  • Simple Chatbot (Conditional Logic):

    • If student says "hello", then say "hello back".

    • This approach quickly becomes unmanageable due to the infinite number of possible human inputs.

  • Modern AI (Training Data & Neural Networks):

    • AI systems are trained on vast amounts of data (e.g., the entire internet, Wikipedia, books).

    • They use neural networks (inspired by the human brain, with interconnected "neurons") to process this data.

    • The goal is to produce the most probabilistically likely answer to a question, rather than following explicit if/else rules for every scenario.

    • This involves a lot of probability and statistics.

Rubber Duck Debugging & CS50.ai

  • Rubber Duck Debugging: A programming technique where you explain your code or problem out loud to an inanimate object (like a rubber duck). The act of verbalizing often helps you identify your own mistakes or logical flaws.

  • CS50.ai: An AI-based tool designed specifically for CS50 students. It acts like a "virtual rubber duck" or a helpful teaching fellow, guiding students to solutions rather than just providing answers. This tool is permitted and encouraged for use in the course, unlike general-purpose LLMs like ChatGPT for assignments.


💻 Programming Languages & Abstraction

Programming involves different levels of abstraction:

  • Lowest Level: Zeros and ones (machine code), which computers directly understand.

  • Higher Level: Programming languages designed for humans.

C Programming Language

  • C is an older but still very popular and omnipresent language, known for its speed and efficiency in operating devices.

  • Pedagogical Value: Learning C provides a conceptual foundation for understanding how computers work at a lower level, which makes learning other, newer languages like Python, JavaScript, and SQL easier.

  • Syntax: C can appear cryptic due to its punctuation and syntax (semicolons, parentheses, curly braces). This requires muscle memory that comes with practice.

Scratch: A Visual Programming Language

  • Scratch (from MIT) is a graphical programming language ideal for beginners.

  • It allows programming by dragging and dropping puzzle pieces (blocks) that snap together logically, eliminating the need for complex syntax like parentheses or semicolons.

  • Benefits:

    • Provides a mental model for understanding programming concepts.

    • Makes learning subsequent languages easier by focusing on ideas rather than syntax.

    • Great for making games, animations, and interactive art.

Scratch User Interface Tour

  • Palette of Puzzle Pieces (Left): Blocks categorized by color (e.g., blue for motion, purple for looks, pink for sound).

  • Script Area (Middle): Where programs are built by dragging and dropping blocks.

  • Stage (Top Right): The world where sprites (characters like the default cat) interact and execute the code.

  • Green Flag / Red Stop Sign: Start and stop program execution.

  • Coordinates: Sprites move on an X-Y coordinate system (e.g., 0,0 for center, 0,180 for top).

Scratch Programming Concepts

  1. Events: Blocks that trigger code execution (e.g., when green flag clicked, when motion exceeds).

  2. Functions (Blocks):

    • Actions or verbs (e.g., say "Hello World!", ask "What's your name?" and wait).

    • Can take inputs (arguments), which are values passed into the function (e.g., the text "Hello World!").

    • Can produce side effects (e.g., the cat speaking visually or audibly).

    • Can produce return values (e.g., the answer block from the ask function).

    • Variables: Return values can be stored and reused using variables (like answer).

    • Nesting Functions: Outputs of one function can become inputs to another (e.g., join "Hello " [answer] as input to say). This is similar to mathematical parentheses.

  3. Custom Blocks (User-Defined Functions):

    • Programmers can create their own reusable blocks to encapsulate complex logic (e.g., a meow n times block that defines how to meow).

    • This promotes abstraction and avoids repetition (DRY - Don't Repeat Yourself principle).

    • Custom blocks can also take inputs (arguments).

  4. Loops:

    • repeat block: Executes code a specified number of times.

    • forever block: Executes code continuously.

  5. Conditionals:

    • if block: Executes code only if a Boolean expression is true.

    • if/else block: Executes one block of code if true, another if false.

    • Boolean Expressions: Blocks that evaluate to true or false (e.g., touching mouse pointer?, key [up arrow] pressed?).

  6. Extensions: Scratch can integrate with external functionalities like:

    • Text to Speech: Allows sprites to speak audibly, with customizable voices.

    • Video Sensing: Uses a webcam to detect motion, enabling interactive programs based on physical movement.

Building Complex Programs: Baby Steps

Creating complex games or applications in Scratch (or any language) is done by taking baby steps:

  • Implement one tiny feature at a time.

  • Test and refine each feature before adding the next.

  • Break down the problem into smaller, manageable parts.

  • Example: In "Oscar Time," initially, Oscar only changed costumes when touched. Then trash was added, then falling trash, then scoring, all incrementally.

Example: Ivy's Hardest Game

This game demonstrates how simple programming primitives can create complex interactions:

  • Harvard Sprite: Moves with arrow keys (if key pressed, change x/y). Bounces off walls (if touching wall, change x/y in opposite direction). Custom functions (listen for keyboard, feel for walls) abstract this logic.

  • Yale Sprite: Moves mindlessly back and forth (forever loop, move steps, if touching wall, turn 180 degrees).

  • MIT Sprite: Chases the Harvard sprite (forever loop, point towards Harvard sprite, move steps). Speed can be adjusted by changing the move steps value.

  • Game Progression: Levels increase complexity by adding more sprites, different behaviors, and more intricate mazes.

The power of programming lies in combining these fundamental concepts – functions, loops, conditionals, and variables – to create rich, interactive, and problem-solving applications.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent