Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

Repetitive control structure:


A repetitive control structure is a programming structure that allows the execution of a
set of statements repeatedly until a certain condition is met. The most common
repetitive control structures in C++ are the for loop, the while loop, and the do-while
loop.
Example: A for loop that prints numbers from 1 to 10

css
Copy code
for(int i = 1; i <= 10; i++) {
cout << i << endl;
}
2. Counter control loops:
A counter control loop is a type of repetitive control structure that executes a set of
statements for a specific number of times.
Example: A for loop that prints numbers from 1 to 10

css
Copy code
for(int i = 1; i <= 10; i++) {
cout << i << endl;
}
3. For Loop:
A for loop is a repetitive control structure that allows the execution of a set of statements
repeatedly until a certain condition is met.
Example: A for loop that prints numbers from 1 to 10

css
Copy code
for(int i = 1; i <= 10; i++) {
cout << i << endl;
}
4. Sentinel Control loops:
A sentinel control loop is a type of repetitive control structure that executes a set of
statements until a specific value, known as the sentinel value, is encountered.
Example: A while loop that reads numbers until a sentinel value of -1 is encountered

c
Copy code
int num;
cout << "Enter a number (-1 to stop): ";
cin >> num;

while(num != -1) {
cout << "You entered " << num << endl;
cout << "Enter a number (-1 to stop): ";
cin >> num;
}
5. While loop:
A while loop is a repetitive control structure that executes a set of statements repeatedly
until a certain condition is met.
Example: A while loop that prints numbers from 1 to 10

bash
Copy code
int i = 1;

while(i <= 10) {


cout << i << endl;
i++;
}
6. Do-While Loop:
A do-while loop is a repetitive control structure that executes a set of statements at least
once, and then repeatedly until a certain condition is met.
Example: A do-while loop that reads numbers until a sentinel value of -1 is encountered

c
Copy code
int num;

do {
cout << "Enter a number (-1 to stop): ";
cin >> num;
cout << "You entered " << num << endl;
} while(num != -1);
7. Functions:
Functions are self-contained blocks of code that perform a specific task. They can be
divided into built-in functions, user-defined functions, inline functions, and recursive
functions.
Example: A user-defined function that returns the sum of two numbers

c
Copy code
int sum(int a, int b) {
return a + b;
}

int main() {
int result = sum(5, 10);
cout << "The sum is " << result << endl;
return 0;
}
8. Towers of Hanoi:
The Towers of Hanoi is a classic mathematical puzzle that consists of three rods and a
number of disks of different sizes. The objective is to move the entire stack to another
rod, obeying the following simple rules: only one disk can be moved at a time, each
move consists of taking the upper disk from one of the stacks and placing it on top of
another stack or on an empty rod, and no larger disk may be placed on top of a smaller
disk.
Example: Recursive function to solve the Towers of Hanoi problem

java
Copy code
void hanoi(int n, char source, char auxiliary,

abdullahch7047@gmail.com
please also do remaining definations
!
9. Single Dimensional Arrays:
A single dimensional array is a collection of elements of the same data type that are
accessed using an index. The index starts from 0 and goes up to size-1, where size is
the total number of elements in the array.
Example: Declaration and initialization of a single dimensional integer array

python
Copy code
int arr[5] = {1, 2, 3, 4, 5};
10. Passing as an argument to the function:
In C++, we can pass arrays as arguments to functions. The array is passed as a pointer
to its first element.
Example: Function that takes an array as an argument and returns its sum

11. Merge Sort:


Merge sort is an efficient, general-purpose, comparison-based sorting algorithm. It
works by dividing the unsorted list into n sublists, each containing one element, and
then repeatedly merging sublists to produce new sorted sublists until there is only one
sublist remaining.
Example: Merge sort function that sorts an array in ascending order

12. Defining the Structure:


A structure is a user-defined data type that groups together variables of different data
types under a single name. It is defined using the struct keyword, followed by the name
of the structure and its members.
Example: Defining a structure to represent a student

13. Syntax of the Structure Definition:


The syntax of a structure definition consists of the struct keyword, followed by the name
of the structure, and its members enclosed in braces. Each member is defined by its
data type and name.
Example: Syntax of a structure definition

14. Use of the Structure Definition:


A structure can be used to group related data together and pass it as a single entity to
functions, or to store it in an array or a linked list.
Example: Using a structure to store information about employees and their salaries

15. Accessing Structure Members:


The members of a structure can be accessed using the dot (.) operator.
Example: Accessing the members of a structure

16. Pointers:
A pointer is a variable that stores the memory address of another variable. It is declared
using the * operator.
Example: Declaring and using a pointer variable

You might also like