In this tutorial, you will learn lớn create while and do...while loop in C programming with the help of examples.

Bạn đang xem: Do while in c

In programming, loops are used khổng lồ repeat a block of code until a specified condition is met.

C programming has three types of loops.

for loopwhile loopdo...while loop

In the previous tutorial, we learned about for loop. In this tutorial, we will learn about while & do..while loop.

while loop

The syntax of the while loop is:

while (testExpression) // the toàn thân of the loop

How while loop works?

The while loop evaluates the testExpression inside the parentheses ().If testExpression is true, statements inside the toàn thân of while loop are executed. Then, testExpression is evaluated again.The process goes on until testExpression is evaluated to lớn false.If testExpression is false, the loop terminates (ends).To learn more about chạy thử expressions (when testExpression is evaluated to true & false), kiểm tra out relational & logical operators.

Flowchart of while loop

Working of while loop

Example 1: while loop

// Print numbers from 1 lớn 5#include int main() { int i = 1; while (i Output

12345Here, we have initialized i lớn 1.

When i = 1, the chạy thử expression i is true. Hence, the toàn thân of the while loop is executed. This prints 1 on the screen và the value of i is increased khổng lồ 2.Now, i = 2, the demo expression i is again true. The body toàn thân of the while loop is executed again. This prints 2 on the screen & the value of i is increased lớn 3.This process goes on until i becomes 6. Then, the test expression i will be false and the loop terminates.

Xem thêm: Cách Vẽ Con Mèo Bằng Bút Chì, Cách Vẽ Anime Hình Con Mèo Đáng Yêu

do...while loop

The do..while loop is similar khổng lồ the while loop with one important difference. The toàn thân of do...while loop is executed at least once. Only then, the test expression is evaluated.

The syntax of the do...while loop is:

do // the body of the loopwhile (testExpression);

How do...while loop works?

The toàn thân of do...while loop is executed once. Only then, the testExpression is evaluated.If testExpression is true, the body of the loop is executed again and testExpression is evaluated once more.This process goes on until testExpression becomes false.If testExpression is false, the loop ends.

Flowchart of do...while Loop

Working of do...while loop

Example 2: do...while loop

// Program to địa chỉ cửa hàng 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;Output

Enter a number: 1.5Enter a number: 2.4Enter a number: -3.4Enter a number: 4.2Enter a number: 0Sum = 4.70Here, we have used a do...while loop to prompt the user to enter a number. The loop works as long as the input đầu vào number is not 0.

The do...while loop executes at least once i.e. The first iteration runs without checking the condition. The condition is checked only after the first iteration has been executed.

do printf("Enter a number: "); scanf("%lf", &number); sum += number;while(number != 0.0);So, if the first input đầu vào is a non-zero number, that number is added lớn the sum variable và the loop continues lớn the next iteration. This process is repeated until the user enters 0.

Xem thêm: Cách Làm Sao Để Mông Nhỏ Lại Cho Nữ Nhanh Nhất Giúp Vòng 3 Săn

But if the first input is 0, there will be no second iteration of the loop and sum becomes 0.0.