Visual flowchart of do while loop in C with execution steps

The given diagram explains the flowchart of a do while loop in C programming.

Step by step, the program first enters the do block. At this point, it doesn’t check any condition. It simply runs the statements written inside the loop body

After executing those statements once, the control moves to the while condition. If the condition is true, the control goes back to the loop body and runs the statements again.

This process continues as long as the condition remains true.
When the condition becomes false, the loop ends, and the program continues with the code that comes after the loop.


Syntax of do while loop in C

Let’s look at a real-world example of the do...while() loop syntax, and then we’ll break it down to understand each part in detail.

 /* Declaration or Initialization statement */
  do {
     /* loop body */
     /* instruction(s); */
     /* update expression */
   } while (condition);


Explanation :
Declaration or initialization : In a do while loop, we usually need to declare and initialize some variables. These variables are used inside the loop body and also in the update expression or the loop condition. Without proper initialization, the loop may not work as expected.

instruction(s) : These are the actual statements written inside the loop. They perform a specific task in the program. The instructions and the update expression together make up the body of the loop. These are the parts that will be repeated as long as the condition is true.

update expression : This is also called the iteration expression. It usually increases or decreases the value of a variable (like i++ or i--).
The update expression runs after each loop cycle, and before the condition is checked again. It helps control how many times the loop runs. condition : The condition is a true or false expression placed at the end of the do while loop. It decides whether the loop should run again or stop.
If the condition is true, the loop repeats. If it is false, the loop ends.
Note : At the end of every do while loop, a semicolon (;) is required after the while(condition) part. If you forget it, the compiler will show a syntax error.



Menu driven program in C using do while loop

#include<stdio.h>
#include<stdlib.h>
void main ()
{
char c;
int choice
do{
printf("\n 1. Withdrawal\n 2.Balence Enquiry \n 3. Deposit \n 4. Options \n
5. Transfer \n 6. Change PIN \n 7. Exit\n");
scanf("%d",&choice);
switch(choice)
{
   case 1 :
    printf("Wellcome to Withdraw Money");
     break;
   case 2:
     printf("Wellcome to Your Balance History");
     break;
   case 3:
     printf("Wellcome to Deposit Your amount.");
     break;
   case 4:
     printf("Wellcome to Options Section");
     break;
   case 5:
     printf("Wellcome to Transfer money");
     break;
   case 6:
     printf("Wellcome to Change Your PIN");
     break;
   case 7:
     exit(0);
     break;
   default:
      printf("please enter valid choice");
}
printf("do you want to enter more?");

scanf("%c",&c);
}while(c=='y');
}

Output:
1. Withdrawal
2. Balence Enquiry
3. Deposit
4. Options
5. Transfer
6. Change PIN
7. Exit

do you want to enter more?
y
1. Withdrawal
2. Balence Enquiry
3. Deposit
4. Options
5. Transfer
6. Change PIN
7. Exit
2
Wellcome to Your Balance History
do you want to enter more?
n

Explanation:
First, the program declares two variables inside the main() function: one integer variable named choice and one character variable named c. This step is called the declaration statement.

Next, the do...while() loop begins. Since it’s a do-while loop, the control directly enters the do block and starts executing the code inside the loop body.

The printf() function is used to display the menu options on the screen. This menu includes choices like Withdrawal, Balance Enquiry, Deposit, Transfer, and so on.

Then, the program uses scanf() to take input from the user. The value entered is stored in the choice variable. In the example above, the user entered 2, which means they chose the Balance Enquiry option.

Because the input matches case 2 in the program, it shows the message: "Welcome to Your Balance History".

After that, the program asks the user if they want to enter more options. The user types 'n', which means they do not want to continue.
Since the input is 'n', the loop condition becomes false, and the program ends its execution.


write a C program for finding total and average of first N natural numbers using a do...while() loop.

#include <stdio.h>
int main()
{
int total = 0, n, i = 1;
float avg=0.0;
printf("Enter the value of n : ");
// input n
scanf("%d", &n);
do {
// loop body
total += i;
// update expression
i++;
} while (i <= n);
printf("Total : %d", total);
/* Average of first N numbers
typecasting or convert total from int to float data type*/
avg = (float)total / n;
/* %0.2f will print avg with a precision of 2 decimal places */
printf("\nAverage of %d numbers : %0.2f", n, avg);
return 0;
}

output:

Enter the value of n : 10
Total: 55
Average of 10 numbers : 5.50
Explanation :
1.Three integer variables — sum = 0, i = 1 and n — are declared and initialized inside the main() function. This step is called declaration and initialization.

2.A float variable named avg is also declared and initialized to 0.0.

3.The program takes an input number from the user using the scanf() function. For example, if the user enters 10, it is stored in the variable n.

4.The do...while() loop starts. The control directly enters the do block and begins executing the code inside the loop.

5.Inside the loop, the statement sum += i (which means sum = sum + i) adds the current value of i to the variable sum during each iteration.

6.The statement i++ increases the value of i by 1. This helps move the loop forward and eventually stop it when the condition becomes false.

7.The loop continues to run as long as the condition i <= n is true. It repeats the steps for all values from 1 to n.

8.When i becomes 11 (i.e., greater than n), the condition i <= n becomes false. The loop ends and control moves to the code after the loop.

9.Outside the loop, the average is calculated using the formula: avg = sum / n.

10.Finally, the program prints the average of n numbers using the printf() function.