What does while(1) and while(condition); statement mean in c language?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sury1591
    New Member
    • Aug 2010
    • 1

    #1

    What does while(1) and while(condition); statement mean in c language?

    Please give me an exact definition of those looping statements.
  • wizardry
    New Member
    • Jan 2009
    • 201

    #2
    its called conditional testing for further processing!

    for conditional testing you have:
    //export value of a

    export a=40
    export x=$a

    // test conditions are used for instance if you only want to show 10 records of something, you can store the numbers in keys then go to the next 10 records but that would start at 0 because 0 is 1 in binary.

    while ($x >= 0 )
    do {

    echo "the value of $x is this is loop $a";
    $x++
    }
    let $a=a+1
    done;

    what this does is loop until x is greater then or equal to 40. x is defined as a. for a simple example.

    *************** *************** *************** ************

    while true
    do
    echo "this is a test";
    sleep 5
    clear
    done

    so this is an infinate loop until you parse a eof or ^X or ^C to the shell.

    the while loop in respective is similar to the for loop your testing the clauses!

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      while(expression)
      ... body
      The body executes repeatedly until the expression is found to be equal to zero.

      while(1)
      ... body
      In this case, the expression is "1". The body executes repeatedly until 1 is found to be equal to zero. Of course, 1 will never be equal to 0 so the loop repeats forever.

      You can do the same with do-while. The only difference is whether the expression is compared to zero before or after each execution of the body.
      do
      ... body
      ... while(expression);

      A common idiom is do body while(0); In this case, the expression is unconditionally equal to zero so the body executes exactly once.

      ("..." is an artifact I put there to indent the lines.)

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        in c while loop continue only if it get a none zero value

        take an example
        Code:
        while(0)
        {
           printf("Never reach\n");
        }
        i=0;
        while(i)
        {
           printf("Never reach\n");
        }
        
        while(1)
        {
           printf("what ever\n");
        }
        in the above example there is two loop. in the first loop and in the second loop "never reach" will never be printed because the statement is zero (false)

        now conditional
        Code:
        i=1;
        while(i<=10)
        {
         printf("%d\n",i);
         i++;
        }
        
        i=1;
        con=i<=10;
        while(con)
        {
         printf("%d\n",i);
         i++;
         con=i<=10;
        }
        In this above example both of the loop will print from 1 to 10.

        The obvious question is why? The reason is if i<=10 is true then it will return 1 else it will return 0.

        Comment

        Working...