×

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

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes 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.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.