Programming Basics

Programming

Programming is the process of writing instructions (code) for a computer to execute. The goal is to solve a problem, perform a task, or automate a process.

1. How Code Runs: Compilation vs. Interpretation

Computers only understand machine code (binary). Therefore, the high-level code we write (like C++, Python, or Java) must be translated before the CPU can execute it.

Translation Methods

  • Compilation (e.g., C, C++): The entire source code is translated into machine code (an executable file, like .exe) by a Compiler before the program runs. It is typically faster during execution but requires re-compilation if changed and is platform-dependent.
  • Interpretation (e.g., Python, JavaScript): An Interpreter translates the source code line-by-line as the program runs. It is typically slower than compiled code but allows for faster testing, easier debugging, and execution across different platforms.
  • Hybrid (e.g., Java, C#): The source code is compiled into an intermediate bytecode, which is then interpreted or Just-In-Time (JIT) compiled by a Virtual Machine (like the JVM) at runtime. This provides a balance of speed and platform independence.
Key Takeaways
  • Compilers translate the entire program into machine code before execution, resulting in faster runtime speeds.
  • Interpreters translate code line-by-line during execution, resulting in easier debugging but slower execution.
  • Hybrid languages compile to bytecode and use virtual machines for cross-platform portability.

2. Data Representation in Computers

At the lowest level, all data is stored as binary (0s and 1s). How do computers represent negative numbers, text, or fractions?

2.1 Representing Integers and Negative Numbers

A single binary digit is a bit (0 or 1). Eight bits make a byte.

Integer Representation

  • Unsigned Integers: Represent only positive numbers. An 8-bit unsigned integer can represent values from 0 to 255.
  • Signed Integers (Two's Complement): The most common method for representing negative numbers. The Most Significant Bit (MSB) acts as a sign bit (0 for positive, 1 for negative). To get the Two's Complement of a number: invert all the bits (1s complement) and add 1.

2.2 Representing Text

Computers use encoding standards to map binary numbers to human-readable characters.

Character Encoding

  • ASCII (American Standard Code for Information Interchange): Uses 7 bits to represent 128 characters (English alphabet, numbers, and basic symbols). For example, the letter 'A' is represented by decimal 65 (binary 01000001).
  • Unicode (e.g., UTF-8): A modern standard that uses up to 32 bits, allowing representation of over a million characters, encompassing all world languages, symbols, and even emojis.

2.3 Representing Fractional Numbers (IEEE 754)

Decimals (floating-point numbers) are complicated to store in binary. The IEEE 754 Standard breaks a floating-point number into three parts to store it efficiently, similar to scientific notation.

IEEE 754 Components

  • Sign Bit (1 bit): Determines if the number is positive (0) or negative (1).
  • Exponent (e.g., 8 bits for Single Precision): The power of 2 by which the number is scaled, stored with a bias to handle negative exponents.
  • Mantissa / Fraction (e.g., 23 bits for Single Precision): The significant digits of the number.
Key Takeaways
  • Negative numbers are commonly stored using Two's Complement, where the leftmost bit indicates the sign.
  • Text is mapped to binary using standards like ASCII (English only) and Unicode/UTF-8 (global characters).
  • Fractional decimals are stored using the IEEE 754 Floating-Point Standard, splitting the number into a sign, exponent, and mantissa.

3. Syntax and Semantics

Just like human languages, programming languages have rules.

Key Concepts

  • Syntax: The set of rules that define the structure of the language (grammar). If syntax is wrong, the code won't compile or run (Syntax Error).
  • Semantics: The meaning of the instructions. Code can be syntactically correct but semantically wrong, producing incorrect results (Logic Error or Bug).
Key Takeaways
  • Syntax refers to the strict grammar rules of a programming language; code will not compile or run if syntax is incorrect.
  • Semantics refers to the logic and meaning behind the code; syntactically correct code can still have semantic errors (bugs).

4. Variables and Data Types

A variable is a named storage location in the computer's memory that holds a value. Think of it as a container with a label.

4.1 Declaring Variables and Typing Systems

Variables are essential because they allow your program to be dynamic. Instead of writing code that only calculates the area for a radius of 5, you can write code that calculates the area for a variable named radius, which can hold any user input.

Typing Systems

  • Statically Typed (e.g., C++, Java): You must declare the data type of the variable upfront. The type is checked at compile-time.
  • Dynamically Typed (e.g., Python, JavaScript): The data type is inferred automatically at runtime based on the assigned value.
  • Strongly vs. Weakly Typed: Strongly typed languages enforce strict type rules (e.g., you cannot add an integer to a string), while weakly typed languages perform implicit type conversions.

4.2 Common Data Types

Fundamental Data Types

  • Integer (int): Whole numbers without decimals (e.g., 10, -5, 0).
  • Floating Point (float/double): Numbers with decimal points (e.g., 3.14, -0.01). Stored using IEEE 754.
  • Character (char): Single letter, number, or symbol (e.g., 'a', '7', '$'). Stored using ASCII/Unicode.
  • Boolean (bool): Represents truth values: True (1) or False (0).
  • String: A sequence of characters (text) (e.g., "Hello World"). Note: In some languages like C, strings are technically arrays of characters.
Key Takeaways
  • Variables act as named memory locations to store and manipulate data dynamically during program execution.
  • Fundamental data types define the kind of data stored, such as integers (int), decimals (float/double), characters (char), and truth values (bool).
  • Languages differ in typing systems: static vs. dynamic, strong vs. weak.

5. Operators

Operators are special symbols that perform operations on variables and values.

5.1 Arithmetic Operators

Used for mathematical calculations.

Arithmetic Rules

  • Addition (+): 5 + 3 = 8
  • Subtraction (-): 5 - 3 = 2
  • Multiplication (*): 5 * 3 = 15
  • Division (/): 10 / 2 = 5 (In languages like Python 3, / always returns a float. In C++, integer division truncates decimals).
  • Modulus (%): Returns the remainder of division. 10 % 3 = 1. Useful for determining even/odd numbers (x % 2 == 0).

5.2 Comparison (Relational) Operators

Used to compare two values. They return a boolean result (True/False).

Comparison Operators

  • Equal to (==): Checks if two values are equal. (Do not confuse with assignment =).
  • Not Equal to (!=): Checks if two values are not equal.
  • Greater Than (>): Checks if the left value is greater than the right.
  • Less Than (<): Checks if the left value is less than the right.
  • Greater/Equal (>=): Checks if left is greater than or equal to right.
  • Less/Equal (<=): Checks if left is less than or equal to right.

5.3 Logical Operators

Used to combine multiple boolean conditions.

Logic Rules

  • AND (&& or and): Returns True if both conditions are True.
  • OR (|| or or): Returns True if at least one condition is True.
  • NOT (! or not): Reverses the boolean value. (True becomes False).

5.4 Assignment and Bitwise Operators

Advanced Operators

  • Assignment (=): Assigns the value on the right to the variable on the left. Compound assignment operators like += (e.g., x += 5 is x = x + 5) are common.
  • Bitwise Operators (&, |, ^, ~, <<, >>): Perform operations directly on the binary bits of integers. Used in low-level programming and optimization.
Key Takeaways
  • Arithmetic operators (+, -, *, /, %) are used to perform mathematical calculations.
  • Relational operators (==, >, <, !=) compare values and return boolean results.
  • Logical operators (AND, OR, NOT) combine multiple conditions to control program flow.

6. Input and Output (I/O)

Programs need to interact with the user or the operating system to be useful.

6.1 Output (Printing)

Displaying information to the screen (standard output), writing to a file, or sending data over a network. Example: print("Hello") in Python or std::cout &lt;&lt; "Hello"; in C++.

6.2 Input (Reading)

Getting information from the user via a keyboard (standard input), reading from a file, or receiving network packets. Example: input() in Python or std::cin &gt;&gt; variable; in C++.
Key Takeaways
  • Output operations (like 'cout' or 'print') display information to the user interface or screen.
  • Input operations (like 'cin' or 'input()') allow programs to receive dynamic data from the user during execution.

Summary

Key Takeaways
  • Source code must be translated into binary via Compilation or Interpretation.
  • Data is fundamentally represented as binary bits; negative numbers use Two's Complement, text uses ASCII/Unicode, and decimals use IEEE 754 Floating-Point.
  • Syntax refers to the grammar of the language, while Semantics refers to the logic/meaning.
  • Variables store data and are defined by specific Data Types (int, float, char, bool) and are governed by the language's typing system.
  • Operators manipulate data arithmetically (+, -, *, /), compare values (==, !=, >), and evaluate logic (AND, OR, NOT).
  • I/O Operations allow dynamic interaction with the user and environment.