Skip to content

while

A while loop is structured as follows, wherein expression is a Boolean expression and statement is one or more lines of code:

while (expression)
{
    statement
}

If expression is true, then statement will be executed. If, after that, expression is still true, then statement will be executed again. And so on, until expression is false.

For instance, consider the examples, below, of a cat that meows three times, each of which is better than the last:

How does it work?
  1. Line 5 defines a variable, i, of type int, with a value of 3.
  2. Line 6 checks whether or not i (i.e., 3) equals 0. It does not, so...
  3. Line 7 prints meow\n.
  4. Line 8 decrements i by 1, so i is now 2.
  5. Line 6 checks whether or not i (i.e., 2) equals 0. It does not, so...
  6. Line 7 prints meow\n.
  7. Line 8 decrements i by 1, so i is now 1.
  8. Line 6 checks whether or not i (i.e., 1) equals 0. It does not, so...
  9. Line 7 prints meow\n.
  10. Line 8 increments i by 1, so i is now 0.
  11. Line 6 checks whether or not i (i.e., 0) equals 3. It does, so the loop exits.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
    int i = 3;
    while (i != 0)
    {
        printf("meow\n");
        i--;
    }
}

In this example, we count from 3 down to 0. Think of i as a counter representing the number of times that the cat will meow.

Control Flow

flowchart LR
    A([start]) --> B[i = 3]
    B --> C{i != 0}
    C -- true --> D["#quot;meow#quot;"]
    D --> E[i--]
    E --> C
    C -- false --> F([stop])
How does it work?
  1. Line 5 defines a variable, i, of type int, with a value of 3.
  2. Line 6 checks whether i (i.e., 3) is greater than 0. It is, so...
  3. Line 7 prints meow\n.
  4. Line 8 decrements i by 1, so i is now 2.
  5. Line 6 checks whether i (i.e., 2) is greater than 0. It is, so...
  6. Line 7 prints meow\n.
  7. Line 8 decrements i by 1, so i is now 1.
  8. Line 6 checks whether i (i.e., 1) is greater than 0. It is, so...
  9. Line 7 prints meow\n.
  10. Line 8 increments i by 1, so i is now 0.
  11. Line 6 checks whether i (i.e., 0) is greater than 0. It is not, so the loop exits.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
    int i = 3;
    while (i > 0)
    {
        printf("meow\n");
        i--;
    }
}

In this example, instead of !=, we use >, which is arguably better insofar as it mitigates a potential bug. For instance, suppose that the value of i is 1 and that we accidentally decrement i by 2 (because of some future bug), so its new value is -1. We might thus "skip over" 0 altogether, and the loop might never exit. By using > instead, we ensure that the loop will exit if i ever equals 0 or is less than 0.

Control Flow

flowchart LR
    A([start]) --> B[i = 3]
    B --> C{i > 0}
    C -- true --> D["#quot;meow#quot;"]
    D --> E[i--]
    E --> C
    C -- false --> F([stop])
How does it work?
  1. Line 5 defines a variable, i, of type int, with a value of 1.
  2. Line 6 checks whether i (i.e., 1) is less than or equal to 3. It is, so...
  3. Line 7 prints meow\n.
  4. Line 8 increments i by 1, so i is now 2.
  5. Line 6 checks whether i (i.e., 2) is less than or equal to 3. It is, so...
  6. Line 7 prints meow\n.
  7. Line 8 increments i by 1, so i is now 3.
  8. Line 6 checks whether i (i.e., 3) is less than or equal to 3. It is, so...
  9. Line 7 prints meow\n.
  10. Line 8 increments i by 1, so i is now 4.
  11. Line 6 checks whether i (i.e., 4) is less than or equal to 3. It is not, so the loop exits.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
    int i = 1;
    while (i <= 3)
    {
        printf("meow\n");
        i++;
    }
}

In this example, we count from 1 up through 3. Think of i as a counter representing the cat's first (1st), second (2nd), and third (3rd) meows.

Control Flow

flowchart LR
    A([start]) --> B[i = 1]
    B --> C{i <= 3}
    C -- true --> D["#quot;meow#quot;"]
    D --> E[i++]
    E --> C
    C -- false --> F([stop])
How does it work?
  1. Line 5 defines a variable, i, of type int, with a value of 0.
  2. Line 6 checks whether i (i.e., 0) is less than 3. It is, so...
  3. Line 7 prints meow\n.
  4. Line 8 increments i by 1, so i is now 1.
  5. Line 6 checks whether i (i.e., 1) is less than 3. It is, so...
  6. Line 7 prints meow\n.
  7. Line 8 increments i by 1, so i is now 2.
  8. Line 6 checks whether i (i.e., 2) is less than 3. It is, so...
  9. Line 7 prints meow\n.
  10. Line 8 increments i by 1, so i is now 3.
  11. Line 6 checks whether i (i.e., 3) is less than 3. It is not, so the loop exits.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main(void)
{
    int i = 0;
    while (i < 3)
    {
        printf("meow\n");
        i++;
    }
}

In this example, we count from 0 up to, but not including, 3. Think of i as a counter representing how many times the cat has meowed.

Control Flow

flowchart LR
    A([start]) --> B[i = 0]
    B --> C{i < 3}
    C -- true --> D["#quot;meow#quot;"]
    D --> E[i++]
    E --> C
    C -- false --> F([stop])