C++ Basics Notes
🔹 Variables & Constants
Variables
A variable is a container for storing data.
Declaration Syntax:
data_type variable_name;
Example: int age;
Common Data Types:
Data Type Description Example
int Integer (whole numbers) int count = 10;
float Floating point numbers float temp = 36.6;
double Double-precision float double pi = 3.14159;
char Single character char grade = 'A';
bool Boolean (true or false) bool isPassed = true;
Constants
A constant is a fixed value that cannot be changed during the program.
Declaration Syntax:
const data_type name = value;
Example: const float PI = 3.14;
🔹 Input & Output in C++
Input: cin
Used to get input from the user.
Syntax: cin >> variable;
Example:
int age;
cin >> age;
Output: cout
Used to display output.
Syntax: cout << message;
Example:
cout << "Hello, World!";
Note: Include the header file
#include <iostream>
using namespace std;
🔹 Operators in C++
1. Arithmetic Operators
Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
2. Relational Operators
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
3. Logical Operators
Operator Meaning Example
&& Logical AND (a > 0 && b > 0)
` `
! Logical NOT !(a > 0)
4. Assignment Operators
Operator Meaning Example
= Assign value a = 5
+= Add and assign a += 2 (a = a + 2)
-= Subtract and assign a -= 3
*= Multiply and assign a *= 4
/= Divide and assign a /= 2
%= Modulus and assign a %= 2