The examples of the three fundamental loops (for loop, while loop, do-while loop), nested loops, if-else statements, and nested if-else statements in C++.
1. For Loop:
The for loop is used for iterating a statement or a block of statements a fixed number of times.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of a for loop
cout << "Example of a for loop:" << endl;
for (int i = 1; i <= 5; ++i) {
cout << "Iteration " << i << endl;
}
return 0;
}
2. While Loop:
The while loop executes a block of code as long as a specified condition is true.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of a while loop
cout << "Example of a while loop:" << endl;
int count = 1;
while (count <= 5) {
cout << "Count: " << count << endl;
count++;
}
return 0;
}
3. Do-While Loop:
The do-while loop executes a block of code at least once and then repeats the loop as long as a specified condition is true.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of a do-while loop
cout << "Example of a do-while loop:" << endl;
int num = 1;
do {
cout << "Number: " << num << endl;
num++;
} while (num <= 5);
return 0;
}
4. Nested Loops:
Nested loops are loops within loops, used to perform iterations inside other iterations.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of nested loops
cout << "Example of nested loops:" << endl;
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 2; ++j) {
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
5. If-Else Statement:
The if-else statement is used for decision-making based on conditions.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of if-else statement
int number = 10;
if (number > 0) {
cout << "Number is positive" << endl;
} else {
cout << "Number is non-positive" << endl;
}
return 0;
}
6. Nested If-Else Statement:
Nested if-else statements are if-else statements inside other if-else statements.
```cpp
#include <iostream>
using namespace std;
int main() {
// Example of nested if-else statement
int num = 15;
if (num > 0) {
if (num % 2 == 0) {
cout << "Number is positive and even" << endl;
} else {
cout << "Number is positive and odd" << endl;
}
} else {
cout << "Number is non-positive" << endl;
}
return 0;
}
Explanation:
- Loop Examples: Each loop type (for, while, do-while) demonstrates how to iterate and execute code based on different conditions.
- Nested Loops: Illustrates how loops can be nested inside each other to perform complex iterations.
- If-Else Statements: Showcases decision-making based on conditions using if-else statements.
- Nested If-Else Statements: Demonstrates if-else statements nested within each other to make more intricate decisions based on conditions.
These examples showcase the use of loops and conditional statements in C++ and how they can be utilized to control the flow of execution and make decisions based on specific conditions. Adjust the conditions and operations inside loops or if-else statements as needed for various scenarios.
0 Comments