C++ Program BASICS Revision
✅ First C++ Program
#include <iostream> // Standard I/O library
using namespace std;
int main() { // Main function
cout << "Hello, World!"; // Output statement
return 0; // Return statement
}
🔹 Explanation:
-
#include <iostream>
→ Header file for input/output. -
using namespace std;
→ Allows using standard namespace (removes need forstd::
prefix). -
cout
→ Prints output to console. -
return 0;
→ Indicates successful execution.
✅ Tokens in C++
Tokens are the smallest units in a C++ program.
🔹 Types of Tokens:
-
Keywords → Reserved words (e.g.,
int, return, if, else, while
) -
Identifiers → Names for variables, functions, classes (e.g.,
main, age, total
) -
Constants → Fixed values (
5, 3.14, 'A'
) -
Operators → Symbols for operations (
+ , - , * , /
) -
Special Symbols →
{ } , ; ( ) [ ]
-
Strings → Text enclosed in
""
(e.g.,"Hello"
)
✅ Functions in C++
A function is a block of code designed to perform a specific task.
🔹 Syntax:
return_type function_name(parameters) {
// Function body
}
🔹 Example:
#include <iostream>
using namespace std;
void greet() { // Function without return type
cout << "Welcome to C++ Programming!\n";
}
int main() {
greet(); // Function call
return 0;
}
✅ Scope of Variables in C++
Scope determines where a variable is accessible.
🔹 Types:
-
Local Variable → Declared inside a function, accessible only there.
-
Global Variable → Declared outside functions, accessible everywhere.
-
Static Variable → Retains its value between function calls.
-
Register Variable → Stored in CPU registers (rarely used today).
🔹 Example:
#include <iostream>
using namespace std;
int globalVar = 10; // Global Variable
void func() {
int localVar = 5; // Local Variable
static int staticVar = 1;
staticVar++;
cout << "Local: " << localVar << ", Static: " << staticVar << "\n";
}
int main() {
func();
func();
cout << "Global: " << globalVar << "\n";
return 0;
}
✅ Conditional Statements in C++
Used for decision-making in programs.
🔹 if Statement
int num = 10;
if (num > 0) {
cout << "Positive Number";
}
🔹 if-else Statement
int num = -5;
if (num > 0) {
cout << "Positive";
} else {
cout << "Negative";
}
🔹 else-if Ladder
int marks = 75;
if (marks >= 90) {
cout << "Grade A";
} else if (marks >= 75) {
cout << "Grade B";
} else {
cout << "Grade C";
}
✅ Nested if and switch in C++
🔹 Nested if
int num = 5;
if (num > 0) {
if (num % 2 == 0) {
cout << "Even Positive";
} else {
cout << "Odd Positive";
}
}
🔹 switch Statement
int choice = 2;
switch (choice) {
case 1:
cout << "Option 1";
break;
case 2:
cout << "Option 2";
break;
default:
cout << "Invalid Option";
}
✅ Increment and Decrement Operators in C++
Used to increase or decrease values by 1.
Operator | Meaning | Example |
---|---|---|
++ | Increment by 1 | a++ or ++a |
-- | Decrement by 1 | a-- or --a |
🔹 Example:
int a = 5;
cout << a++ << "\n"; // Post-increment → prints 5, then a = 6
cout << ++a << "\n"; // Pre-increment → a = 7, then prints 7
✅ Operators in C++
🔹 Types of Operators:
-
Arithmetic Operators →
+ , - , * , / , %
-
Relational Operators →
== , != , > , < , >= , <=
-
Logical Operators →
&& , || , !
-
Assignment Operators →
= , += , -= , *= , /=
-
Bitwise Operators →
& , | , ^ , ~ , << , >>
-
Conditional (Ternary) Operator →
condition ? true : false
-
Comma Operator →
,
used to separate expressions. -
sizeof Operator → Returns the size of a variable.
🔹 Example:
int a = 10, b = 20;
cout << "Sum = " << a + b << "\n"; // Arithmetic
cout << (a > b) << "\n"; // Relational
cout << ((a < b) && (b > 15)) << "\n"; // Logical
a += 5; // Assignment
cout << "a = " << a << "\n";
✅ Loops in C++
Loops are used to execute a block of code multiple times.
🔹 Types of Loops:
-
for Loop
-
while Loop
-
do-while Loop
for Loop
while Loop
do-while Loop
🔹 for Loop
Output: 1 2 3 4 5
🔹 while Loop
🔹 do-while Loop
✅ Arrays in C++
An array is a collection of similar data elements stored at contiguous memory locations.
🔹 Declaring an Array
🔹 Initializing an Array
Good to refresh it once !
ReplyDelete