Comprog2 Notes
Comprog2 Notes
2. Character (char)
result =
o Represents single characters. - Subtraction operand1 -
o Size: 1 byte. operand2;
4. Assignment Operators
Symbo Operator
Syntax
l Name
Symbo Operator
Syntax
l Name
result =
* Multiplication operand1 *
Simple variable =
operand2; =
assignment expression
result =
Addition variable +=
/ Division operand1 / +=
assignment expression
operand2;
Subtraction variable -=
result = -=
Modulus assignment expression
% operand1 %
(Remainder)
operand2;
Multiplication variable *=
*=
assignment expression
2. Relational Operators
Division variable /=
Symbo Operator /=
Syntax assignment expression
l Name
variable
operand1 == Modulo
== Equal to %= %=
operand2 assignment
expression
operand1 !=
!= Not equal to
operand2 5. Increment and Decrement Operators
++ (Increment): Increases the value of the
operand1 > operand by 1.
> Greater than
operand2
-- (Decrement): Decreases the value of the
operand by 1.
operand1 <
< Less than
operand2
6. Bitwise Operators
Greater than operand1 >= Bitwise operators manipulate data at the bit level.
>=
or equal to operand2 Symbol Operator Syntax
name
Bitwise AND result =
& operand1 &
operand2
Less than or operand1 <=
<= ‘ Bitwise OR `result = operand2`
equal to operand2 ‘ operand1
Global Scope
o Definition: Variables declared outside all
functions and blocks have global scope.
o Visibility: They are accessible from
anywhere in the program after their
declaration.
o Lifetime: They exist throughout the entire
execution of the program and are destroyed o You can perform operations on multiple
when the program terminates. variables within expressions. C++ handles
the operations according to the types and
precedence rules.
int main() {
int number = 10;
if (number > 0) {
cout << "The number is positive." << endl;
int main() {
if (score >= 90) {
int number;
cout << "You got an A!" << endl;
cout << "Enter a number: ";
} else if (score >= 80) {
cin >> number;
cout << "You got a B." << endl;
} else if (score >= 70) {
if (number >= 0) {
cout << "You got a C." << endl;
cout << "The number is non-negative." <<
endl; } else if (score >= 60) {
} else { cout << "You got a D." << endl;
cout << "The number is negative." << endl; } else {
} cout << "You got an F." << endl;
return 0; }
} return 0;
Output: "The number is non-negative." or "The }
number is negative."
Output: Based on the input score, the program will
print the corresponding grade.
3. else if Ladder
The else if ladder is used for checking multiple 4. Nested if...else Statements
conditions sequentially. It tests additional conditions
Nested if...else statements are used when one
if the previous ones were false.
decision leads to another, placing an if...else
Syntax: statement inside another.
cpp Syntax:
CopyEdit cpp
if (condition1) { CopyEdit
// Code to execute if condition1 is true if (condition1) {
} else if (condition2) { // Code for condition1
// Code to execute if condition2 is true if (condition2) {
} else { // Code for condition2
// Code to execute if no conditions are true } else {
} // Code if condition2 is false
Example: }
} else { case value1:
// Code if condition1 is false // Code if expression == value1
} break;
Example: case value2:
cpp // Code if expression == value2
CopyEdit break;
#include <iostream> default:
using namespace std; // Code if no case matches
}
int main() { Example:
int number; cpp
cout << "Enter a number: "; CopyEdit
cin >> number; #include <iostream>
using namespace std;
if (number >= 0) {
cout << "The number is non-negative." << int main() {
endl;
int day;
cout << "Enter a number (1-7) for the day of the
if (number == 0) { week: ";
cout << "The number is zero." << endl; cin >> day;
} else {
cout << "The number is positive." << endl; switch (day) {
} case 1: cout << "Sunday" << endl; break;
} else { case 2: cout << "Monday" << endl; break;
cout << "The number is negative." << endl; case 3: cout << "Tuesday" << endl; break;
} case 4: cout << "Wednesday" << endl; break;
return 0; case 5: cout << "Thursday" << endl; break;
} case 6: cout << "Friday" << endl; break;
Output: Depending on the input, the program will case 7: cout << "Saturday" << endl; break;
print whether the number is positive, zero, or
default: cout << "Invalid input!" << endl;
negative.
}
5. switch Statement
return 0;
The switch statement is used to select one of many
possible blocks of code to execute based on the }
value of a single expression. It is an alternative to
Output: Prints the corresponding day based on the
multiple if...else conditions.
input number.
Syntax:
cpp
Key Points:
CopyEdit
if, if...else, else if, and switch statements
switch (expression) { allow decision-making based on conditions.
if and else statements are used for binary using namespace std;
decision-making.
The else if ladder is useful for testing
int main() {
multiple conditions.
for (int i = 1; i <= 5; i++) {
Nested if...else statements allow complex
decision-making processes. cout << "Value of i: " << i << endl;
The switch statement is more efficient when }
comparing a variable to many values.
return 0;
WEEK 5 C++ Loops
}
For Loop in C++
This will print the values of i from 1 to 5.
The for loop is a control flow statement used to
repeat a block of code a specific number of times,
useful when the number of iterations is known While Loop in C++
beforehand.
A while loop repeatedly executes a block of code as
Syntax: long as a specified condition remains true. It's
typically used when the number of iterations is not
cpp
known in advance.
CopyEdit
Syntax:
for (initialization; condition; update) {
cpp
// Statements to be executed
CopyEdit
}
while (condition) {
Components:
// Statements to be executed
1. Initialization: Sets the starting value of the
}
loop control variable. Executed once.
Components:
o Example: int i = 0;
1. Condition: A boolean expression that is
2. Condition: A boolean expression checked
checked before each iteration.
before each iteration. The loop runs if it's
true. o Example: i <= 10;
o Example: i < 10; 2. Loop Body: Executes as long as the
condition is true.
3. Update: Modifies the loop control variable
after each iteration. How it Works:
o Example: i++; 1. Condition is evaluated before each iteration.
If true, the loop body executes.
How it Works:
2. The loop continues until the condition
1. Initialization runs once at the start.
becomes false.
2. The condition is checked before each
Example:
iteration. If true, the loop body executes; if
false, the loop terminates. cpp
Syntax: }
cpp }
#include <iostream> }
using namespace std; cout << endl; // Move to the next line after
inner loop completes
}
int main() {
return 0;
int i = 1;
}
do {
This prints a 3x3 grid of stars.
cout << "Value of i: " << i << endl;
i++; // Incrementing i
Special Value Type Loops in C++
} while (i <= 5);
A special value type loop terminates when a
return 0;
specific "special" or "sentinel" value is encountered.
}
Common Use Case: These loops are used when the
number of iterations is unpredictable, such as
processing user input or data from external sources.
Nested Loop in C++
Example:
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter numbers (enter -1 to stop): ";
while (true) {
cin >> num;
if (num == -1) {
break; // Exit the loop
}
cout << "You entered: " << num << endl;
}
cout << "Loop terminated as -1 was entered." <<
endl;
return 0;
}
The loop stops when the user enters -1.
Key Points:
Sentinel Value: A predefined value (e.g., -1)
that signals the loop to stop.
These loops are often used in situations
where the input is dynamic and
unpredictable.
MIDTERM CopyEdit
WEEK 7 #include <iostream>
Arrays in C++ using namespace std;
📌 Definition
📌 Size Calculation
📌 Accessing Elements
You can calculate array size using sizeof:
cpp
cpp
CopyEdit
CopyEdit
cout << numbers[2]; // Access 3rd element
int numbers[5];
numbers[0] = 100; // Modify 1st element
int size = sizeof(numbers) / sizeof(numbers[0]); //
size = 5
📌 Example Code
cpp 📌 Advantages
✅ Efficient storage and access
✅ Simple and easy to use
Initialization of Multidimensional Arrays
✅ Good for managing lists of data (grades, sales,
temperatures) You can initialize multidimensional arrays at the
time of declaration, either by listing out all the
WEEK 8
elements or specifying the elements for each sub-
What is a Multidimensional Array? array (row).
int matrix[3][3] = {
1 2 3
{1, 2, 3},
4 5 6
{4, 5, 6},
You can also initialize a 2D array without
{7, 8, 9}
specifying all the elements:
};
In this case, the 2D array matrix can be visualized
int matrix[2][3] = {1, 2, 3}; // First row: {1, 2, 3},
as:
Second row: {0, 0, 0}
1 2 3
4 5 6
Accessing Elements in a Multidimensional Array
7 8 9
Each element in a multidimensional array is
Each element is accessed using two indices: one for accessed by specifying the indices for each
the row and one for the column. dimension. For a 2D array, the first index specifies
the row, and the second index specifies the column.
Syntax:
int matrix[3][3] = {
Default Arguments:
C++ allows default values for function parameters.
void display(int x = 10, int y = 20) {
cout << x << " " << y;
}