Key Points:
- Understand the different primitive data types and how to declare and initialize variables.
- Define constants using
const
and#define
. - Perform basic I/O operations using
cin
andcout
. - Utilize arithmetic, relational, logical, bitwise, and assignment operators.
1. Data Types and Variables
A. Primitive Data Types
- int: Integer data type used to store whole numbers.
- Example:
int age = 25;
- char: Character data type used to store single characters.
- Example:
char grade = 'A';
- float: Floating-point data type used to store decimal numbers.
- Example:
float temperature = 23.5f;
- double: Double-precision floating-point data type for larger decimal numbers.
- Example:
double pi = 3.14159;
- bool: Boolean data type used to store true/false values.
- Example:
bool isRaining = false;
B. Variable Declaration and Initialization
- Syntax:
data_type variable_name = value;
- Example:
cpp int number = 10; char letter = 'B'; float price = 9.99f; double distance = 384400.0; bool isAvailable = true;
2. Constants and Literals
A. Defining Constants
- const Keyword: Used to declare constant variables whose values cannot be changed.
- Example:
cpp const int MAX_AGE = 100;
B. #define Directive
- Macro Definition: Preprocessor directive to define constant values.
- Example:
cpp #define PI 3.14159
C. Literals
- Integer Literals: Numeric values without decimal points.
- Example:
42
- Floating-point Literals: Numeric values with decimal points.
- Example:
3.14
- Character Literals: Single characters enclosed in single quotes.
- Example:
'A'
- String Literals: Sequence of characters enclosed in double quotes.
- Example:
"Hello, World!"
- Boolean Literals:
true
orfalse
.
3. Basic I/O Operations
A. Input Using cin
- Syntax:
cin >> variable;
- Example:
cpp int age; cout << "Enter your age: "; cin >> age;
B. Output Using cout
- Syntax:
cout << value;
- Example:
cpp cout << "Hello, World!" << endl;
C. Formatting Output
- End of Line: Use
endl
to insert a newline. - Example:
cout << "Hello" << endl;
- Multiple Outputs: Chain multiple
<<
operators. - Example:
cpp int a = 10, b = 20; cout << "a = " << a << ", b = " << b << endl;
4. Operators
A. Arithmetic Operators
- Addition:
+
- Example:
int sum = 5 + 3;
- Subtraction:
-
- Example:
int difference = 5 - 3;
- Multiplication:
*
- Example:
int product = 5 * 3;
- Division:
/
- Example:
int quotient = 6 / 3;
- Modulus:
%
- Example:
int remainder = 5 % 3;
B. Relational Operators
- Equal to:
==
- Example:
bool isEqual = (5 == 3);
- Not equal to:
!=
- Example:
bool isNotEqual = (5 != 3);
- Greater than:
>
- Example:
bool isGreater = (5 > 3);
- Less than:
<
- Example:
bool isLess = (5 < 3);
- Greater than or equal to:
>=
- Example:
bool isGreaterOrEqual = (5 >= 3);
- Less than or equal to:
<=
- Example:
bool isLessOrEqual = (5 <= 3);
C. Logical Operators
- Logical AND:
&&
- Example:
bool result = (5 > 3) && (5 < 10);
- Logical OR:
||
- Example:
bool result = (5 > 3) || (5 < 2);
- Logical NOT:
!
- Example:
bool result = !(5 > 3);
D. Bitwise Operators
- AND:
&
- Example:
int result = 5 & 3;
- OR:
|
- Example:
int result = 5 | 3;
- XOR:
^
- Example:
int result = 5 ^ 3;
- NOT:
~
- Example:
int result = ~5;
- Left Shift:
<<
- Example:
int result = 5 << 1;
- Right Shift:
>>
- Example:
int result = 5 >> 1;
E. Assignment Operators
- Assignment:
=
- Example:
int a = 5;
- Add and Assign:
+=
- Example:
a += 3;
- Subtract and Assign:
-=
- Example:
a -= 3;
- Multiply and Assign:
*=
- Example:
a *= 3;
- Divide and Assign:
/=
- Example:
a /= 3;
- Modulus and Assign:
%=
- Example:
a %= 3;
Practice Task:
- Write a program to take two integer inputs from the user and perform all arithmetic operations on them.
- Experiment with relational and logical operators to compare the inputs and print the results.