upgrade
upgrade

🐛Intro to Computer Programming

Basic Programming Concepts

Study smarter with Fiveable

Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.

Get Started

Why This Matters

Programming isn't just about memorizing syntax—it's about understanding how computers process information and how you can control that process. Every concept in this guide connects to a fundamental question: How do we store data? How do we make decisions? How do we repeat tasks efficiently? How do we organize code for reuse? These principles appear in every programming language you'll ever learn, making them the true foundation of computer science.

When you're tested on these concepts, you're being evaluated on your ability to choose the right tool for the problem, not just recall definitions. Can you explain why you'd use a loop instead of repeated statements? Do you understand when a function improves your code? Don't just memorize what each concept does—know what problem each one solves and how they work together to create dynamic programs.


Storing and Managing Data

Every program needs to hold information somewhere. Data storage concepts determine how your program remembers, organizes, and accesses the information it needs to function.

Variables and Data Types

  • Variables act as named containers—they store values that your program can reference, modify, and use throughout execution
  • Data types define what kind of information a variable holds: integers for whole numbers, floats for decimals, strings for text, booleans for true/false values
  • Type selection affects memory and operations—you can't perform arithmetic on strings or concatenate integers without conversion

Arrays and Lists

  • Arrays store fixed-size collections of elements with the same data type, accessed by numerical index starting at 00
  • Lists offer dynamic sizing—they can grow, shrink, and often hold mixed data types depending on the language
  • Index-based access enables efficient retrieval—accessing element at position ii is typically O(1)O(1) time complexity

Compare: Variables vs. Arrays—both store data, but variables hold single values while arrays hold collections. If an exam asks about storing multiple related items (like test scores), arrays are your answer; for single values (like a user's name), use a variable.


Communicating with Users and Systems

Programs need to receive information and present results. Input/output operations form the bridge between your code and the outside world—users, files, and other systems.

Input and Output

  • Input captures external data—keyboard entry via functions like input() or scanf(), file reading, or data from other programs
  • Output displays results—screen printing with print() or printf(), file writing, or sending data to other systems
  • I/O handling requires validation—user input is unpredictable, so checking data before processing prevents crashes

File Handling

  • Files enable data persistence—information survives after your program ends, unlike variables stored only in memory
  • Core operations follow a pattern: open the file, perform read/write operations, then close to free system resources
  • File modes control access—read mode (rr), write mode (ww), and append mode (aa) determine what operations are permitted

Compare: Console I/O vs. File I/O—both move data in and out of your program, but console operations are temporary and interactive while file operations create permanent records. FRQs often ask you to modify programs to save results—that's when file handling becomes essential.


Making Decisions and Performing Operations

Programs need to evaluate conditions and perform calculations. Operators and control structures give your code the ability to think, compare, and choose different paths.

Operators

  • Arithmetic operators perform math: ++ (addition), - (subtraction), * (multiplication), //// (division), %\% (modulus for remainders)
  • Comparison operators return booleans: ==== (equal), !=!= (not equal), >>, <<, >=>=, <=<= evaluate relationships between values
  • Logical operators combine conditions: AND requires both true, OR requires at least one true, NOT inverts the result

Control Structures

  • If-else statements enable branching—your program evaluates a condition and executes different code blocks based on the result
  • Loops automate repetition: for loops iterate a known number of times, while loops continue until a condition becomes false
  • Nesting combines structures—loops inside loops, conditionals inside loops, creating complex decision trees

Compare: for loops vs. while loops—both repeat code, but for loops are ideal when you know the iteration count in advance (processing each array element), while while loops handle uncertain repetition (waiting for valid input). Choose based on whether the endpoint is known.


Organizing and Reusing Code

As programs grow, organization becomes critical. Functions and object-oriented principles help you write maintainable, modular code that doesn't repeat itself.

Functions and Methods

  • Functions are reusable code blocks—define once, call anywhere, reducing redundancy and improving readability
  • Parameters pass data in, return values pass data out—functions can accept inputs and produce outputs without affecting global state
  • Methods are object-bound functions—in OOP, they operate on the data contained within their object

Object-Oriented Programming Basics

  • Classes serve as blueprints—they define the attributes (data) and methods (behaviors) that objects of that type will have
  • Four pillars structure OOP: encapsulation (bundling data with methods), inheritance (creating specialized subclasses), polymorphism (same interface, different implementations), abstraction (hiding complexity)
  • Objects are instances of classes—each object has its own attribute values but shares the class's method definitions

Compare: Functions vs. Methods—both are reusable code blocks, but functions stand alone while methods belong to objects and can access that object's internal data. When code needs to operate on object-specific data, use methods; for general-purpose operations, use functions.


Solving Problems Systematically

Writing code means solving problems. Algorithms provide the logical steps, while debugging ensures those steps work correctly.

Basic Algorithms and Problem-Solving

  • Algorithms are step-by-step procedures—precise instructions that transform input into desired output, independent of programming language
  • Fundamental algorithms appear everywhere: searching (finding items), sorting (ordering items), counting, and accumulating (summing values)
  • Efficiency matters—the same problem can be solved multiple ways, but some algorithms use significantly less time or memory

Debugging Techniques

  • Debugging identifies and fixes errors—syntax errors prevent compilation, logic errors produce wrong results, runtime errors crash execution
  • Strategic print statements reveal program state—output variable values at key points to trace where behavior diverges from expectations
  • Debugger tools offer precision—breakpoints pause execution, step-through shows line-by-line flow, watch windows track variable changes

Compare: Syntax errors vs. Logic errors—syntax errors are caught by the compiler and prevent your program from running, while logic errors let the program run but produce incorrect results. Logic errors are harder to find because the code "works"—it just doesn't do what you intended.


Quick Reference Table

ConceptBest Examples
Data StorageVariables, Arrays, Lists
Data TypesIntegers, Floats, Strings, Booleans
User InteractionInput functions, Print statements, File I/O
Decision MakingIf-else statements, Comparison operators
RepetitionFor loops, While loops
Code OrganizationFunctions, Methods, Classes
OOP PrinciplesEncapsulation, Inheritance, Polymorphism
Problem SolvingSorting algorithms, Searching algorithms, Debugging

Self-Check Questions

  1. Which two data structures both store collections of items, and what key difference determines when you'd choose one over the other?

  2. Compare and contrast for loops and while loops—give a specific scenario where each would be the better choice.

  3. If a program needs to remember user data after closing, which concept from this guide solves that problem, and what are the three basic operations involved?

  4. Which three types of operators would you use to write a condition like "if the score is greater than 90 AND the student is enrolled"? Identify each operator type in that statement.

  5. A student writes a program that runs without errors but produces incorrect output. What type of error is this, and what debugging technique would you recommend they try first?