×

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

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

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.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

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.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

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

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.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.