C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C SJF Scheduling Program in C Socket Programming in C Structure in C Tower of Hanoi in C Union Program in C Variable Declaration in C What is Linked List in C While Loop Syntax in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C C and C++ Binary Files C program that does not Suspend when Ctrl+Z is Pressed Different ways to Declare the Variable as Constant in C Range of Int in C C Program to find the size of a File FIFO Example in the C Language For loop in C Programming GCD program of two numbers in C GPA Calculator in C How to Calculate Time Complexity in C How to include graphics.h in C How to measure time taken by a function in C How to return a Pointer from a Function in C What is the main in C Addition of Matrix in C Booleans in C C Program for Extended Euclidean algorithms C Program of Fencing the Ground Ceil and Floor in C Compound Interest Program in C Displaying Array in C Distance Vector Routing Protocol Program in c Dos.h Header File in C Language DSA Program in C Explain the two-way selection in C Fee Management System in C File Operations in C Malloc function in C Multiplication Table in C Simple Programs in C Language tolower() Function in C Type Conversion in the C Why does sizeof(x++) not Increment x in C Advantages of Dynamic Memory Allocation in C Armstrong Number in C Assignment Operator Program in C Banker’s Algorithm in C Binary Search in C with Best and Worst Time Complexity Caesar Cipher Program in C Call by Value and Call by Reference in C Conditional Operator in C CRC Program in C Deadlock Detection Program in C Decimal to Binary in C Difference between If Else and Nested If Else in C Difference between Pre-increment and Post-increment in C Difference between Scope and Lifetime in C Evaluation of Arithmetic Expression in C Explain the Increment and Decrement Operators in C Fseek Function in C Functions in C How to Find Square Free Numbers in C Length of an Array Function in C OpenGL in C Projects on C language in 2023 Purpose of a Function Prototype in C Stdio.h in C Two-Dimensional array in C What is String Comparison in C C Compilers for Windows Functions and Recursion in C How to Declare Boolean in C How to Declare Character in C How to Round up a number in C How to use strlen() in C Pointer Declaration in C Algorithm for String Palindrome in C C Program to find ASCII value of a character Constant Pointer in C How to find string length in C using strlen() function Implicit and Explicit in C Indirect Recursion in C Input and Output functions in C isupper() in C Jump Statement in C Lifetime of a Variable in C Linker Error in C Language Numeric Constant in C Size of Pointer in C Square Root in C Language Static and Dynamic Memory allocation String Declaration in C Strong Number in C Symmetric Matrix in C Types of C Tokens What is a Size of Pointer in C What is Increment and Decrement Operator in C 1 2 3 4 Series Program in C Advantages and Disadvantages of C Language C Program for Polynomial Addition C Program to Count the Number of Vowels in a String C Programming Errors and Solutions Compilation Errors in C Complex C Programs Difference between Argument and Parameter in C Difference between char s[] and char *s in C Evaluation of Postfix Expression Using Stack in C Find Leftmost and Rightmost Set Bit of a Number fprintf and fscanf in C Introduction to Dynamic Array in C Print Address in C Realloc function in C Ternary Operators in C Types of Tokens in C with Examples Difference between Static and Dynamic Memory Allocation in C Addition Program in C Array Definition in C Array of Pointers in C Arrow Operator in C Average of Two Numbers in C Binary to Decimal in C Binary to Octal in C BREAK STATEMENT in C C Programming Operators Questions C Programs Asked in Interview Calculator Program in C C Program to Read and Print an Employee's Detail Using Structure Bubble Sort Algorithm in C C Program to Find Area and Perimeter of Circle C Program to Check Whether a Given Number is Even or Odd C in Roman Numerals C Program to Make a Simple Calculator Using Switch Case Insertion Sort Program in C How to take input in string in C GCC Conflicting Types in C Function Definition in C Format Specifier for Hexadecimal in C Flowchart in C Float in C Fizzbuzz Implementation in C Conditional Statement in C Conio.h functions list in C Constants in C Dynamic Array in C Decision Making Statements in C Continue Statement in C Creation of Thread in C DFS Algorithm in C Difference between parameter and arguments in C Dijkstra's Algorithm in C Leap Year Program in C Jump Statements in C Modulus Operator in C Memory Allocation in C Simple Interest Program in C Reverse Array in C Recursive Function in C Queue in C Printing Pascal’s Triangle in C Preprocessor Directives in C Perror() in C Perfect Number in C Programming Language Parameter Passing Techniques in C Pascal Triangle in C Patterm Program in C Affine cipher in C Dereferencing pointer in C Internal static variable vs External static variables in C Difference between exit(0) and exit(1) in C Booth's Algorithm in C Condition Control Statements in C Double Specifier in C Dynamic variables in C How to print alphabets in C How to print char array in c Order of Evaluation in C Order of Operations in C Semantic Error in C Size of String Variable in C SJF PREEMPTIVE SCHEDULING PROGRAM C: Tree in C Arithmetic Progression Program in C Array, Declaring Arrays, and Array Initialization ARRAYS IN C Assert() Function in c Atoi in C Bar3d() function in C Graphics Beep function in c Bigint (BIG INTEGERS) in C with Example Builtin functions of GCC compiler Fibonacci series in C Priority Queue in C 2D ARRAY IN C 7 Best IDEs for C/C++ Developers in 2022 Addition of Two Numbers in C Advantages and Disadvantages of C Language Advantages of Function in C Algorithm for String Palindrome in C What is fgets in C language 2d Shearing Program in C Recursion Questions In C Static Identifier in C Inserting elements in an array using C Language complex.h Header File in C fsetpos()(set file position) in C Comparator function of qsort() in C Print Hex in C strnset() function in C %d in C Butterfly Star pattern in C C form format in Excel Scan Line Polygon Filling in C 8 Queens Problem in C Back Trace in C BSS (Block Started by Symbol) Data Segment in C language Class C in Networking Strings in C Programming What is Circular linked list? What is Loop in C BITWISE OPERATOR IN C LANGUAGE BOMB DIFFUSION PROGRAM IN C LANGUAGE

While Loop in C programming examples

Introduction

There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs.

The following statement describes how to use/add header file in C:

#include <stdio.h>

After that,

We write the following line…

int main() 
 {

The line in this block consists of int (a sort of data type just like float, char, and so on) with the main() function, which represents the integer value after the evaluation of the entire program. 

If we used the int in the above line, then we have to write the following statement at the end of the program before closing parenthesis:

return 0;
 }

After doing this, we may assign the value directly to the variable or lets the user use the scanf function for inserting the value at run time.

Initiate value in Program:

int a=1;

By user:

int a;
 printf("ENTER ANY NUMBER: \n" );
   scanf("%d", &a );

In this case, the user can pass an integer value during runtime.

Now, let’s start with the while loop:

Syntax of while loop statement:

while (termination condition/ test expression) 
  {
    printf("%d\n", initiated integer);
    (++/--) variable;
  }

In the syntax, we initially provide a termination condition that helps in dismissing the loop. Then we print the initiated value.

And at last, we use any increment or decrement statement, i.e., ++a, --i, or many more.

In the first four examples, we will see increment and decrement statements, pre-initialized and user-initialized variable values with their outputs.

EXAMPLE 01: In this program, we will print values between 20 and 26.

LESS THAN (<) Comparison Operation:


include <stdio.h>
int main() 
{
  int a=20; // declaring variable or initialization statement
  printf(" THE INPUT IS: %d \n", a);
  printf(" THE OUTPUT IS: \n");
  while (a < 26) // termination condition/ test expression
  {
    printf(" THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

 THE INPUT IS: 20 
 THE OUTPUT IS: 
 THE VALUE OF A IS 21
 THE VALUE OF A IS 22
 THE VALUE OF A IS 23
 THE VALUE OF A IS 24
 THE VALUE OF A IS 25

Explanation:

In the above example, the while loop is initiated with the variable “a” having a value of 20.

According to the termination condition, it is mandatory that the value of “a” must have less than and not equal to 26.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from 20 to 21.

It will again evaluate the termination condition and check whether the value of “a” is less than 26 or not. As 21 is less than 26, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to 23, and the whole evaluation process will be repeated again and again until the “a” becomes 26.

When the value of “a” is incremented to 26, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

The following code will print values from 49 to 55. Here, code accepts the starting value from user at run time:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a < 55) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

ENTER ANY NUMBER: 
49
THE OUTPUT is: 
THE VALUE OF A IS 50
THE VALUE OF A IS 51
THE VALUE OF A IS 52
THE VALUE OF A IS 53
THE VALUE OF A IS 54

Explanation:

This example is also the same as the above, but the difference is that here we are taking input from the user for starting.

According to the termination condition, it is mandatory that the value of “a” must have less than and not equal to 55.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from 49 to 50.

It will again evaluate the termination condition and check whether the value of “a” is less than 55 or not. As 49 is less than 55, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to 50, and the whole evaluation process will be repeated again and again until the “a” becomes 55.

When the value of “a” is incremented to 55, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

EXAMPLE 02: In this program, we will print values between -5 and 1.

Less Than or Equals To (<=) Comparison Operation:

#include <stdio.h>
int main() 
{
  int a=-5; // declaring variable or initialization statement
  printf("THE INPUT IS: %d\n", a);
  printf("THE OUTPUT IS: \n");
  while (a <= 0) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

THE INPUT IS: -5
THE OUTPUT IS: 
THE VALUE OF A IS -4
THE VALUE OF A IS -3
THE VALUE OF A IS -2
THE VALUE OF A IS -1
THE VALUE OF A IS 0

Explanation:

In the above example, the while loop is initiated with the variable “a” having a value of -5.

According to the termination condition, it is mandatory that the value of “a” must have less than or equal to 0.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from -5 to -4.

It will again evaluate the termination condition and check whether the value of “a” is less than or equal to 0 or not. As -4 is less than 0, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to -3, then -2, -1. And, at last, when the value of “a” will become 0 then the condition (a < 0) evaluate as false, but condition a=0 evaluate as true.

As the termination condition has two choices, either less than or equal to. The machine will automatically consider it true because it accepts the value when any one condition becomes true.

When the value of “a” is incremented to 1, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

In the following code, user enters the value at run time:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a <= 101) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

ENTER ANY NUMBER: 
92
THE OUTPUT is: 
THE VALUE OF A IS 93
THE VALUE OF A IS 94
THE VALUE OF A IS 95
THE VALUE OF A IS 96
THE VALUE OF A IS 97
THE VALUE OF A IS 98
THE VALUE OF A IS 99
THE VALUE OF A IS 100
THE VALUE OF A IS 101

Explanation:

This example is also the same as the above, but the difference is that here we are taking input from the user to start.

In the above example, the while loop is initiated with the variable “a” having a value of 92.

According to the termination condition, it is mandatory that the value of “a” must have less than or equal to 101.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from 92 to 93.

It will again evaluate the termination condition and check whether the value of “a” is less than or equal to 101 or not. As 93 is less than 101, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to 94, then 95, 96, and so on.

At last, when the value of “a” will become 101 then the condition (a < 101) will be false, but the condition (a=101) evaluate as true.

As the termination condition has two choices, either less than or equal to. The machine will automatically consider it true because it accepts the value when any one condition becomes true.

When the value of “a” is incremented to 102, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

EXAMPLE 03: (In this program, we will print values between 51 and44.)

More Than (>) Comparison Operation:

#include <stdio.h>
int main() 
{
  int a=51; // declaring variable or initialization statement
  printf("THE INPUT IS: %d\n", a);
  printf("THE OUTPUT IS: \n");
  while (a > 44) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

THE INPUT IS: 51
THE OUTPUT IS: 
THE VALUE OF A IS 50
THE VALUE OF A IS 49
THE VALUE OF A IS 48
THE VALUE OF A IS 47
THE VALUE OF A IS 46
THE VALUE OF A IS 45

In the above example, the while loop is initiated with the variable “a” having a value of 51.

According to the termination condition, it is mandatory that the value of “a” must have more than and not equal to 51.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 51 to 50.

It will again evaluate the termination condition and check whether the value of “a” is more than 44 or not. As 50 is more than 44, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to 49, and the whole evaluation process will be repeated again and again until the “a” becomes 44.

When the value of “a” becomes 44, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the while loop.

In the below program, we will print values from 88 to 78. The following program takes the starting value from user:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a > 77) // termination condition/ test expression
  {
    printf("THE VALUE OF IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

ENTER ANY NUMBER: 
89
THE OUTPUT is: 
THE VALUE OF IS 88
THE VALUE OF IS 87
THE VALUE OF IS 86
THE VALUE OF IS 85
THE VALUE OF IS 84
THE VALUE OF IS 83
THE VALUE OF IS 82
THE VALUE OF IS 81
THE VALUE OF IS 80
THE VALUE OF IS 79
THE VALUE OF IS 78

Explanation:

This example is also the same as the above, but the difference is that here we are taking input from the user to start.

According to the termination condition, it is mandatory that the value of “a” must have more than and not equal to 89.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 89 to 88.

It will again evaluate the termination condition and check whether the value of “a” is more than 80 or not. As 88 is more than 80, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to 87, and the whole evaluation process will be repeated again and again until the “a” becomes 80.

When the value of “a” becomes 79, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

Example 04: In this program, we will print values between 10 and 5.

More Than or Equals To (>=) Comparison Operation:

#include <stdio.h>
int main() 
{
  int a=10; // declaring variable or initialization statement
  printf("THE INPUT IS: %d \n", a);
  printf("THE OUTPUT IS: \n");
  while (a >= 5) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

THE INPUT IS: 10 
THE OUTPUT IS: 
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

In the above example, the while loop is initiated with the variable “a” having a value of 10.

According to the termination condition, it is mandatory that the value of “a” must have more than or equal to 5.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 10 to 9.

It will again evaluate the termination condition and check whether the value of “a” is more than or equal to 5 or not. As 9 is more than 5, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to 8, then 7, 6. And at last, when the value of “a” will become 5, then the condition (a < 5) will be false, but condition (a=5) becomes true.

As the termination condition has two choices, either less than or equal to. The machine will automatically consider it true because it accepts the value when any one condition becomes true.

When the value of “a” is decremented to 4, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

In this program, we will print values between 20 and 12. The following program takes the starting value from user:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a >= 12) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

OUTPUT:

ENTER ANY NUMBER: 
20
THE OUTPUT is: 
THE VALUE OF A IS 19
THE VALUE OF A IS 18
THE VALUE OF A IS 17
THE VALUE OF A IS 16
THE VALUE OF A IS 15
THE VALUE OF A IS 14
THE VALUE OF A IS 13
THE VALUE OF A IS 12

Explanation:

This example is also the same as the above, but the difference is that here we are taking input from the user for starting.

In the above example, the while loop is initiated with the variable “a” having a value of 20.

According to the termination condition, it is mandatory that the value of “a” must have a value more than or equal to 12.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 20 to 19.

It will again evaluate the termination condition and check whether the value of “a” is more than or equal to 12 or not. As 19 is more than 12, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to 18, then 17, 16, and so on.

At last, when the value of “a” will become 12, then the condition a < 12 will be false, but the condition (a=12) becomes true.

As the termination condition has two choices, either less than or equal to. The machine will automatically consider it true because it accepts the value when any one condition becomes true.

When the value of “a” is decremented to 11, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the “while” loop.

Infinity while loop

Example 01: This program print the values 5 to infinity with the help of more than (<) comparison operation and decrement statement:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a < 6) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

Output:

ENTER ANY NUMBER: 
5
THE OUTPUT is: 
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
THE VALUE OF A IS 0
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:

This example is also the same as the examples mentioned above. Here, we also taking input from the user to start.

In the above example, the while loop is initiated with the variable “a” having a value of 5.

According to the termination condition, it is mandatory that the value of “a” must have a value less than and not equal to 6.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 5 to 4.

It will again evaluate the termination condition and check whether the value of “a” is less than 6 or not. As 5 is less than 6, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to 4, and the whole evaluation process will be repeated again and again until the “a” becomes 6.

But here we see that the value entered is 5, which is less than 6. And, the modification statement decrements the value which will always be less than 6.

Hence, it will print the values till (minus) infinity.

The loop will never end and continue to evaluate the termination condition.

Example 02: This program print the values 0 to minus infinity with the help of more than or equals to (<=) comparison operation and decrement statement:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );


  while (a <= 0) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    --a; // increment/ decrement statement
  }
  return 0;
}

Output:

ENTER ANY NUMBER: 
0
THE OUTPUT is: 
THE VALUE OF A IS 0
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
THE VALUE OF A IS -11
THE VALUE OF A IS -12

This example is also the same as the examples mentioned above. here we also taking input from the user to start.

In this example, the while loop is initiated with the variable “a” having a value of 0.

According to the termination condition, it is mandatory that the value of “a” must have less than or equal to 0.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line decrements the statement used to modify the value for further evaluation.

The decrement statement will decrease the value from 0 to -1.

It will again evaluate the termination condition and check whether the value of “a” is less than or equal to 0 or not. As -1 is less than 0, so the loop will be considered true and executed.

After this, the value of “a” will be decremented to -2, and the whole evaluation process will be repeated again and again until the “a” becomes 0.

But here we see that value after first modification becomes -1, which is less than 0. And, the modification statement decrements the value which will always be less than 0.

Hence, it will print the values till (minus) infinity.

The loop will never end and continue to evaluate the termination condition.

Example 03: This program print the values 38 to infinity with the help of less than (>) comparison operation and increment statement:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a > 37) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

Output:

ENTER ANY NUMBER: 
38
THE OUTPUT is: 
THE VALUE OF A IS 39
THE VALUE OF A IS 40
THE VALUE OF A IS 41
THE VALUE OF A IS 42
THE VALUE OF A IS 43
THE VALUE OF A IS 44
THE VALUE OF A IS 45
THE VALUE OF A IS 46
THE VALUE OF A IS 47
THE VALUE OF A IS 48
THE VALUE OF A IS 49
THE VALUE OF A IS 50

This example is also the same as the examples mentioned above. Here, we also taking input from the user to start.

In the above example, the while loop is initiated with the variable “a” having a value of 38.

According to the termination condition, it is mandatory that the value of “a” must have more than and not equal to 37.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from 38 to 39.

It will again evaluate the termination condition and check whether the value of “a” is more than 37 or not. As 39 is more than 37, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to 40, and the whole evaluation process will be repeated again and again until the “a” becomes 37.

But here we see that the value entered is 38, which is more than than 37. And, the modification statement increments the value which will always be more than 37.

Hence, it will print the values till (plus) infinity.

The loop will never end and continue to evaluate the termination condition.

Example 04: This program print the values 65 to infinity with the help of less than or equals to (>=) comparison operation and increment statement:

#include <stdio.h>
int main() 
{
  int a; // declaring variable or initialization statement
   printf("ENTER ANY NUMBER: \n" ); // taking input from user
   scanf("%d", &a ); // assigning value to the variable 
   printf("THE OUTPUT is: \n" );
  while (a >= 65) // termination condition/ test expression
  {
    printf("THE VALUE OF A IS %d\n", a); // printing output
    ++a; // increment/ decrement statement
  }
  return 0;
}

Output:

ENTER ANY NUMBER: 
65
THE OUTPUT is: 
THE VALUE OF A IS 65
THE VALUE OF A IS 66
THE VALUE OF A IS 67
THE VALUE OF A IS 68
THE VALUE OF A IS 69
THE VALUE OF A IS 70
THE VALUE OF A IS 71
THE VALUE OF A IS 72
THE VALUE OF A IS 73
THE VALUE OF A IS 74
THE VALUE OF A IS 75
THE VALUE OF A IS 76
THE VALUE OF A IS 77........

This example is also the same as the examples mentioned above. Here we also taking input from the user to start.

In the above example, the while loop is initiated with the variable “a” having a value of 65.

According to the termination condition, it is mandatory that the value of “a” must have more than or equal to 65.

Let's talk about the body of the while loop. The first program line is for printing the current value of “a”, and the second line increments the statement used to modify the value for further evaluation.

The increment statement will increase the value from 65 to 66.

It will again evaluate the termination condition and check whether the value of “a” is more than or equal to 65 or not. As 66 is more than 65, so the loop will be considered true and executed.

After this, the value of “a” will be incremented to 67, and the whole evaluation process will be repeated again and again until the “a” becomes 64.

But here we see that the value after first modification becomes 66, which is more than than 65. And, the modification statement increments the value again and again which will always be more than 65.

Hence, it will print the values till (plus) infinity.

The loop will never end and continue to evaluate the termination condition.

Example 5: This Program will print the values from -5 to 4 with the help AND (&&) operation and increment statement:

#include <stdio.h>
int main () 
{
  int a;//initializing “a” as a value
  printf ("enter any digit\n"); //providing a source of input to user  
  scanf ("%d", &a); // assigning the value to a
  while (a>=-5 &&a <=4) // 'and operation' termination condition
  {
    printf ("the value of a is %d\n", a); //body of while-loop
    a++;
  }
   return 0;
}

Output:

enter any digit
-5
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
the value of a is -0
the value of a is 1
the value of a is 2
the value of a is 3
the value of a is 4

In this example, the user initiates the while loop with the initialization value of -5.

According to the termination condition, it is mandatory for “a” that its value must less than or equal to 4 and more than or equal to -5.

It means it will accept values less than or equal to 4 and more than or equal to -4.

Let's talk about the body of the while loop. The first line is for printing the value of a a, and the second line increments the statement used for modifying the value for further evaluation.

In the code, the modification statement will increase the value from -5 to -4. After modification, code willevaluate the termination condition and check whether the -4 is less than or equal to 4 and more than or equal to -4. If the condition is satisfied then the loop evaluates. After this, the value of “a” will be incremented to -3, then -2, -1,0, 1, 2, etc.

The entire evaluation process will be repeated again and again until and unless the value of the termination condition results in false.

When the value of “a” becomes 5, then the loop will undoubtedly false as a result of termination condition evaluation, and the control will come out from the while loop.

Example 6: This Program will print the values from 1 to 10 with the help OR (||) operation and increment statement:

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

Output:

enter any digit
1
 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
 the value of a is 11
 the value of a is 12

In this example, the user initiates the while loop with the initialization value 1. According to the termination condition, it is mandatory for “a” that its value must less than or equal to 10 and more than or equal to 1.

It means it will accept values less than or equal to 10 and more than or equal to 1.

Let's talk about the body of the while loop.

The first program line is for printing “the value of a is” and then the current value of “a”, and the second line increments the statement used for modifying the value for further evaluation.

The modification statement increase the value from 1 to 2, and the control will again evaluate the termination condition and check whether the resultant value is true or false.

The condition will be considered true and printed as 2. After this, the value of “a” will be incremented to 3, then 4, 5, 6, 7, and so on.

The entire evaluation process will be repeated again and again until and unless the value value of the termination condition results as false.

But here we can see that the value entered is 1, which is undoubtedly a value less than or equal to 10 “or” more than or equal to 1, and then comes modifying program line, which is an increment statement.

So, the value after modification will always be less than or equal to 10 “or” more than or equal to 1.

But here we see that the value after first modification becomes 2, which is less than and equal to 10 or more than and equal to 1. And, the modification statement increments the value again and again which will always always be less than or equal to 10 or more than or equal to 1.

Hence, it will print the values till (plus) infinity.

If there is a decrement statement in the body of loop, then the output will run till the minus infinity. The loop will never end and continue to evaluate the termination condition.

Few more examples

#include <stdio.h>
int main()
{
    int a;
    printf("Enter any value of 'a':\n");
    scanf("%d", &a);
    while (a >= 66)
    {
       printf("The value of 'a' is %d \n", a );
       a=a+1;
       if (a == 71)
       {
          break;
       }
    }
}

Output: When the input is less than the condition given in IF statement and more than the condition given in While statement, then the following output is shown:

Enter any value of 'a':
67
The value of 'a' is 67 
The value of 'a' is 68 
The value of 'a' is 69 
The value of 'a' is 70
The value of 'a' is 71
The value of 'a' is 72
The value of 'a' is 73
The value of 'a' is 74

Output: When the input is more than the condition of IF statement, then the code will continuously print the results and run towards positive infinity.

Enter any value of 'a':
76
The value of 'a' is 76 
The value of 'a' is 77 
The value of 'a' is 78 
The value of 'a' is 79
The value of 'a' is 80
The value of 'a' is 81
The value of 'a' is 82
The value of 'a' is 83
The value of 'a' is 84
The value of 'a' is 85
The value of 'a' is 86.......