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


Joined
Jul 12, 2016
Messages
1
Reaction score
0
Hiiii,
i am looking for answer of this question, please give the answer with example.
What is the different between while(0) and while(1) in c language?
 
Ad

Advertisements

Joined
Jul 12, 2016
Messages
1
Reaction score
1
The word while() in C occurs in two different constructs: in a while-loop
Code:
while (condition)
    loop-body;
and in a do-loop:
Code:
do
    loop-body;
while (condition);

In a while-loop, the condition is tested, and if true (non-zero) the body is executed; and then the test is repeated, and the body repeated, until the test is false (0). So
Code:
while (1)
{
    body;
}
will result in the body of the loop being executed forever (or until loop exit is triggered some other way, such as break or return). Whereas
Code:
while (0)
{
    body;
}
will mean that the body is not executed at all -- obviously this is not very useful!

In a do-loop, the body is executed first, and then the test is evaluated, and the whole is repeated until the test is false. So
Code:
do
{
    body;
} while (1);
will also result in the body being repeated forever (or until a break/return is reached), whereas
Code:
do
{
    body;
} while (0);
means the body will be executed just once. This is most commonly used in macro definitions, to prevent confusion between code generated by the macro and code immediately after the macro call { but that's a separate question ;) }.
 
Joined
Jul 12, 2016
Messages
1
Reaction score
0
While(1) is a infinite loop so if you need to come out of it then you need to issue the break statement explicitly.

Code:
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.
Code:
main()
{
int i=0;
while(0)
{
printf("%d\n", i++); // This will never get executed
if (i==2)
break;
}
}

Source:- http://tech.queryhome.com/48201/wha...e-0-and-while-in-c-language?show=48208#a48208
 
Joined
Feb 6, 2018
Messages
4
Reaction score
0
Hey there,
In C unlike languages like java where the Boolean values can be only either "true" or "false", they can be a bit different where a "non-zero" digit like "1" would be treated as "true" and as you might have guessed a "zero" would be treated as a "false"
So, a while(1){ } would mean the loop would iterate infinitely and a while(0){ } would mean that the body of the loop will never get executed.
 
Last edited by a moderator:
Ad

Advertisements

Joined
Jul 15, 2019
Messages
11
Reaction score
0
loops work based on true(1) and false(0) condition.
while(condition)
if condition is true then statement inside while is executed,else it is avoided. condition will be false only if value will be zero and for rest other values it will be true.
so for
i) while(0)
{
//statement
}
it is evaluated as false and hence statement inside while won't be executed.
ii) while(1){
//statement
}
in this condition is true(1) so it will execute statement inside while loop infinite times as it will will evaluate true always.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top