| by Arround The Web | No comments

Do-While Loops in the C Language

Conditional loops are a great resource in programming. They can be found in many functions in the various C language libraries and have the task of repeating a code snippet until it is released from the loop by an escape condition.

In this Linux Hint article, you will learn how to use the do-while conditional loop, a variant of the “while” loop with a different escape method that is very useful in certain cases.

We will see the various options that the C language offers for conditional loops and a brief description of each one. Then, we’ll take a detailed look at how the do-while loop works, and explain its escape method and the relational operations that control it. We will also apply what we learned through the practical examples with code snippets and images that show the use of this conditional loop in different cases.

Conditional Loops in C Language

A conditional loop repeats the execution of the code that it encloses in curly braces until the escape condition is met, which releases the program from the loop.

The escape condition is determined by a relational operation where a “false” result exits the program loop while a “true” result keeps it inside.

The expression for this operation must be enclosed in parentheses and must preceded by the name of the loop as shown in the following:

while ( relational operation )

Next, let us look at a brief description of the two conditional loop options which are provided by the C language. Each of them has functionality that adapts to the particular requirements of the case. Then, we will look at the do-while statement, a variant of the while loop, in detail.

While Loop

The conditional “while” loop repeats its cycle as long as the value of a variable does not satisfy the escape condition. Let us look at an example where the escape condition in this case is “a” equals “0”:

while ( a!=0 ){

//code…
}

For Loop

The “for” loop provides a way to declare a variable and apply a relational operation to the result of an arithmetic or logical operation on that variable. If the result of the relation is false, the program exits the loop and continues its execution. If the result does not match, the loop repeats in the same way as the “while” loop. Let us look at the syntax of this conditional loop:

for (Variable declaration, relational operation, arithmetic operation)

In the following illustration, we see a code fragment in which the variable a is declared with the value 0, the arithmetic operation “a+2” is applied to it, and the relational operation “a!=100” is applied to the result:

for ( int a =0; a!=100; a+2){
//code
}

In this case, the program runs through 50 cycles of the “for” loop before breaking out of it.

Do-While Loop Syntax in C Language

do{

   code…

  }while (condition);

Description and Examples of the Conditional Do-While Statement in the C Language

The conditional do-while statement works the same as the while statement, but in reverse order. That is, “while” analyzes the escape condition at the beginning of the loop, and “do-while” does so at the end. This guarantees that the program runs through the code by enclosing the loop at least once, regardless of whether the escape condition is met or not.

The difference between the “while” and “do-while” statements is the point in the loop where the output is determined. Let us look at what happens in a “while” loop when the escape condition is specified before entering it:

#include <stdio.h>
void main()
{
printf ( "\nStart program...\n");
while (0 != 0){
       printf ( "\nInside loop\n");
       }
printf ( "\nEnd program\n");

}

In the following figure, we see the result of compiling and executing this code. Since the escape condition was given before the “while” loop was reached, the program jumps to the end of the loop without executing the code inside.

Let us see what happens if we replace the “while” statement with a “do-while” statement with the same condition:

#include <stdio.h>
void main()
{
printf ( "\nStart program...\n");
do{
   printf ( "\nInside loop\n");
  }while (0 != 0);

printf ( "\nEnd program\n");

}

In the following figure, we see the result of compiling and executing this code.
As you can see, although the escape condition is specified before entering the loop, as in the previous example, the program in this case enters it and executes the code one pass.

Nested Do-While Loops in the C Language

Just like other conditional loops, the C language allows the nesting of “do-while” loops. We see an example where one of these loops is nested inside another as shown in the following:

#include <stdio.h>
void main()
{
int a =3;
int b=3;
printf ( "\nStart program...");
do{
   printf ( "\nloop 1");
   a--;
   do{
    printf ( "\n    loop 2");
    b--;
    }while (b != 0);
    b=3;
  }while (a != 0);
printf ( "\nEnd program\n");
}

The image that we see in the following illustration shows the compilation and execution of this code where two nested “do-while” loops are executed.

Conclusion

In this Linux Hint article, we showed you how to use the conditional “do-while” loop.
We started with an introduction which explains what the conditional loops are consist of and discussed the different options available in the C language, briefly describing each of them, their development, and their use.

We also discussed the details with the theoretical explanation of the “do-while” loop by showing you how it works, the relational operations that determine the escape condition, and the differences between this variant and the “while” loop.

We then put what we learned into practice with code snippets and images that show how to develop a program with the simple or the nested “do-while” loops.

Share Button

Source: linuxhint.com

Leave a Reply