Comparing while and if statements
-
whileandifare 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:
ifstatements only execute the block of code once if the condition is true, butwhilestatements may execute the block of code multiple times if the condition is true -
Compare these snippets of code:
if(number < 3)
{
Console.WriteLine("Hello!");
Console.WriteLine(number);
number++;
}
Console.WriteLine("Done");and
while(number < 3)
{
Console.WriteLine("Hello!");
Console.WriteLine(number);
number++;
}
Console.WriteLine("Done");- If
numberis 4, then both will do the same thing: skip the block of code and display “Done”. - If
numberis 2, both will also do the same thing: Display “Hello!” and “2”, then incrementnumberto 3 and print “Done”. - If
numberis 1, there is a difference: Theifstatement will only display “Hello!” once, but thewhilestatement will display “Hello! 2” and “Hello! 3” before displaying “Done”
Code duplication in while loops
-
Since the
whileloop evaluates the condition before executing the code in the body (like anifstatement), you sometimes end up duplicating code -
For example, consider an input-validation loop like the one we wrote for Item prices:
Console.WriteLine("Enter the item's price.");
decimal price = decimal.Parse(Console.ReadLine());
while(price < 0)
{
Console.WriteLine("Invalid price. Please enter a non-negative price.");
price = decimal.Parse(Console.ReadLine());
}
Item myItem = new Item(desc, price);-
Before the
whileloop, 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
whileloop, 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
whileloop is necessary to givepricean initial value, so that we can check it for validity in thewhilestatement -
It would be nice if we could tell the
whileloop to execute the body first, and then check the condition
Introduction to do-while
-
The
do-whileloop executes the loop body before evaluating the condition -
Otherwise works the same as a
whileloop: 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:
decimal price;
do
{
Console.WriteLine("Please enter a non-negative price.");
price = decimal.Parse(Console.ReadLine());
} while(price < 0);
Item myItem = new Item(desc, price);-
The keyword
dostarts 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 < 0is evaluated at the end of the loop body, sopricehas 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
whileloop: 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 < 0is false -
Note that the variable
pricemust be declared before thedo-whileloop so that it is in scope after the loop. It would not be valid to declarepriceinside 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-whileloop is written like this:
do
{
<statements>
} while(<condition>);-
The
dokeyword does nothing, but it is required to indicate the start of the loop. You cannot just write a{by itself.-
Unlike a
whileloop, a semicolon is required afterwhile(<condition>) -
It’s a convention to write the
whilekeyword on the same line as the closing}, rather than on its own line as in awhileloop -
When the computer encounters a
do-whileloop, it first executes the body (code block), then evaluates the condition -
If the condition is true, the computer jumps back to the
dokeyword and executes the loop body again -
If the condition is false, execution continues to the next line after teh
whilekeyword -
If the loop body is only a single statement, you can omit the curly braces, but not the semicolon:
do <statement> while(<condition>); -
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-whileloop:
decimal price;
bool parseSuccess;
do
{
Console.WriteLine("Please enter a price (must be non-negative).");
parseSuccess = decimal.TryParse(Console.ReadLine(), out price);
} while(!parseSuccess || price < 0);
Item myItem = new Item(desc, price);-
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!parseSuccessis false), the loop should not stop unlessprice < 0is 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