Introduction to Programming - Notes
1. What is Programming?
- Programming is the process of designing and building an executable computer program to
accomplish a specific task.
- Algorithm vs Program:
* Algorithm: Step-by-step solution to a problem.
* Program: Implementation of an algorithm in a programming language.
- Purpose: Automates tasks, solves problems, performs calculations, manages data.
2. Why C++?
- Combines procedural programming and object-oriented programming (OOP).
- Offers high performance and control over system resources.
- Used in software development, games, system programming, and applications.
3. Structure of a C++ Program
#include <iostream> // Preprocessor directive
using namespace std; // Namespace declaration
int main() {
cout << "Hello, World!"; // Output
return 0; // Exit status
}
- Header files: Provide pre-written functions (#include <iostream>).
- Namespace: Avoids naming conflicts (using namespace std).
- Main function: Entry point of the program (int main()).
- Statements: Instructions executed by the program (cout << "Hello, World!";).
- Return statement: Indicates program exit status (return 0;).
4. Basic Input/Output
- Output: Displaying information to the user.
- Input: Receiving data from the user.
Example:
int age;
cin >> age;
5. Variables and Data Types
- Variables: Named storage for data.
- Common Data Types:
* int: integer numbers
* float: decimal numbers
* double: higher precision decimals
* char: single character
* bool: true/false
Example:
int age = 20;
float price = 99.99;
char grade = 'A';
bool isPassed = true;
6. Operators
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, <, >, <=, >=
- Logical: && (AND), || (OR), ! (NOT)
- Assignment: =, +=, -=, *=, /=
7. Steps to Write a Program
1. Analyze the problem and write an algorithm.
2. Convert the algorithm into a C++ program.
3. Compile the program to check for syntax errors.
4. Run/Execute the program.
5. Verify the output and debug if necessary.
8. Characteristics of a Good Program
- Clear and readable code.
- Correct and efficient logic.
- Proper input validation.
- Reusable and modular design.