×

Flow chart of While loop in C

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.
Generally, as we know there are three main components of while loop:
1. The initialization statement,
2. The termination condition, and
3. The update statement which helps in altering the value while further execution.

Here, you can see that the first step is to declare the variable and initialize its value with the help of assignment operator.

In the next step, we will have to use a test expression that will certainly help in dismissing the 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 control will go to the main body of the loop.
The last component of while loop is the increment or decrement statement that will be executed after the execution of the body.

This execution will terminate here. And, the loop will go to execute while next time and 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 of while Loop: Here is program in c that will print numbers from 11 to 20 with the help of while loop:

#include <stdio.h>
int main() 
{
  int a=11; /* declaring a variable ‘a’ and initializing its value as 11*/
  while (a <= 20)
/* a must be less than 20 */
  {
    printf ("%d ", a);
/* printing final value of variable ‘a’ */
++a;
/* modifying statement */
  return 0;
}	

Explanation:

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

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

In this, the value of ‘a’ will begin with 11, and the termination condition will check that the value of ‘a’ is smaller than 20 or not. After this, there is modifying statement that 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. When the value of ‘a’ goes higher than 20 then the termination condition will terminate the while loop.

Two More Examples of while Loop

Example 1:

#include <stdio.h>
int main() 
{
  int a=11; 
/* declaring a variable ‘a’ and initializing its value as 11*/
  while (a >= 2)
/* a must be more than 2 */
  {
    printf ("%d ", a);
/* printing final value of variable ‘a’ */
--a;
/* modifying statement */
  }
  return 0;
}		

Example 2:               

#include <stdio.h>
int main()
{
    int a=0, b=1, c = 0;
/* declaring a variable ‘a’ ‘b’ ‘c’ and initializing values*/
    printf("Enter a positive integer: ");
    scanf("%d", &a);
    while (b <= a)
/* initializing its value as 1; a must be less than a; modifying statement */
    {
        c += b;
    }
    printf("c = %d", c);
/* printing final value of variable ‘c’ */
--b;
/* modifying statement */
    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.

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.

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.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

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

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

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

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

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.

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.