CSEN102 Introduction to Computer Science: Lecture 3
This lecture for CSEN102: Introduction to Computer Science, given by Milad Ghantous, PhD, focuses on fundamental programming concepts.
3.1 Basic Programming Concepts
Programming involves writing instructions that a computer can understand and execute to perform a specific task. To do this, we use programming languages, which have their own syntax and rules.
1. Variables:
Variables are named storage locations in a computer's memory used to hold data. The value stored in a variable can change during program execution.
- Declaration: Giving a name and a data type to a variable.
- Initialization: Assigning an initial value to a variable.
- Assignment: Changing the value of a variable after it has been initialized.
Example (Conceptual):
int age; // Declaration: 'age' is an integer variable
age = 30; // Assignment: 'age' is now 30
float price = 99.99; // Declaration and Initialization: 'price' is a float, set to 99.99
2. Data Types:
Data types classify the kind of values that a variable can hold and the operations that can be performed on them. Common data types include:
- Integers (e.g., `int`): Whole numbers (e.g., 5, -100).
- Floating-point Numbers (e.g., `float`, `double`): Numbers with decimal points (e.g., 3.14, -0.5).
- Characters (e.g., `char`): Single letters, symbols, or numbers (e.g., 'A', '$', '7').
- Strings (e.g., `string`): Sequences of characters (e.g., "Hello World", "CSEN102").
- Booleans (e.g., `bool`): Represents true or false values.
3. Operators:
Operators are symbols that perform operations on values and variables.
- Arithmetic Operators: For mathematical calculations (+, -, *, /, %).
- Assignment Operators: For assigning values (=, +=, -=, etc.).
- Comparison/Relational Operators: For comparing values (==, !=, <, >, <=, >=). These typically result in a Boolean (true/false) value.
- Logical Operators: For combining Boolean expressions (AND, OR, NOT).
4. Input and Output (I/O):
How programs interact with the user or external systems.
- Input: Getting data from the user or a file (e.g., `cin` in C++, `input()` in Python).
- Output: Displaying information to the user or writing to a file (e.g., `cout` in C++, `print()` in Python).
Example (Conceptual):
// Input
int num;
cout << "Enter a number: "; // Prompt
cin >> num; // Read input
// Output
cout << "You entered: " << num << endl;
3.2 Program Structure (Conceptual)
While specific syntax varies, most programs follow a general structure:
- Header/Includes: Importing necessary libraries or modules.
- Main Function/Block: The starting point of program execution.
- Declarations: Defining variables and functions.
- Statements: The instructions that perform the actual work.
- Comments: Explanatory notes within the code, ignored by the compiler/interpreter. Essential for code readability and maintenance.
3.3 Error Types in Programming
Understanding different types of errors is crucial for debugging and writing robust programs.
- Syntax Errors: Violations of the programming language's rules (grammar). The program will not compile or run until these are fixed. Examples: missing semicolons, misspelled keywords.
- Runtime Errors: Errors that occur while the program is executing. The program might compile but crash during execution. Examples: division by zero, accessing an invalid memory location.
- Logical Errors: The program runs and compiles without crashing, but it produces incorrect output because of a flaw in the algorithm's logic. These are often the hardest to find. Example: incorrect formula in a calculation.
Thank you