By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

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

P: 1
Please give me an exact definition of those looping statements.
Aug 31 '10 #1
Share this Question
Share on Google+
3 Replies


100+
P: 201
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!
Aug 31 '10 #2

Expert 100+
P: 2,400
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.)
Aug 31 '10 #3

100+
P: 1,059
in c while loop continue only if it get a none zero value

take an example
Expand|Select|Wrap|Line Numbers
  1. while(0)
  2. {
  3.    printf("Never reach\n");
  4. }
  5. i=0;
  6. while(i)
  7. {
  8.    printf("Never reach\n");
  9. }
  10.  
  11. while(1)
  12. {
  13.    printf("what ever\n");
  14. }
  15.  
  16.  
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
Expand|Select|Wrap|Line Numbers
  1. i=1;
  2. while(i<=10)
  3. {
  4.  printf("%d\n",i);
  5.  i++;
  6. }
  7.  
  8. i=1;
  9. con=i<=10;
  10. while(con)
  11. {
  12.  printf("%d\n",i);
  13.  i++;
  14.  con=i<=10;
  15. }
  16.  
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.
Aug 31 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.