Comparing while
and if
statements
-
while
andif
are very similar: Both test a condition, execute a block of code if the condition is true, and skip the block of code if the condition is false -
There is only a difference if the condition is true:
if
statements only execute the block of code once if the condition is true, butwhile
statements may execute the block of code multiple times if the condition is true -
Compare these snippets of code:
and
- If
number
is 4, then both will do the same thing: skip the block of code and display “Done”. - If
number
is 2, both will also do the same thing: Display “Hello!” and “2”, then incrementnumber
to 3 and print “Done”. - If
number
is 1, there is a difference: Theif
statement will only display “Hello!” once, but thewhile
statement will display “Hello! 2” and “Hello! 3” before displaying “Done”
Code duplication in while
loops
-
Since the
while
loop evaluates the condition before executing the code in the body (like anif
statement), you sometimes end up duplicating code -
For example, consider an input-validation loop like the one we wrote for Item prices:
-
Before the
while
loop, we wrote two lines of code to prompt the user for input, read the user’s input, convert it todecimal
, and store it inprice
-
In the body of the
while
loop, we also wrote two lines of code to prompt the user for input, read the user’s input, convert it todecimal
, and store it inprice
-
The code before the
while
loop is necessary to giveprice
an initial value, so that we can check it for validity in thewhile
statement -
It would be nice if we could tell the
while
loop to execute the body first, and then check the condition
Introduction to do-while
-
The
do-while
loop executes the loop body before evaluating the condition -
Otherwise works the same as a
while
loop: If the condition is true, execute the loop body again; if the condition is false, stop the loop -
This can reduce repeated code, since the loop body is executed at least once
-
Example:
-
The keyword
do
starts the code block for the loop body, but it does not have a condition, so the computer simply starts executing the body -
In the loop body, we prompt the user for input, read and parse the input, and store it in
price
-
The condition
price < 0
is evaluated at the end of the loop body, soprice
has its initial value by the time the condition is evaluated -
If the user entered a valid price, and the condition is false, execution simply proceeds to the next line
-
If the user entered a negative price (the condition is true), the computer returns to the beginning of the code block and executes the loop body again
-
This has the same effect as the
while
loop: the user is prompted repeatedly until he/she enters a valid price, and the program can only reach the lineItem myItem = new Item(desc, price)
whenprice < 0
is false -
Note that the variable
price
must be declared before thedo-while
loop so that it is in scope after the loop. It would not be valid to declareprice
inside the body of the loop (e.g. on the line withdecimal.Parse
) because then its scope would be limited to inside that code block.
Formal syntax and details of do-while
- A
do-while
loop is written like this:
-
The
do
keyword does nothing, but it is required to indicate the start of the loop. You cannot just write a{
by itself.-
Unlike a
while
loop, a semicolon is required afterwhile(<condition>)
-
It’s a convention to write the
while
keyword on the same line as the closing}
, rather than on its own line as in awhile
loop -
When the computer encounters a
do-while
loop, it first executes the body (code block), then evaluates the condition -
If the condition is true, the computer jumps back to the
do
keyword and executes the loop body again -
If the condition is false, execution continues to the next line after teh
while
keyword -
If the loop body is only a single statement, you can omit the curly braces, but not the semicolon:
-
do-while
loops with multiple conditions
- We can combine both types of user-input validation in one loop:
Ensuring the user entered a number (not some other string), and
ensuring the number is valid. This is easier to do with a
do-while
loop:
-
There are two parts to the loop condition: (1) it should be true if the user did not enter a number, and (2) it should be true if the user entered a negative number.
-
We combine these two conditions with
||
because either one, by itself, represents invalid input. Even if the user entered a valid number (which means!parseSuccess
is false), the loop should not stop unlessprice < 0
is also false. -
Note that both variables must be declared before the loop begins, so that they are in scope both inside and outside the loop body