×

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

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

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.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 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.