Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Day 5 content of do-while loop , for loop. #187

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions docs/day-05/do-while-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,72 @@ sidebar_label: "While and Do-While Loop"
slug: while-and-do-while-loop-in-cpp
---

TASK:
## 1. C++ while and do...while Loop

1. C++ while and do...while Loop
2. C++ while Loop
3. Flowchart of while Loop
4. Example 1: Display Numbers from 1 to 5
5. C++ do...while Loop
6. Flowchart of do...while Loop
7. Example 2: Display Numbers from 1 to 5
The `while`loop loops through a block of code as long as a specified condition is true:

The `do/while` loop is a variant of the `while` loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

IMAGE FILE:
![Do While Loop](../../static/img/day-05/while-do-while-loop-in-cpp.png)

## 2. C++ while Loop

The `while`loop loops through a block of code as long as a specified condition is true:

### Syntax
```cpp
while (condition) {
// code block to be executed
}

```

## 3. Example 1: Display Numbers from 1 to 5

The program uses a while loop to print numbers from 1 to 5 to the console.
```cpp
#include <iostream>

using namespace std;

int main() {
int i = 1;
while (i <= 5) {
cout << i << endl;
++i;
}
return 0;
}

```
## 4. C++ do...while Loop

The `do/while` loop is a variant of the `while` loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

### Syntax
```cpp
do {
// code block to be executed
}
while (condition);
```

## 5. Example 2: Display Numbers from 1 to 5

The program uses a do-while loop to print numbers from 1 to 5 to the console.

```cpp
#include <iostream>

using namespace std;

int main() {
int i = 1;
do {
cout << i << endl;
++i;
} while (i <= 5);
return 0;
}
```
89 changes: 84 additions & 5 deletions docs/day-05/for-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,90 @@ slug: for-loop-in-cpp

TASK:

1. What is the For Loop in C++?
2. Explain the Syntax of For Loop in C++.
3. C++ for Loop Example
4. C++ Infinite for Loop
5. C++ Nested for Loop
## 1. What is the For Loop in C++?

A For loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line.

### Syntax:
```cpp
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
```
IMAGE FILE:
![For Loop](../../static/img/day-05/for-loop-in-cpp.png)

## 2. Explain the Syntax of For Loop in C++.

### Syntax:
```cpp
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
```
Here,

Initialization – this expression initializes variable(s) and is executed only once.

Condition – if the specified test condition is True, the body of the for loop is executed. In case the test condition returns False, the for loop is terminated.

Update – this expression updates the value of initialized variables and then the condition is evaluated again.

## 3. C++ for Loop Example

The program uses a for loop to print "Hello World!" five times to the console.

## Example
```cpp

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
```

## 4. C++ Infinite for Loop

The program uses a for loop without any conditions, which creates an infinite loop that continuously prints "This loop will run forever." to the console.

## Example
```cpp
#include <iostream>

using namespace std;

int main() {
for (;;) {
cout << "This loop will run forever." << endl;
}
return 0;
}

```
## 5. C++ Nested for Loop
The program uses nested for loops to iterate through values of i from 1 to 3 and j from 1 to 2, printing each combination to the console.

## Example
```cpp
#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 2; ++j) {
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
21 changes: 0 additions & 21 deletions docs/day-05/if-else-statement.md

This file was deleted.

Loading