While And Do...While Loop In C

While And Do…While Loop In C

With the aid of examples, you will learn how to write a while and do…while loop in C programming in this article.

The control flow statement known as a while loop enables code to be performed repeatedly in response to a specified Boolean condition.

The while loop can be viewed as an iterative version of the if statement.

Loops are used in programming to repeatedly run a block of code until a specific condition is satisfied.

There are three varieties of loops in C programming.

  • for loop
  • while loop
  • do…while loop 

We learned about the for loop in the last tutorial. We will study the while and do..while loops in this tutorial.

while loop(while and do…while Loop)

The while loop’s syntax is as follows:

while (testExpression) {
  // the body of the loop 
}

How does the while loop operate?

  • The while loop assesses the parenthesized test expression (). The statements contained within the while loop’s body are carried out if the test expression is true. Then test expression is assessed once more.
  • Up until the test, expression is judged to be false, and the process continues.
  • The loop finishes if the test expression returns a false result (ends).
  • Check out relational and logical operators to learn more about test expressions (when test expression is evaluated as true and false).

Example 1: while loop(while and do…while Loop)

// Print numbers from 1 to 5

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

  return 0;
}

Output

1
2
3
4
5

We have initialized I to 1 in this case.

  • The test statement I = 5 is true when I = 1. As a result, the while loop’s body is run. This increases the value of I to 2 and prints 1 on the screen.
  • The test statement is once again true when I = 2, I = 5. The while loop’s body is once more run. This displays 2 on the screen and raises the value of I to 3.
  • This continues until I turn six. The loop will then end if the test expression I = 5 returns false.

do…while loop( while and do…while Loop )

With one significant exception, the do..while loop and while loop is very similar. At least one iteration of the do…while loop’s body is performed. The test expression is only then evaluated.

Do while loops are examples of exit control loops and are similar to while loops with the exception that they check the condition after executing the statements.

The do…while loop is written as follows:

do {
  // the body of the loop
}
while (testExpression);

How does the do…while Loop operates?

  • The do…while loop’s body is only ever executed once. The test expression is only assessed after that.
  • The body of the loop is once more run and the test expression is again examined if the test expression returns true.
  • This continues till the test expression turns out to be false.
  • The loop ends if the test expression result is false.

Example 2: do…while loop(while and do…while Loop)

// Program to add numbers until the user enters zero

#include <stdio.h>
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.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70

Here, a do…while loop has been used to ask the user to enter a number. As long as the input number is not 0, the loop continues to run.

The do…while loop runs at least once, meaning that the initial iteration occurs even when the condition hasn’t been verified. Only after the first iteration has been completed is the condition checked.

do {
  printf("Enter a number: ");
  scanf("%lf", &number);
  sum += number;
}
while(number != 0.0);

Therefore, the loop moves on to the next iteration if the first input is a non-zero value, at which point the number is added to the sum variable. Up until the user enters 0, this process is repeated.

However, there won’t be a second iteration of the loop if the initial input is 0, therefore the sum becomes 0.0.

We output the sum’s value outside of the loop.

Difference Between while and do…while Loop

whiledo-while
Prior to executing any statements, the condition is checked.Statement(s) are run at least once, and then the condition is verified.
If the condition is true, it’s possible that the statement(s) will be executed 0 times.The statement(s) must be executed at least once.
There isn’t a semicolon after a while.
while(condition)
a semicolon follows the word while.
There is no need for brackets if there is only one sentence.There is always a need for brackets.
Prior to the execution of the loop, the variable in the condition is initialized.variable initialization might happen before or during the loop.
The while loop controls entry.the do-while loop is exit controlled loop.
while(condition)
{ statement(s); }
do { statement(s); }
while(condition);

You may like:

JavaScript Variable Scope with Example

Leave a Reply