×

Flow Chart of Do while loop in C

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language

Generally, as we know there are three main components of Do While loop:
1. The initialization statement: This statement initialize the C variables.
2. The update statement: This statement this helps in altering the value for further execution.
3. The termination condition: This statement checks the loop for further execution.

Here, you can see that declaration and initialization of variable by value with the help of assignment operator is done before the do while loop. Then, body of do while loop execute first.

In the next step, we have to use the increment or decrement statement that will be executed after the execution of statements of the body.
The last component of loop will execute a test expression that will certainly help in dismissing the do while loop or from preventing it to run toward infinity as per the requirement. If the termination condition or test expression is true then the loop will go to execute for next time and again check whether the value returned by the termination condition is true or false. Loop will continue to execute until the condition becomes false. As the value is found to be false then the control will come out of the loop and the loop will completely dismissed.

Example:

Here is program in c that will print numbers from 11 to 20 with the help of do while loop:

#include <stdio.h>
int main() 
{	
  int a=11; /* declaring a variable ‘a’ */
do
{
    printf ("%d ", a);
/* printing final value of variable ‘a’ 
}
While (a<=20)
    printf ("%d ", a);
/* printing final value of variable ‘a’ */
++a;
  return 0;
}	

Explanation:

Initially we declared a variable ‘a’. Then, we have for loop syntax that contains 3 main components which are as follows:

  1. a=11            (Initialization statement)
  2. ++a              (increment or decrement statement)
  3. a<=20          (termination condition)

The value of ‘a’ is initialized with 11 before the do while loop. Then, body of the loop will execute. After that, the modification statement helps in altering the value of ‘a’ so that further evaluations can be done. This can be pre-increment, post-increment, pre-decrement, or post-decrement.

And, the termination condition will check that the value of ‘a’ is smaller than 20 or not.  When the value of ‘a’ goes higher than 20 then the termination condition will terminate the for loop.

Two More Examples of Do while Loop:

Example 1:

#include <stdio.h>
int main() 
{
  int a=11; /* declaring a variable ‘a’ */
do
{
    printf ("%d ", a);
/* printing final value of variable ‘a’ 
}
While (a>=2)
    printf ("%d ", a);
/* printing final value of variable ‘a’ */
  }
--a;
  return 0;
}	

Example 2:

#include <stdio.h>
int main() {
  double number, sum = 0; 
/* declaring various variables in distinct data types and initializing their values */
  do {
    printf("Enter a number: ");
/* printf statement to let the user to provide input */
    scanf("%lf", &number);
/* scanf statement to assign the provided value from user to the given variable */
    sum += number;
  }
  while (number != 0.0);
  printf("Sum = %.2lf",sum);
/* printf statement to display the result */
  return 0;
}

Related Topics

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.