Answer the question

Answer the question

by Md Assaduzzaman -
Number of replies: 43

Write down the difference between For, While and do while loop with code.

In reply to Md Assaduzzaman

Re: Answer the question

by Lauhe Mafuj Udoy -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then.
#include<stdio.h>
int main(){
int i = 0;

while (i < 5) {
  printf("%d\n", i);
  i++;
}

#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}




#include <stdio.h>

int main() {
    int i = 0;
    do {
        printf("%d\n", i);
        i++;
    } while (i < 5);
    return 0;
}


#include <stdio.h>

int main() {
    for (int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}


In reply to Md Assaduzzaman

Re: Answer the question

by Parvez Mosharaf Siam -
For Loop: includes initialization, condition-checking, and iteration expression all in one line.

#include
int main() {
int i;
for (i = 0; i < 5; i++) {
printf(" %d\n", i);
}
return 0;
}

The while loop is used when the number of iterations is not known ahead of time, and the loop continues as long as the specified condition remains true.

#include
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}

The do-while loop guarantees that the code block will execute at least once because the condition is evaluated after the execution of the loop body.
#include
int main() {
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Abdur Rouf Rokon -
Here is the comparison of for, while, and do while loops with code examples:

for loop: Executes a block of code a specific number of times & we can place initial value,conditions & incrimination/discrimination inside this one loop.

for (int i = 0; i < 5; i++) printf("%d ", i);

while loop: Continues to execute a block of code as long as the condition is true.

int i = 0; while (i < 5) { printf("%d ", i); i++; }

do while loop: Executes a block of code at least once, then continues as long as the condition is true.

int i = 0; do { printf("%d ", i); i++; } while (i < 5);
In reply to Md Assaduzzaman

Re: Answer the question

by MD Rokonuzzaman Rokon -
for Loop: Best when the number of iterations is known or can be determined by iterating over a sequence. Executes a block of code a specific number of times.
while Loop: Best when the number of iterations is not known ahead of time and depends on a condition. Executes a block of code as long as the condition is true.
do while Loop: Executes a block of code once before checking the condition, then continues to execute as long as the condition remains true. Not natively available in Python but available in other languages like C.


#while loop code: int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}


#do while code: int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);


#for loop code: int i;

for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md Shadik Istiak -
The for loop is used when you know in advance how many times you want to execute a block of code. It includes initialization, a condition, and an increment/decrement expression.
Example Code:
#include int main() {
// This will print numbers from 0 to 4
for (int i = 0; i < 5; i++) {
printf("Number: %d\n", i);
}
return 0;
}


The while loop is used when you do not know in advance how many times you need to execute a block of code. It checks the condition before each iteration.
Example Code:
#include int main() {
int i = 0;
// This will print numbers from 0 to 4
while (i < 5) {
printf("Number: %d\n", i);
i++; // Increment i
}
return 0;
}


The do-while loop ensures that the code block is executed at least once because it checks the condition after executing the code block.
Example Code:
#include int main() {
int i = 0;
// This will print numbers from 0 to 4
do {
printf("Number: %d\n", i);
i++; // Increment i
} while (i < 5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by MD MARUF HASAN -
#for Loop: Ideal when the number of iterations is known. Combines initialization, condition, and increment in one lin

int i;

for (i = 0; i < 5; i++) {
printf("%d\n", i);
}

#while Loop: Ideal when the number of iterations is not known and depends on a condition. The condition is checked before the loop executes.
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}


#do while Loop: Similar to while, but ensures the loop body is executed at least once. The condition is checked after the loop body executes.
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
In reply to Md Assaduzzaman

Re: Answer the question

by Niamur Rashid Shihab -
While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true.

//While loop
#include
int main() {
int i = 1;

while (i <= 10) {
printf("%d\n", i);
i++;
}

return 0;
}

//do while
#include
int main() {
int i = 1;

do {
printf("%d\n", i);
i++;
}
while (i <= 10);

return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Maiesha Rahman -
Summary of Differences:
- **For Loop:** Best when you know in advance how many times the loop should run.
- **While Loop:** Best when the loop should run as long as a condition is true, but you might not know in advance how many times it will run.
- **Do-While Loop:** Guarantees the loop runs at least once, regardless of the condition. Condition is checked after the loop's first execution.
1. **For Loop**
- A `for` loop is used when the number of iterations is known beforehand.
**Example:**

#include int main() {
    for (int i = 0; i < 5; i++) {
        Printf("%d",I);
    }
    return 0;
}

**Output:**
```
0 1 2 3 4
```

### 2. **While Loop**
- A `while` loop is used when the number of iterations is not known and depends on a condition.
**Example:**
`
#include

int main() {
    int i = 0;
    while (i < 5) {
        Printf("%d",I);
        i++;
    }
    return 0;
}
```
**Output:**
```
0 1 2 3 4
3. **Do-While Loop**
- A `do-while` loop is similar to a `while` loop, but the condition is checked **after** the loop body has executed.
-
**Example:**

#include
int main() {
    int i = 0;
    do {
        Printf("%d",i);
        i++;
    } while (i < 5);
    return 0;
}
```
**Output:**
```
0 1 2 3 4
In reply to Md Assaduzzaman

Re: Answer the question

by Nafis Rizone -
1. for Loop
The for loop is generally used when you know the number of iterations in advance. It combines initialization, condition-checking, and iteration in a single line.
for (initialization; condition; iteration) {
// Code to be executed
}

2. while Loop
The while loop is used when the number of iterations is not known beforehand and depends on a condition being true.
#include int main(){
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}

3. do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the loop body is executed at least once because the condition is checked after the loop body.
#include int main(){
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
return 0;}
In reply to Md Assaduzzaman

Re: Answer the question

by Sakibul islam Roni -
For loop
For-loop functions by running a section of code repeatedly until a certain condition has been satisfied.
Code:
#include
int main() {
int i;

for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}

While loop:
while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
Code:
#include int main() {
int i = 1;

while (i <= 5) {
printf("%d\n", i);
++i;
}

return 0;
}

Do while loop:
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.
Code:

#include int main() {
double number, sum = 0;
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0);

printf("Sum = %.2lf",sum);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Jannat Rayhana Tarin -
the difference between for ,while and do while loop is in for loop the initialization, condition and increment/decrement is written in the loop. In while loop it consist of the condition only where as in do while loop it executes at least once one statement because the condition is checked after the loop body executes.
for loop :
#include<stdio.h>
  int main(){
int i,sum;
for(i=0;i>10;i++){
{

printf("%d\n",i);
}
}
return 0;
}


while loop :
#include main(){
int i=0;
while(i<5){
printf("%d\n",i);
i++;
}
return 0;
}

do while:
#include
int main() {
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);

return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Ahnab Shariar -
The difference between For, While and do while loop with code:

For loop: The for loop is typically used when the number of iterations is known beforehand. It includes initialization, condition checking, and iteration in one line.

#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}


While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.

#include <stdio.h>
int main()

{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}



Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.

#include <stdio.h>
int main()

{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md Faisal Hossain -
// for loop
for( i=0; i<5; i++)
{
printf("%d",i);
}
return 0;


// while loop

int i=0;
while(i<5)
{
printf("%d",i);
i++;
}
return 0;


// do while loop

int i=0;
do
{
printf ("%d",i);
i++;
}
while(i<5);
return 0;
In reply to Md Assaduzzaman

Re: Answer the question

by Mridul Barmon -
//for loop
for (i=0;i<5; i++){
printf("%d",i)
}
return0;
}
//while loop

int i=0
while (i<5)
{
printf ("%d",i);
i++;
}
return 0;
}

//do while loop

int i =0
do
{

printf ("%d",i);
i++;
}
while(i<5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Raisa Rahman -
For,while and do while loops work same,but the difference is,.
For loop the initialization,condition and incerment written in the loop,while loop is consists of the condition only where as do while loop it execites at least one one statement becuse the condition is checked after the loop body executes.
For loop,
#include Int main(){
Int i;
For(i=5;i>0;i --){
Printf("%d/n",i);
}
return 0;
}



While loop,
#inclide Int main(){
Int i=0;
While (i<5){
Printf("%d\n",i);
i++;
}
Return 0
Do while,

In reply to Md Assaduzzaman

Re: Answer the question

by Omar Bin Osman -
//for loop
for (i=0;i<5; i++)
{
printf("%d",i)
}
return0:
}
//while loop

int i=0
while (i>5)
{
printf("%d",i);
i++;
}
return 0;
}

//do while loop

int i =0
do
{

printf("%d",i);
i++;
}
while 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Most Juwayriah -
The difference between For , While and do while loop the is in for loop the initialization, condition and increment/decrement is written in the loop. in while loop, it consist of the condition only where as in do while loop it executes at least once one statement because the condition is checked in after the loop body executes
# include <stdio.h>
  int main ( ) {
int i;
for ( i = 5 ; i > 0 ; i--){
printf ("%d\n", i );
}
return 0;

# include <stdio.h>
   main ( )
int i = 0
while (i<5){
printf ("%d\n",i);
i++;
}
return 0;
}
#include <stdio.h>
  int main ( ) {
int i = 0;
do {
printf ("%d\n", i);
i++;
}
while (i < 5);

return 0 ;
In reply to Md Assaduzzaman

Re: Answer the question

by MD. Sifatur Rahman -
In C programming, `for`, `while`, and `do while` are looping constructs used to execute a block of code multiple times, but they each have different syntax and use cases. Here's a breakdown of their differences:

### 1. **for Loop**

The `for` loop is typically used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and increment/decrement.

**Syntax:**
```c
for (initialization; condition; update) {
// Code to be executed
}
```

**Example:**
```c
#include
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
```
*This will print numbers from 0 to 4.*

### 2. **while Loop**

The `while` loop is used when the number of iterations is not known beforehand and the loop should continue as long as a certain condition is true. The condition is checked before the loop executes.

**Syntax:**
```c
while (condition) {
// Code to be executed
}
```

**Example:**
```c
#include
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}
```
*This will also print numbers from 0 to 4.*

### 3. **do while Loop**

The `do while` loop is similar to the `while` loop, but it guarantees that the body of the loop will execute at least once, as the condition is checked after the loop execution.

**Syntax:**
```c
do {
// Code to be executed
} while (condition);
```

**Example:**
```c
#include
int main() {
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by sharaban tohora litha -
A for loop is used when the number of iterations is known.A while loop runs as long as a condition is true.A do while loop runs at least once and then continues if a condition is true.


for loop
#include int main(){
int i;
for(i=0; i<5; i++){
printf("%d\n",i);
}
return 0;
}




while loop
#include int main(){
int i=0;
while(i<5){
printf("%d\n",i);
i++;
}
return 0;
}

do while loop
#include int main(){
int i=0;
do{
printf("%d\n",i);
i++;
}
while(i<5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Al-Amin Khan -
for Loop: Best when the number of iterations is known or can be determined by iterating over a sequence. Executes a block of code a specific number of times.
while Loop: Best when the number of iterations is not known ahead of time and depends on a condition. Executes a block of code as long as the condition is true.
do while Loop: Executes a block of code once before checking the condition, then continues to execute as long as the condition remains true. Not natively available in Python but available in other languages like C.

#for loop code: int i;

for (i = 0; i < 5; i++) {
printf("%d\n", i);
}



#while loop code: int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}


#do while code: int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
In reply to Md Assaduzzaman

Re: Answer the question

by Ali Jahan Riashad -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true.
In reply to Ali Jahan Riashad

Re: Answer the question

by Ali Jahan Riashad -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true.


for loop
#include int main(){
int i;
for(i=0; i<5; i++){
printf("%d\n",i);
}
return 0;
}




while loop
#include int main(){
int i=0;
while(i<5){
printf("%d\n",i);
i++;
}
return 0;
}

do while loop
#include int main(){
int i=0;
do{
printf("%d\n",i);
i++;
}
while(i<5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md. Fahim Shariar -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then.
#include int main(){
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}

#include
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}




#include
int main() {
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
return 0;
}


#include
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Mustafizur Rahman Dip -
For loop
For-loop functions by running a section of code repeatedly until a certain condition has been satisfied.
Code:
#include
int main() {
int i;

for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}

While loop:
while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
Code:
#include int main() {
int i = 1;

while (i <= 5) {
printf("%d\n", i);
++i;
}

return 0;
}

Do while loop:
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.
Code:

#include int main() {
double number, sum = 0;
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0);

printf("Sum = %.2lf",sum);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Ankur Saha -
In C, for, while, and do-while loops are used to repeat a block of code, but they have different structures and use cases. Here's a comparison along with code examples:

1. for Loop
Structure: The for loop is typically used when the number of iterations is known beforehand. It includes three parts: initialization, condition, and iteration expression.

Syntax:

c
Copy code
for (initialization; condition; iteration) {
// Code to be executed
}
Example:

c
Copy code
#include
int main() {
printf("For loop example:\n");
for (int i = 0; i < 5; ++i) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Explanation:

Initialization: int i = 0; sets up the loop counter.
Condition: i < 5; is checked before each iteration; the loop continues as long as this condition is true.
Iteration: ++i; updates the counter after each iteration.
2. while Loop
Structure: The while loop is used when the number of iterations is not known beforehand and depends on a condition. It checks the condition before each iteration.

Syntax:

c
Copy code
while (condition) {
// Code to be executed
}
Example:

c
Copy code
#include
int main() {
int i = 0;
printf("While loop example:\n");
while (i < 5) {
printf("%d ", i);
++i; // Update the loop variable
}
printf("\n");
return 0;
}
Explanation:

Condition: i < 5 is checked before the loop body executes.
Update: The loop variable i needs to be updated within the loop to eventually terminate the loop.
3. do-while Loop
Structure: The do-while loop is similar to the while loop, but it guarantees that the loop body executes at least once because the condition is checked after the loop body.

Syntax:

c
Copy code
do {
// Code to be executed
} while (condition);
Example:

c
Copy code
#include
int main() {
int i = 0;
printf("Do-while loop example:\n");
do {
printf("%d ", i);
++i; // Update the loop variable
} while (i < 5);
printf("\n");
return 0;
}
Explanation:

Loop Body: Executes at least once regardless of the condition.
Condition: i < 5 is checked after the loop body executes.
Summary
for Loop: Best for a known number of iterations. Initialization, condition, and iteration expression are all in one line.
while Loop: Best when the number of iterations is not known and the condition needs to be checked before each iteration.
do-while Loop: Similar to while, but ensures the loop body executes at least once because the condition is checked after the body.
All three types of loops can be used interchangeably depending on the specific needs of your program.
In reply to Md Assaduzzaman

Re: Answer the question

by Abdus Sami -
#for Loop: Ideal when the number of iterations is known. Combines initialization, condition, and increment in one lin

int i;

for (i = 0; i < 5; i++) {
printf("%d\n", i);
}

#while Loop: Ideal when the number of iterations is not known and depends on a condition. The condition is checked before the loop executes.
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}


#do while Loop: Similar to while, but ensures the loop body is executed at least once. The condition is checked after the loop body executes.
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
In reply to Md Assaduzzaman

Re: Answer the question

by Mohammed Intisher Islam -
The difference between For, While and do while loop with code:

For loop: The for loop is typically used when the number of iterations is known beforehand. It includes initialization, condition checking, and iteration in one line.

#include int main()
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}


While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.

#include int main()

{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}



Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.

#include int main()

{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md sami -
#include #include #include
#define MAX_DIGITS 500

// Function to multiply a large number by an integer
void multiply(int *result, int *size, int number) {
int carry = 0;
for (int i = 0; i < *size; ++i) {
int prod = result[i] * number + carry;
result[i] = prod % 10;
carry = prod / 10;
}
while (carry) {
result[(*size)++] = carry % 10;
carry /= 10;
}
}

// Function to calculate factorial of a number
void factorial(int n) {
int result[MAX_DIGITS];
int size = 1;

memset(result, 0, sizeof(result));
result[0] = 1; // Initial value for factorial of 0 or 1

for (int i = 2; i <= n; ++i) {
multiply(result, &size, i);
}

printf("Factorial of %d is: ", n);
for (int i = size - 1; i >= 0; --i) {
printf("%d", result[i]);
}
printf("\n");
}

int main() {
int number = 50;
factorial(number);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md. Tanvir Islam -
The difference between For, While and do while loop with code:

For loop: The for loop is typically used when the number of iterations is known beforehand. It includes initialization, condition checking, and iteration in one line.

#include int main()
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}


While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.

#include int main()

{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}



Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.

#include int main()

{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by shovon das -
The difference between For, While and do while loop with code:

For loop: The for loop is typically used when the number of iterations is known beforehand. It includes initialization, condition checking, and iteration in one line.

#include int main()
{
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}


While loop: The while loop is used when the number of iterations is not known beforehand and is based on a condition. It checks the condition before executing the loop body.

#include int main()

{
int i;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}



Do while loop: The do-while loop is similar to the while loop but it guarantees that the loop body will be executed at least once, as the condition is checked after the loop body.

#include int main()

{
int i;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5)
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Abdullah Hell Kafi -
Let's break down the differences between for, while, and do-while loops with examples in a common programming language like C++ or Java. The concepts are similar in many languages.

1. for Loop
Structure:

cpp
Copy code
for (initialization; condition; update) {
// Code to execute
}
Explanation:

Initialization: Executed once at the beginning of the loop.
Condition: Evaluated before each iteration; if true, the loop body executes.
Update: Executed after each iteration of the loop body.
Example:

cpp
Copy code
#include
int main() {
for (int i = 0; i < 5; i++) {
std::cout << "i = " << i << std::endl;
}
return 0;
}
Output:

css
Copy code
i = 0
i = 1
i = 2
i = 3
i = 4
2. while Loop
Structure:

cpp
Copy code
while (condition) {
// Code to execute
}
Explanation:

Condition: Evaluated before each iteration; if true, the loop body executes.
Example:

cpp
Copy code
#include
int main() {
int i = 0;
while (i < 5) {
std::cout << "i = " << i << std::endl;
i++;
}
return 0;
}
Output:

css
Copy code
i = 0
i = 1
i = 2
i = 3
i = 4
3. do-while Loop
Structure:

cpp
Copy code
do {
// Code to execute
} while (condition);
Explanation:

Condition: Evaluated after each iteration; if true, the loop body executes again. The body executes at least once regardless of the condition.
Example:

cpp
Copy code
#include
int main() {
int i = 0;
do {
std::cout << "i = " << i << std::endl;
i++;
} while (i < 5);
return 0;
}
Output:

css
Copy code
i = 0
i = 1
i = 2
i = 3
i = 4
Key Differences
Initialization:

for loop: Initialization is part of the loop structure.
while and do-while loops: Initialization must be done separately before entering the loop.
Condition Check:

for loop: Condition is checked before each iteration.
while loop: Condition is checked before each iteration.
do-while loop: Condition is checked after each iteration.
Guarantee of Execution:

for and while loops: If the condition is false initially, the loop body may not execute at all.
do-while loop: The loop body executes at least once because the condition is checked after the execution of the loop body.
These distinctions help in choosing the right type of loop based on the specific needs of your program
In reply to Md Assaduzzaman

Re: Answer the question

by Md sami -
#include #include #include
#define MAX_DIGITS 500

// Function to multiply a large number by an integer
void multiply(int *result, int *size, int number) {
int carry = 0;
for (int i = 0; i < *size; ++i) {
int prod = result[i] * number + carry;
result[i] = prod % 10;
carry = prod / 10;
}
while (carry) {
result[(*size)++] = carry % 10;
carry /= 10;
}
}

// Function to calculate factorial of a number
void factorial(int n) {
int result[MAX_DIGITS];
int size = 1;

memset(result, 0, sizeof(result));
result[0] = 1; // Initial value for factorial of 0 or 1

for (int i = 2; i <= n; ++i) {
multiply(result, &size, i);
}

printf("Factorial of %d is: ", n);
for (int i = size - 1; i >= 0; --i) {
printf("%d", result[i]);
}
printf("\n");
}

int main() {
int number = 50;
factorial(number);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Natasha Zaman -
Different between for,while and do while loop with code :

for Loop: Use when the number of iterations is known beforehand.

while Loop: Use when the number of iterations is not known and depends on a condition, which is checked before each iteration.

do-while Loop: Use when you want to guarantee that the loop body is executed at least once, with the condition checked after each iteration.
In reply to Md Assaduzzaman

Re: Answer the question

by Md. Imran Ali Limon -
2. While Loop:

The while loop is used when the condition is checked before executing the loop. It continues to run as long as the condition is true.


Syntax:

while (condition) {
// Code to be executed
}

Example (C++):

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

3. Do-While Loop:

The do-while loop is similar to the while loop, but the condition is checked after executing the loop body. So, it runs at least once even if the condition is false.


Syntax:

do {
// Code to be executed
} while (condition);
In reply to Md Assaduzzaman

Re: Answer the question

by KRISTI DEBANGONA MITRA -
Here’s a comparison of for, while, and do-while loops in C, along with their examples:

for Loop

for (initialization; condition; update) {
// Code to execute
}
Example:


#include
int main() {
// Print numbers from 0 to 4
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}


while Loop:

while (condition) {
// Code to execute
}
Example:

#include
int main() {
int i = 0;
// Print numbers from 0 to 4
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}

do-while Loop

do {
// Code to execute
} while (condition);
Example:

#include
int main() {
int i = 0;
// Print numbers from 0 to 4
do {
printf("%d\n", i);
i++;
} while (i < 5);
return 0;
}

So ,
for Loop: Ideal for a known number of iterations. Initialization, condition, and update are all handled in the loop header.
while Loop: Suitable for an unknown number of iterations where the condition is checked before the loop body executes.
do-while Loop: Guarantees at least one execution of the loop body, with the condition being checked after the loop body.
These examples in C should help clarify how each loop works and when to use them.
In reply to Md Assaduzzaman

Re: Answer the question

by Sadia Parvez Orni -
Difference between for,while and do while loop with code :
for Loop: Use when the number of iterations is known beforehand.
while Loop: Use when the number of iterations is not known and depends on a condition, which is checked before each iteration.
do-while Loop: Use when you want to guarantee that the loop body is executed at least once, with the condition checked after each iteration.
In reply to Md Assaduzzaman

Re: Answer the question

by Sadia Parvez Orni -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then.

#include
int main(){

int i = 0;

while (i < 5) {
  printf("%d\n", i);
  i++;
}


#include

int main() {

    int i = 0;

    while (i < 5) {

        printf("%d\n", i);

        i++;

    }

    return 0;

}





#include

int main() {

    int i = 0;

    do {

        printf("%d\n", i);

        i++;

    } while (i < 5);

    return 0;

}



#include

int main() {

    for (int i = 0; i < 5; i++) {

        printf("%d\n", i);

    }

    return 0;

}
In reply to Md Assaduzzaman

Re: Answer the question

by AL MUBIN -
For loop:
The for loop combines initialization, condition-checking, and increment/decrement in one line.
Example:
int i;
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
>>THIS CODE WILL PRINT THE NUMBERS 0 TO 4 BY FOR LOOP.

While Loop:
The condition is checked before each iteration.
Example:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
>> THIS CODE WILL RUN, OVER AND OVER AGAIN, AS LONG AS A VARIABLE (I) IS LESS THAN 5.

Do While Loop:
The condition is checked after each iteration.
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
>> THIS CODE WILL ALWAYS BE EXECUTED AT LEAST ONCE, EVEN IF THE CONDITION IS FALSE, BECAUSE THE CODE BLOCK IS EXECUTED BEFORE THE CONDITION IS TESTED.
In reply to Md Assaduzzaman

Re: Answer the question

by Natasha Zaman -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then.

#include
int main(){

int i = 0;

while (i < 5) {
  printf("%d\n", i);
  i++;
}


#include

int main() {

    int i = 0;

    while (i < 5) {

        printf("%d\n", i);

        i++;

    }

    return 0;

}





#include

int main() {

    int i = 0;

    do {

        printf("%d\n", i);

        i++;

    } while (i < 5);

    return 0;

}



#include

int main() {

    for (int i = 0; i < 5; i++) {

        printf("%d\n", i);

    }

    return 0;

}
In reply to Md Assaduzzaman

Re: Answer the question

by Ali Jahan Riashad -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true.



for loop
#include int main(){
int i;
for(i=0; i<5; i++){
printf("%d\n",i);
}
return 0;
}




while loop
#include int main(){
int i=0;
while(i<5){
printf("%d\n",i);
i++;
}
return 0;
}

do while loop
#include int main(){
int i=0;
do{
printf("%d\n",i);
i++;
}
while(i<5);
return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md Ikramul Islam Sami -
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then.
#include int main(){
int i = 0;

 
For Loop:
#include <stdio.h>
   int main() {
   for (int i = 0; i < 5; i++) {
   printf("%d\n", i);
   }
   return 0;
}




while loop:
#include <stdio.h>
   int main() 
{
   int i = 0;
   while (i < 5) {
   printf("%d\n", i);
   i++;
   }
   return 0;
}




Do While Loop:
#include <stdio.h>
   int main() 
{
   int i = 0;
   do {
   printf("%d\n", i);
   i++;
   } 
   while (i < 5);
   return 0;
}



In reply to Md Assaduzzaman

Re: Answer the question

by Mst Humayra Afroz Mim -
C programming has three types of loops:
1. for loop
2. while loop
3. do...while loop

1. The For Loop
The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
Example:
// Print numbers from 1 to 10
#include
int main() {
int i;

for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
2. The While Loop:
The while loop in C is used to evaluate a test condition and iterate over the loop body until the condition returns True. The loop ends when the condition returns False. This loop is also known as a pre-tested loop because it is commonly used when the number of iterations is unknown to the user ahead of time.
Example:
// Print numbers from 1 to 5

#include int main() {
int i = 1;

while (i <= 5) {
printf("%d\n", i);
++i;
}

return 0;
}
3. The 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.
Example :
// Program to add numbers until the user enters zero

#include int main() {
double number, sum = 0;

// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
}
In reply to Md Assaduzzaman

Re: Answer the question

by Md. Imran Ali Limon -
For Loop:

Best used when the number of iterations is known.
Syntax includes initialization, condition, and increment/decrement in one line.
Loop condition is checked before execution.

#include using namespace std;

int main() {

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

While Loop:

Best used when the number of iterations is unknown.
The condition is checked before each iteration.
May not execute at all if the condition is false initially.
#include using namespace std;

int main() {
int i = 0;

while (i < 5) {
cout << i << " ";
i++;
}
return 0;
}

Do-While Loop:

Best used when you need the loop to execute at least once.
The condition is checked after the loop body executes.
Ensures that the loop runs at least once, even if the condition is false initially.
#include using namespace std;

int main() {
int i = 0;

do {
cout << i << " ";
i++;
} while (i < 5);
return 0;
}