محاضرة 8
Complete Review - C++ Final Revision
مراجعة شاملة لكل موضوعات C++ من البداية حتى النهاية
ملخص المحاضرة
📜 Lecture 8: Complete Review - C++ Final Revision
This is a comprehensive review of all C++ topics covered in the course. Use this as your final preparation guide.
1. Basic Program Structure
#include <iostream> using namespace std; int main() { // Your code here return 0; }
#include <iostream>: Header for input/outputusing namespace std;: Allows usingcoutandcinwithoutstd::main(): Entry point of every C++ program
2. Input/Output
- Output:
cout << "Hello" << endl; - Input:
cin >> variable; - New Line:
endlor\n
3. Data Types
| Type | Description | Example |
|---|---|---|
int | Whole numbers | int x = 10; |
float | Decimal numbers (7 digits) | float y = 3.14f; |
double | Decimal numbers (15 digits) | double z = 3.14159; |
char | Single character | char c = 'A'; |
bool | True/False | bool flag = true; |
string | Text | string name = "Ali"; |
4. Operators
Arithmetic Operators
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)
Comparison Operators
==Equal to!=Not equal to>Greater than<Less than>=Greater than or equal<=Less than or equal
Logical Operators
&&AND (both conditions must be true)||OR (at least one condition must be true)!NOT (inverts the condition)
Increment/Decrement
- Prefix (
++x,--x): Increment/decrement before using the value - Postfix (
x++,x--): Use the value then increment/decrement
Example:
int x = 5; cout << x++; // Prints 5, then x becomes 6 cout << ++x; // x becomes 7, then prints 7
5. Conditional Statements
if Statement
if (condition) { // code if true }
if-else Statement
if (condition) { // code if true } else { // code if false }
if-else if-else
if (condition1) { // code } else if (condition2) { // code } else { // code }
Checking Even/Odd
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
6. Loops
for Loop
for (initialization; condition; increment/decrement) { // code } // Example: for (int i = 0; i < 5; i++) { cout << i << " "; // Prints: 0 1 2 3 4 }
while Loop
while (condition) { // code } // Example: int i = 0; while (i < 5) { cout << i << " "; i++; }
do-while Loop
do { // code (executes at least once) } while (condition); // Example: int i = 0; do { cout << i << " "; i++; } while (i < 5);
Loop Control
- break: Exit the loop immediately
- continue: Skip current iteration, move to next
Example:
for (int i = 0; i < 10; i++) { if (i == 5) break; // Stops at 5 if (i == 3) continue; // Skips 3 cout << i << " "; // Prints: 0 1 2 4 }
7. Arrays (1D)
Declaration
int arr[5]; // Declare with size int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize int arr[] = {1, 2, 3, 4, 5}; // Size determined automatically
Accessing Elements
- Indexing starts at 0
arr[0]is the first elementarr[4]is the last element (for size 5)
Looping Through Array
int arr[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) { cout << arr[i] << " "; }
Reading Array from User
int arr[5]; for (int i = 0; i < 5; i++) { cin >> arr[i]; }
8. Arrays (2D)
Declaration
int matrix[3][4]; // 3 rows, 4 columns int matrix[2][2] = {{1, 2}, {3, 4}};
Accessing Elements
int matrix[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; cout << matrix[1][1]; // Prints 4
Nested Loops
// Reading 2D array for (int i = 0; i < 3; i++) { // Rows for (int j = 0; j < 3; j++) { // Columns cin >> matrix[i][j]; } } // Printing 2D array for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << matrix[i][j] << " "; } cout << endl; // New line after each row }
9. Pointers
Declaration
int* ptr; // Pointer to integer double* dptr; // Pointer to double
Address-of Operator (&)
int x = 10; int* ptr = &x; // ptr stores the address of x
Dereference Operator (*)
int x = 10; int* ptr = &x; cout << *ptr; // Prints 10 (value of x) *ptr = 20; // Changes x to 20
NULL Pointer
int* ptr = nullptr; // Safe initialization
Pointers and Arrays
int arr[3] = {10, 20, 30}; int* ptr = arr; cout << *ptr; // Prints 10 cout << *(ptr + 1); // Prints 20 cout << *(ptr + 2); // Prints 30
10. Operator Precedence (Priority)
- Parentheses
() - Unary operators
++,--,! - Multiplication/Division/Modulus
*,/,% - Addition/Subtraction
+,- - Comparison
<,>,<=,>= - Equality
==,!= - Logical AND
&& - Logical OR
||
Example: 5 + 3 * 2 = 5 + 6 = 11 (not 16)
Common Patterns and Examples
Find Maximum in Array
int arr[5] = {3, 7, 2, 9, 1}; int max = arr[0]; for (int i = 1; i < 5; i++) { if (arr[i] > max) { max = arr[i]; } } cout << "Max: " << max; // Prints 9
Sum of Array Elements
int arr[5] = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i < 5; i++) { sum += arr[i]; } cout << "Sum: " << sum; // Prints 15
Reverse an Array
int arr[5] = {1, 2, 3, 4, 5}; for (int i = 4; i >= 0; i--) { cout << arr[i] << " "; // Prints: 5 4 3 2 1 }
Count Positive and Negative
int count_pos = 0, count_neg = 0; int num; while (true) { cin >> num; if (num == 0) break; if (num > 0) count_pos++; else count_neg++; }
Swap Two Variables Using Pointers
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 10; swap(&x, &y); cout << x << " " << y; // Prints: 10 5 }
Quick Tips for Exam
- Always initialize variables before using them
- Array indices start at 0, not 1
- Remember operator precedence - use parentheses when in doubt
- Prefix vs Postfix:
++xincrements first,x++increments after - Pointers:
&gets address,*gets value - Loops: Use
forwhen you know iterations,whilewhen you don't - 2D Arrays: Outer loop = rows, inner loop = columns
- Check your conditions:
==for comparison,=for assignment - NULL pointers: Always initialize pointers to
nullptr - Test edge cases: Empty arrays, zero values, negative numbers
Study Checklist
- Can you write a basic C++ program structure?
- Do you understand all data types?
- Can you use all arithmetic and logical operators?
- Can you write if-else statements?
- Can you write all three types of loops?
- Do you know when to use break vs continue?
- Can you declare and use 1D arrays?
- Can you work with 2D arrays using nested loops?
- Do you understand pointers and the & and * operators?
- Can you use pointers with arrays?
Good luck with your exam! 🎓
محتويات المحاضرة
اختبر فهمك للمحاضرة
تصحيح فوري بالذكاء الاصطناعي