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