More topics in Do-while loop in C – Full explanation with examples and tutorials

# Do-while loop in C – Full explanation with examples and tutorials

This one’s simple to understand. The entire concept of a do-while loop lies in its name. The first thing that the command does is that it “does” something, and then it checks for a condition. This means that before a condition is checked for, the contents of the loop execute at least one time. If later, the condition holds true, the loop will execute again. The sequence of events is something as follows:

Execute loop -> Check for condition (if true) -> Execute loop -> Check for condition (if false) -> Exit loop. Contents

## What is the syntax of the do-while loop in C?

```do
{
statement(s);
}
while( condition );```

## What is a do-while loop? How do these do while loops work? Where should you use a do-while loop?

As explained above a do-while loop executes the set of statements first and then check for the condition.

The control enters the loop normally, executes it and then exits the loop to check the condition. If the condition is false then the control transfers to the subsequent command and the loop is history. However, if the condition is true then the control goes back to the beginning of the loop and its contents execute all over again.

The do-while loop in C is used mainly when you want to execute a set of commands at least once even if the condition is false.

## What does the flow chart of a do-while loop in C look like? ## What is the difference between a do-while loop and a while loop?

A while loop checks the condition first, if the condition is satisfied, the control enters the loop and executes the statements within the loop. In short, the contents of the loop never execute even once before the condition is checked. Meanwhile, in a do while loop, the loop executes at least once before the condition is checked.

#### Example of a while loop

```#include <stdio.h>
int main()
{
int a=0;
while(a==1)
{
printf("I am in the loop");
}
printf("I am out of the loop");
}
```

Output

`I am out of the loop`

#### Example of a do-while loop

```#include <stdio.h>
int main()
{
int a=0;
do
{
printf("I am in the loop");
}
while(a==1)
printf("\nI am out of the loop");
}```

Output

```I am in the loop
I am out of the loop```

## What is the difference between a do-while loop, a while loop and a for loop?

• The main difference between the three is that in for loops and while loops, the condition is checked before the control enters the loop. Whereas in the do-while loop, the condition is checked when the control exits the loop.
• Another main difference is in the syntax. In for and while loops, there is no semicolon after the condition. However, there is a semicolon after the condition in a do-while loop. This makes sense because if a semicolon were entered after the condition in for and while loop, the control would never enter the loop. As a semicolon signals the end to a command.
• In a for loop, the initialization of the condition along with updating it forms a part of the syntax. In a while and do-while, it doesn’t.

## Example 1: Write a program in C using a do while loop to print something n times.

```//print number of times the user wants to print something
#include<stdio.h>
#include<conio.h>
void main()
{
int i, x=0;
printf("Enter number of times you want to print hello\n");
scanf("%d",&i);
do
{
printf("Hello\n");
x++;
}
while(x<i);
getch();
}```

### Output

```Enter number of times you want to print hello
1
Hello```

## Example 2: Write a program in C using a do while loop to take a number from the user and print the sum of its digits.

```//take number from the user and print sum of digits
#include<stdio.h>
#include<conio.h>
void main()
{
int i,temp,a,sum=0;
printf("Enter number\n");
scanf("%d",&i);
do
{
temp=i%10; //separate the numbers starting from the RHS and store to temp
a=temp; //assign seperated numbers to a
i=i/10; //remove the LSB of the number
}
while(i>0);
printf("sum of digits = %d",sum);
getch();
}```

### Output

```Enter number
234
sum of digits = 9```

First, a number is taken from the user and stored in a variable. Then we use the modulo function to get the remainder by dividing the number by 10. And then we decrease the number in terms of numerals by dividing it by 10. The separated number is added in every iteration of the loop. The condition remains true as long as the division of the number does not yield 0. This happens only after all the digits are added.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8717512,"math_prob":0.8312252,"size":4288,"snap":"2020-10-2020-16","text_gpt3_token_len":1024,"char_repetition_ratio":0.20751634,"word_repetition_ratio":0.098445594,"special_character_ratio":0.2497668,"punctuation_ratio":0.10867179,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9858598,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-03-28T17:09:25Z\",\"WARC-Record-ID\":\"<urn:uuid:1b2e068f-62b7-452e-9221-52511bb9bf77>\",\"Content-Length\":\"99036\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a5a79193-669e-470a-9aa8-59d67590bd8e>\",\"WARC-Concurrent-To\":\"<urn:uuid:03813a5e-2a5c-41a0-88d8-e3642ce80102>\",\"WARC-IP-Address\":\"35.208.13.189\",\"WARC-Target-URI\":\"https://www.technobyte.org/do-while-loop-c-explanation-examples-tutorials/\",\"WARC-Payload-Digest\":\"sha1:QAMURZNPUPV5Y5HXJ6KMEDW2U4BKBOLN\",\"WARC-Block-Digest\":\"sha1:WHXWCN7EPVNFRRNYMPWIKBKB3WZCT5WA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370492125.18_warc_CC-MAIN-20200328164156-20200328194156-00050.warc.gz\"}"}