# The WHILE keyword.

The while keyword is related to do and for. Its purpose is to repeatedly execute a block of statements. Here is an example :

            main()
            {
                int i=5;

                while(--i)
                {
                    printf(" i is %d\n", i);
                }
            }

The expression i-- is evaluated and if its true the statements in the block are executed. The loop continues until the expression is false (zero). The result will look like this:

                    i is 4
                    i is 3
                    i is 2
                    i is 1

It is important to note that the statements on a while will not get executed if the first evaluation of the expression is FALSE. If you do not want this to happen you may prefer to use the do statement.

Now consider the next example.

            main()
            {
                int i=5;

                while(--i);
                {
                    printf(" i is %d\n", i);
                }
            }

The result will look like this:

                    i is 0

This is because of the ; on the end of the while statement which means the while will loop (executing NULL statements) until i is zero. Execution will then continue down the program (to the printf).

## Examples:

Basic while.

## See also:

- for keyword.
- do keyword.
- break keyword.
- continue keyword.

Top | Master Index | Keywords | Functions