×

While-Loop in C

Syntax of While Loop

The syntax that has been used for the while loop in C programming language is:

while (termination condition) {
  // the body of the loop 
}

Working of While-Loop

  1. Initially, we use the parentheses for storing the termination condition of the While loop.
  2. Then, there can be two outcomes True or False.
  3. Firstly, if the result is true, then the control will automatically execute the body of the loop.
  4. It will continue to evaluate again and again until the termination condition is false.
  5. Secondly, if the result is false, then the control will definitely come out of the While loop and will dismiss the While Loop.    

Flow chart of While Loop

Here is the diagrammatic presentation, which shows the evaluation of the termination condition (test condition) by while-loop with the help of a flow chart.

WHILE-LOOP IN C

Examples of While Loop

Let's understand the entire process with the help of an example:

Example 1: Program of While loop (Increment)

/* Printing integers from 1 to 10 */
#include <stdio.h>
int main () 
{
  int a = 1; // initializing the value of a as 1
  while (a <= 10) // termination condition
  {
    printf ("the value of 'a' is %d\n", a); // body of the while-loop 
    ++a; // increment/ decrement statement
  }
  return 0;
}

Output:

the value of a is 1
the value of a is 2
the value of a is 3
the value of a is 4
the value of a is 5
the value of a is 6
the value of a is 7
the value of a is 8
the value of a is 9
the value of a is 10

Explanation:

Firstly, we initialized the value of the integer variable, which we took as a. Then, we applied a termination condition (while (a <= 10) to avoid the output running to infinity.

After that, we implemented the main body of the while loop just like the following statement:

[Printf ("the value of ‘a’ is %d\n", a);].

This statement prints the desired output that we want (see output). In the main body, we placed an increment statement (++a;). Through this statement, we can get positive integers starting with 1 and ending with 10.

++a statement performs the increment command and will add 1 to the previous value of a.

This evaluation will perform several times until the value of a becomes 11.

When the value of a becomes 11, then it will terminate the entire process because the termination condition gets false as the value of a is greater than 10. So, the loop only executes to the values less than 10 or equal to 10.

Let's see a few more similar examples of while loop.

Example 02: Program of While Loop (decrement):

/* Printing integers from 10 to 1 */
#include <stdio.h>
int main() 
{
  int a = 10; // initializing the value of a as 10
  while (a >= 1) // termination condition
  {
    printf("the value of ‘a’ is %d\n", a); // body of the while-loop 
    --a; // increment/ decrement statement
  }
  return 0;
}

Output:

the value of a is 10
the value of a is 9
the value of a is 8
the value of a is 7
the value of a is 6
the value of a is 5
the value of a is 4
the value of a is 3
the value of a is 2
the value of a is 1

Example 03: Program of while loop with specified starting and ending values:

/* to print the value from a specific point to a specific point */
#include <stdio.h>
int main () 
{
  int a;//initializing 'a' as a value 
  printf ("enter any digit"); //providing a source of input to user  
  scanf ("%d", &a); // assigning the value to a 
  while (a>=1 &&a <=10) //termination condition
  {
    Printf ("\n the value of ‘a’ is %d", a); //body of while-loop
    a++;
  } 
   return 0;
}

Output:

the value of a is 1
 the value of a is 2
 the value of a is 3
 the value of a is 4
 the value of a is 5
 the value of a is 6
 the value of a is 7
 the value of a is 8
 the value of a is 9
 the value of a is 10

Related Topics

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute 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.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

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

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

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.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

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