Lecture 1: Introduction to Programming

Welcome to CS103! This lecture will introduce you to the fundamental concepts of programming and set the stage for our journey into C++.

Instructor: Dr. Debasish Pattanayak

What is Programming?

What is Programming?

Programming is giving computers instructions to solve problems. It involves algorithms (step-by-step solutions), syntax (language rules), logic (reasoning), and debugging (fixing errors).

  • Algorithms: Step-by-step solutions to problems
  • Syntax: Rules of the programming language
  • Logic: The reasoning behind your code
  • Debugging: Finding and fixing errors

Programming is the process of creating a set of instructions that tell a computer how to perform a task. These instructions are written in a specific language that the computer can understand. Essentially, you are giving a computer a recipe to follow, a precise sequence of steps to achieve a desired outcome. Without programming, computers are just inert machines; it's the code that brings them to life and enables them to perform countless tasks, from simple calculations to complex simulations.

Key concepts fundamental to programming:

  • Algorithms: At the heart of programming is the algorithm. An algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation. Think of it as a detailed, unambiguous plan or recipe. For example, an algorithm for making tea would involve steps like "boil water," "add tea leaves," "steep," and "pour."
  • Syntax: Just like human languages have grammar rules, programming languages have syntax. Syntax refers to the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in a particular language. A single misplaced comma or a misspelled keyword can lead to a "syntax error," preventing the program from running.
  • Logic: While syntax dictates how you write code, logic dictates what the code does. Logic is the reasoning behind the sequence of instructions to achieve the desired outcome. It's about designing the flow of your program, making decisions (e.g., "if this condition is true, do X; otherwise, do Y"), and handling different scenarios. Flaws in logic lead to "logical errors," where the program runs but produces incorrect results.
  • Debugging: No programmer writes perfect code on the first try. Debugging is the systematic process of finding and fixing errors or "bugs" in the source code. It involves identifying the problem, locating the faulty code, understanding why it's failing, and then correcting it. Debugging is a crucial skill that often takes more time than writing the initial code.

How Do Computers Work?

How Do Computers Work?

Computers are deterministic machines that follow instructions precisely and predictably.

  • Binary System: Everything is 0s and 1s (electricity on/off)
  • CPU (Processor): Executes instructions one by one
  • Memory (RAM): Temporarily stores data and instructions
  • Storage: Permanent data storage (hard drive, SSD)
  • Deterministic: Same input → Same output, every time
// Deterministic example:
int result = 5 + 3; // Always equals 8

Understanding how computers work is essential for effective programming. At their core, computers are deterministic machines that process information through a series of predictable, logical operations.

The Deterministic Nature of Computing

Programming is fundamentally a deterministic process. This means that given the same input and conditions, a computer program will always produce the same output. There's no randomness or unpredictability in how computers execute instructions - they follow your code exactly as written, every single time.

Basic Computer Architecture

  • Binary System: All data is represented as combinations of 0s and 1s, corresponding to electrical states (off/on).
  • CPU (Central Processing Unit): The "brain" that executes instructions sequentially.
  • Memory (RAM): Temporary storage for data and programs currently in use.
  • Storage: Permanent storage for programs and data (hard drives, SSDs).
  • Input/Output Devices: Keyboard, mouse, screen, etc.

This deterministic nature is both a strength and a responsibility - your programs will do exactly what you tell them to do, so precision in your instructions is crucial.

Brief History of Programming Languages

History of Languages

Programming languages evolved from machine code (0s and 1s) to human-readable high-level languages.

  • 1GL: Machine Language (binary)
  • 2GL: Assembly Language
  • 3GL: High-Level Languages (C++, Python)
  • 4GL: Domain-Specific (SQL)
  • 5GL: AI-focused languages

From early, cumbersome machine code to the sophisticated, human-friendly languages of today, programming has undergone a remarkable evolution, driven by the need for greater abstraction, efficiency, and problem-solving capabilities.

  • First Generation (1GL): Machine Language (e.g., binary code - 0s and 1s).
  • Second Generation (2GL): Assembly Language (e.g., Assembly).
  • Third Generation (3GL): High-Level Languages (e.g., Fortran, COBOL, C, C++, Java, Python).
  • Fourth Generation (4GL): Domain-Specific Languages (e.g., SQL for database queries).
  • Fifth Generation (5GL): Artificial Intelligence Languages (e.g., Prolog, OPS5).

High-Level vs. Low-Level Languages

High-Level vs. Low-Level

A look at the trade-offs between performance and abstraction.

  • Low-Level (e.g., Assembly): Fast, direct hardware control. Complex & platform-dependent.
  • High-Level (e.g., C++): Human-friendly, portable. Easier & faster to develop.
// High-Level (C++)
int sum = 5 + 10;

// Low-Level (Conceptual Assembly)
MOV AX, 5
ADD AX, 10

The distinction between high-level and low-level languages is crucial for understanding how software interacts with hardware and for choosing the right tool for a given task.

  • Low-Level Languages: Closer to machine code, fast, platform-dependent.
    ; Add two numbers
    MOV AX, 5
    MOV BX, 10
    ADD AX, BX
  • High-Level Languages: Closer to human language, easier, portable.
    // Add two numbers
    int a = 5;
    int b = 10;
    int sum = a + b;

Your First C++ Program: "Hello, World!"

Your First C++ Program

The classic "Hello, World!" demonstrates the basic structure of a C++ program.

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
  • #include: Brings in external functionality
  • main(): The entry point of every C++ program
  • std::cout: Outputs text to the console
  • return 0: Indicates successful execution

Let's start with the classic "Hello, World!" program in C++. This simple program is traditionally the first program learned in any language, as it demonstrates the basic structure and how to produce output. It will print the text "Hello, World!" to the console.

#include <iostream> // Allows input and output operations.

int main() { // The entry point of the program.
    std::cout << "Hello, World!" << std::endl; // Prints to the console.
    return 0; // Indicates successful execution.
}

Explanation: #include , int main(), std::cout, <<, "Hello, World!", std::endl, and return 0; are the key components.

Setting Up Your Development Environment

Setting Up Your Environment

You need a compiler (like G++) to turn your .cpp code into an executable program.

  • Windows: Install MinGW-w64
  • macOS: Install Xcode Command Line Tools
  • Linux: Install `build-essential`

Example Compilation:

# Compile the code
g++ hello.cpp -o hello

# Run the program
./hello

To write, compile, and run C++ programs, you'll need a C++ compiler and a text editor or Integrated Development Environment (IDE). The compiler translates your human-readable C++ code into machine code that your computer can execute.

Installing a C++ Compiler (GCC/G++)

  • Windows: Install MinGW-w64.
  • macOS: Install Xcode Command Line Tools.
  • Linux (Debian/Ubuntu): Install `build-essential`.

Compiling and Running C++ Programs

  1. Save your code as hello.cpp.
  2. Open Terminal/Command Prompt.
  3. Compile: g++ hello.cpp -o hello
  4. Run: ./hello (on macOS/Linux) or hello.exe (on Windows).

Setting Up GitHub (Basic)

GitHub is a web-based platform for version control using Git. It's essential for managing your code.

C++ vs. Python: Why C++ in the Age of Python?

Why Learn C++?

C++ offers superior performance, system-level control, and resource efficiency. This makes it vital for:

  • Game Engines: Real-time graphics and performance
  • Operating Systems: Low-level system programming
  • Embedded Systems: Resource-constrained devices
  • High-Frequency Trading: Microsecond-level performance
  • Scientific Computing: Computationally intensive tasks

It also provides a deep understanding of computer architecture and memory management.

Python is incredibly popular, but C++ is still a fundamental language to learn, especially in a course like CS103. The answer lies in C++'s unique strengths and its role in foundational computing.

  • Performance: C++ is compiled, resulting in significantly faster execution speeds compared to interpreted languages like Python.
  • System-Level Programming: C++ provides low-level memory management and direct hardware access.
  • Resource Efficiency: C++ gives you precise control over memory and CPU usage.
  • Foundation for Libraries: Many high-performance Python libraries (e.g., NumPy, TensorFlow) have their core components written in C++.
  • Understanding Computer Architecture: C++ forces you to think about how computers actually work.

Problem-Solving Methodology

Problem-Solving Methodology

A systematic approach to tackling programming challenges:

  • 1. Understand: Read the problem carefully, identify inputs/outputs
  • 2. Plan: Break down into smaller steps, create pseudocode
  • 3. Code: Implement your solution step by step
  • 4. Test: Try different inputs, edge cases
  • 5. Debug: Fix any errors or unexpected behavior
  • 6. Optimize: Improve efficiency and readability
// Example: Find the larger of two numbers
// 1. Understand: Compare two integers, return the larger one
// 2. Plan: Use if-else statement for comparison
int findMax(int a, int b) {
    return (a > b) ? a : b;
}

Effective programming isn't just about knowing syntax - it's about developing a systematic approach to problem-solving. This methodology will serve you throughout your programming journey.

The Six-Step Process

  1. Understand the Problem: Read carefully, identify what you're given and what you need to produce.
  2. Plan Your Approach: Break the problem into smaller, manageable pieces. Write pseudocode.
  3. Implement Your Solution: Write the actual code, focusing on one piece at a time.
  4. Test Thoroughly: Try various inputs, including edge cases and invalid data.
  5. Debug and Fix: When things don't work as expected, systematically find and fix issues.
  6. Optimize and Refactor: Make your code more efficient, readable, and maintainable.

This process becomes second nature with practice and will make you a more effective programmer.

Fun Programming Expert Bar

Fun Programming Expert Bar

An interactive tool to estimate your programming expertise. This slide is best viewed in normal mode!

Switch back to normal view to try the interactive calculator that measures your programming skills based on languages known, algorithms implemented, and data structures understood.

How do you measure up as a programming expert? Enter your stats below!

(Note: This is just for fun and doesn't reflect actual expertise!)

C++ Comments and Variables

Comments and Variables

Comments (// or /*...*/) explain code. Variables are named containers for storing data.

You must declare a variable's type before using it:

// dataType variableName = value;
int score = 100;
double price = 19.99;
std::string playerName = "Alex";
bool isActive = true;
  • Comments: Documentation for your code
  • Variables: Named storage for data
  • Data Types: int, double, string, bool, char
  • Static Typing: Must declare type before use

As you begin writing your own C++ programs, two fundamental concepts you'll encounter immediately are comments and variables.

C++ Comments

  • Single-line comments: Start with //.
  • Multi-line comments: Start with /* and end with */.

C++ Variables

Variables are named storage locations that hold data. You must declare them with a data type:

  • int: for whole numbers (e.g., 100).
  • double: for decimal numbers (e.g., 98.6).
  • char: for single characters (e.g., 'A').
  • bool: for true or false.
  • std::string: for text (e.g., "Alice").
Back to Course Outline

Slides

What is Programming? How Computers Work History of Languages High vs Low Level Hello World Development Setup Why C++? Problem Solving Expert Bar Comments & Variables