Exercise 9. While-Loop and Boolean Expressions

The first looping construct I’ll show you is the while-loop, and it’s the simplest, useful loop you could possibly use in C. Here’s this exercise’s code for discussion:

ex9.c

 1   #include <stdio.h>  2  3   int main(int argc, char *argv[])  4   {  5       int i = 0;  6       while (i < 25) {  7           printf("%d", i);  8           i++;  9       } 10 11       return 0; 12   }

From this code, and from your memorization of the basic syntax, you can see that a while-loop is simply this:

while(TEST) {     CODE; }

It simply runs the CODE as long as TEST is true (1). So to replicate how the for-loop works, we need to do our own initializing and incrementing of i. Remember ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.