While Statement

The while keyword is used to conditionally iterate a statement. The syntax of the while statement is:

while (expression) statement

The statement executes repeatedly until the value of expression is false. The test takes place before statement is executed. Thus, if expression evaluates to false on the first pass, the loop does not execute. Note that parentheses around expression are mandatory.

Here is an example of calculating scalar product of two vectors, using the while statement:

int s = 0, i = 0;
while (i < n) {
  s += a[i] * b[i];
  i++;
}

Note that body of the loop can be a null statement. For example:

while (*q++ = *p++);
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code