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

Discussion in 'C Programming' started by abhishek maheshwari, Jul 12, 2016.

  1. abhishek maheshwari

    abhishek maheshwari

    Joined:
    Jul 12, 2016
    Messages:
    1
    Likes Received:
    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?
     
    abhishek maheshwari, Jul 12, 2016
    #1
    1. Advertisements

  2. abhishek maheshwari

    dg32768

    Joined:
    Jul 12, 2016
    Messages:
    1
    Likes Received:
    0
    The word while() in C occurs in two different constructs: in a while-loop
    Code (Text):
    while (condition)
        loop-body;
     
    and in a do-loop:
    Code (Text):
    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 (Text):
    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 (Text):
    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 (Text):
    do
    {
        body;
    } while (1);
    will also result in the body being repeated forever (or until a break/return is reached), whereas
    Code (Text):
    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 ;) }.
     
    dg32768, Jul 12, 2016
    #2
    1. Advertisements

  3. abhishek maheshwari

    atindra kumar nath

    Joined:
    Jul 12, 2016
    Messages:
    1
    Likes Received:
    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 (Text):
    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 (Text):
    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
     
    atindra kumar nath, Jul 12, 2016
    #3
  4. abhishek maheshwari

    Vishal Jamdagni

    Joined:
    Feb 6, 2018
    Messages:
    4
    Likes Received:
    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: Feb 13, 2018
    Vishal Jamdagni, Feb 12, 2018
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.