Difference between while(1) and while(0) in C language
Last Updated :
08 Nov, 2022
Prerequisite: while loop in C/C++
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false.
while(1)
It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not while(1) but any integer which is non-zero will give a similar effect as while(1). Therefore, while(1), while(2) or while(-255), all will give infinite loop only.
We write conditions in brackets(). Condition can either resolve to true or false. So 0 represents false and any value except it is true.
so logically:
while(true) ==while(1)==while(any value representing true);
while(false)==while(0);
while(1) or while(any non-zero integer)
{
// loop runs infinitely
}
A simple usage of while(1) can be in the Client-Server program. In the program, the server runs in an infinite while loop to receive the packets sent from the clients.
But practically, it is not advisable to use while(1) in real-world because it increases the CPU usage and also blocks the code i.e one cannot come out from the while(1) until the program is closed manually. while(1) can be used at a place where condition needs to be true always.
C
// C program to illustrate while(1)
#include <stdio.h>
int main()
{
int i = 0;
while (1) {
printf("%d\n", ++i);
if (i == 5)
break; // Used to come
// out of loop
}
return 0;
}
C++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while (1) {
cout << ++i << "\n";
if (i == 5)
// Used to come
// out of loop
break;
}
return 0;
}
while(0)
It is opposite of while(1). It means condition will always be false and thus code in while will never get executed.
while(0)
{
// loop does not run
}
C
// C program to illustrate while(0)
#include<stdio.h>
int main()
{
int i = 0, flag=0;
while ( 0 )
{
// This line will never get executed
printf( "%d\n", ++i );
flag++;
if (i == 5)
break;
}
if (flag==0)
printf ("Didn't execute the loop!");
return 0;
}
C++
#include <iostream>
using namespace std;
int main() {
int i = 0, flag=0;
while ( 0 )
{
// This line will never get executed
cout << ++i << "\n";
flag++;
if (i == 5)
break;
}
if (flag==0)
cout << "Didn't execute the loop!";
return 0;
}
OutputDidn't execute the loop!
Similar Reads
Difference between while and do-while loop in C, C++, Java while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax :while (boolean condition){ loop statements...}Flowchart:Example:C++ #include <iostream> usin
2 min read
Difference between for and while loop in C, C++, Java In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov
5 min read
Difference between for and do-while loop in C, C++, Java for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Syntax: for (initialization condition; testing con
2 min read
Difference between scanf() and gets() in C scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code:Â C // C program to see how scanf() // stops reading input after white
3 min read
Difference between Keyword and Identifier in C In C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff
3 min read