×

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

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

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

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.

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.

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.

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.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

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.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

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

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

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.