×

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

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

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.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

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

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

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

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.

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.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

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.

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.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

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

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.