Translate

ads

27 Jun 2019

Difference between while(1) and while(0) in C language

Difference between while(1) and while(0) in C language



In most programing languages, a jiffy loop may be a control flow statement that enables code to be executed repeatedly supported a given mathematician condition. The mathematician condition is either true or false

while(1)

It is Associate in Nursing infinite loop which can run until a prospect statement is issued expressly. apparently not while(1) however any number that is non-zero can provide the similar result as while(1). Therefore, while(1), while(2) or while(-255), all can provide infinite loop only.

while(1) or while(any non-zero integer)
    // loop runs infinitely
}


A simple usage of while(1) will be within the Client-Server program. within the program, the server runs in an infinite whereas loop to receive the packets sent type the purchasers.But much, it's not advisable to use while(1) in realworld as a result of it will increase the computer hardware usage and additionally blocks the code i.e one cannot start from the while(1) till the program is closed manually.while(1) will be used at an area wherever condition has to be true continually

while(1) is used at an area wherever condition must be true always

// C program to illustrate while(1) 
int main() 
int i = 0; 
while ( 1 ) 
printf( "%d\n", ++i ); 
if (i == 5)  
break; // Used to come 
// out of loop 
return 0; 

1
2
3
4
5

while(0)

It is opposite of while(1). It suggests that condition can perpetually be false and therefore code in whereas can never get executed.

while(0)
{
    // loop does not run
}


// C program to illustrate while(0) 
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; 


Output:

Didn't execute the loop!


No comments:

Post a Comment