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
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    14,195
  2. Flip
    Replies:
    0
    Views:
    496
  3. Joona I Palaste
    Replies:
    14
    Views:
    1,785
    Fred Bloggs
    Jan 20, 2004
  4. Uday Bidkar
    Replies:
    4
    Views:
    818
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Logan Lee

    Difference between while and do while

    Logan Lee, Dec 24, 2007, in forum: C Programming
    Replies:
    9
    Views:
    666
    James Kuyper
    Dec 25, 2007
  6. Vijay Mathew
    Replies:
    0
    Views:
    885
    Vijay Mathew
    Mar 23, 2010
  7. Kit
    Replies:
    0
    Views:
    553
  8. alxcam8
    Replies:
    0
    Views:
    528
    alxcam8
    Jun 29, 2016
Loading...