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?
- Line 5 defines a variable,
i, of typeint, with a value of3. - Line 6 checks whether or not
i(i.e.,3) equals0. It does not, so... - Line 7 prints
meow\n. - Line 8 decrements
iby1, soiis now2. - Line 6 checks whether or not
i(i.e.,2) equals0. It does not, so... - Line 7 prints
meow\n. - Line 8 decrements
iby1, soiis now1. - Line 6 checks whether or not
i(i.e.,1) equals0. It does not, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now0. - Line 6 checks whether or not
i(i.e.,0) equals3. It does, so the loop exits.
1 2 3 4 5 6 7 8 9 10 11 | |
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?
- Line 5 defines a variable,
i, of typeint, with a value of3. - Line 6 checks whether
i(i.e.,3) is greater than0. It is, so... - Line 7 prints
meow\n. - Line 8 decrements
iby1, soiis now2. - Line 6 checks whether
i(i.e.,2) is greater than0. It is, so... - Line 7 prints
meow\n. - Line 8 decrements
iby1, soiis now1. - Line 6 checks whether
i(i.e.,1) is greater than0. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now0. - Line 6 checks whether
i(i.e.,0) is greater than0. It is not, so the loop exits.
1 2 3 4 5 6 7 8 9 10 11 | |
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?
- Line 5 defines a variable,
i, of typeint, with a value of1. - Line 6 checks whether
i(i.e.,1) is less than or equal to3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now2. - Line 6 checks whether
i(i.e.,2) is less than or equal to3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now3. - Line 6 checks whether
i(i.e.,3) is less than or equal to3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now4. - Line 6 checks whether
i(i.e.,4) is less than or equal to3. It is not, so the loop exits.
1 2 3 4 5 6 7 8 9 10 11 | |
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?
- Line 5 defines a variable,
i, of typeint, with a value of0. - Line 6 checks whether
i(i.e.,0) is less than3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now1. - Line 6 checks whether
i(i.e.,1) is less than3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now2. - Line 6 checks whether
i(i.e.,2) is less than3. It is, so... - Line 7 prints
meow\n. - Line 8 increments
iby1, soiis now3. - Line 6 checks whether
i(i.e.,3) is less than3. It is not, so the loop exits.
1 2 3 4 5 6 7 8 9 10 11 | |
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])