top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the different between while(0) and while(1) in c language?

+2 votes
16,445 views
What is the different between while(0) and while(1) in c language?
posted Jun 19, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

+1 vote

While(1) is a infinite loop so if you need to come out of it then you need to issue the break statement explicitly.

main()
{
  int i=0;
  while(1) 
  {
    printf("%d\n", i++);
    if (i==2)
      break;
  }
}

While(0) means non entry and code under while will never get executed.

main()
{
  int i=0;
  while(0) 
  {
    printf("%d\n", i++); // This will never get executed 
    if (i==2)
      break;
  }
}
answer Jun 19, 2014 by Salil Agrawal
0 votes

While 0
{
// will never be executed as condition always FALSE
}

While 1
{
// will never given up as condition always TRUE
// this will be used in Multitasking/Threading concepts.
}

answer Dec 4, 2014 by sivanraj
while(0) means the condition will always false and
while(1)means the condition is true
0 votes

while(0)

Actually While of something (i.e) while( X ) X is the condition we are giving to run the loop for X times, so as we know that in C the value 0 have meaning called FALSE(0x00000000). so whenever a loop receives a FALSE input it will just leave the loop and proceeds with the next immediate statement.

while(1)
looking into While(X), if X = 1, here the value X = 1 will be taken as TRUE (i.e) 0xffffffff, so not only while(1) while (ANY NON ZERO VALUE) can run as infinite loop other than X = 0. simply ,the condition for the loop is always TRUE.
so usually we have to give a condition for the loop to break.

answer Jun 21, 2016 by Sudharshan
0 votes

while(1) Is infinite loop & While(0) Do not enter in while loop it directed exit the loop

answer Sep 5, 2016 by Sharad Gangurde
Similar Questions
+3 votes

While(0) means dont enter into it, but why you want to have while(0) in c code. Any suggestion.

Asked in the interview today.

+1 vote

Array consist of -1 and 1, Find count of all sub-arrays where sum = 0.
Input:
[-1,1,-1,1]

Output:
4
[-1,1] [1,-1],[-1,1],[-1,1,-1,1]

...