Difference between revisions of "Do-While Loops"

From CompSciWiki
Jump to: navigation, search
m
(Formatted opening code and code blocks.)
Line 1: Line 1:
{{1010Topic|Introduction=The do-while loop is similar to a while loop, except that the condition is tested at the bottom of the loop.|Chapter_TOC=[[Loops]]}}
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Loops]]
 +
|Previous=[[While Loops]]
 +
|Next=[[Do-While Loops]]
 +
|Body=
  
 +
==Introduction==
 
A do-while loop is similar to a while loop, except that the boolean test is done at the '''bottom''' of the loop. This can be more natural in some loops, but the do-while loop is perhaps not as frequently used as while loops, since with a little manipulation, you can rewrite any do-while loop as a regular while loop.
 
A do-while loop is similar to a while loop, except that the boolean test is done at the '''bottom''' of the loop. This can be more natural in some loops, but the do-while loop is perhaps not as frequently used as while loops, since with a little manipulation, you can rewrite any do-while loop as a regular while loop.
  
 
== Syntax ==
 
== Syntax ==
  
<pre>
+
{{CodeBlock
 +
|Code=
 
do {  
 
do {  
 
   stmts;
 
   stmts;
 
} while (boolExpr);
 
} while (boolExpr);
</pre>
+
}}
  
 
Note that there '''is''' a semicolon after the boolean expression.  
 
Note that there '''is''' a semicolon after the boolean expression.  
Line 19: Line 25:
 
Here's an example which repeatedly asks the user for an input until the input is a positive number:
 
Here's an example which repeatedly asks the user for an input until the input is a positive number:
  
<pre>
+
{{CodeBlock
 +
|Code=
 
String input;
 
String input;
 
int userInput;
 
int userInput;
Line 26: Line 33:
 
   userInput = Integer.parseInt(input);
 
   userInput = Integer.parseInt(input);
 
} while (userInput <= 0);
 
} while (userInput <= 0);
</pre>
+
}}
  
 
Notice that the loop doesn't need priming like a [[While_Loops|while loop]], since the test is not done at the top of the loop, only at the bottom, once a full execution of the body of the loop has been completed.
 
Notice that the loop doesn't need priming like a [[While_Loops|while loop]], since the test is not done at the top of the loop, only at the bottom, once a full execution of the body of the loop has been completed.
Line 37: Line 44:
 
Luckily, we can unroll do-while loops and replace them with a while loop which has been properly primed. For instance,  the do-while loop from the first example
 
Luckily, we can unroll do-while loops and replace them with a while loop which has been properly primed. For instance,  the do-while loop from the first example
  
<pre>
+
{{CodeBlock
 +
|Code=
 
String input;
 
String input;
 
int userInput;
 
int userInput;
Line 44: Line 52:
 
   userInput = Integer.parseInt(input);
 
   userInput = Integer.parseInt(input);
 
} while (userInput <= 0);
 
} while (userInput <= 0);
</pre>
+
}}
  
 
can be rewritten as a while loop:  
 
can be rewritten as a while loop:  
  
<pre>
+
{{CodeBlock
 +
|Code=
 
String input;
 
String input;
 
int userInput;
 
int userInput;
Line 59: Line 68:
 
   userInput = Integer.parseInt(input);
 
   userInput = Integer.parseInt(input);
 
}  
 
}  
</pre>
+
}}
  
 
To convert a do-while loop to a while loop this way, you need to:
 
To convert a do-while loop to a while loop this way, you need to:
 
# Take the entire body of the loop and paste it before the new while loop.
 
# Take the entire body of the loop and paste it before the new while loop.
 
# Take the conditional expression from the end of the do-while loop and place it at the top of the while loop.
 
# Take the conditional expression from the end of the do-while loop and place it at the top of the while loop.

Revision as of 16:48, 7 December 2011

{{Template:1010Topic |Chapter_TOC=Loops |Previous=While Loops |Next=Do-While Loops |Body=

Introduction

A do-while loop is similar to a while loop, except that the boolean test is done at the bottom of the loop. This can be more natural in some loops, but the do-while loop is perhaps not as frequently used as while loops, since with a little manipulation, you can rewrite any do-while loop as a regular while loop.

Syntax

 do { 
   stmts;
} while (boolExpr); 

Note that there is a semicolon after the boolean expression.

One important point to note about do-while loops is that the loop will always execute once: the boolean condition is not tested until the bottom of the loop, 'after the loop body has been executed once. This may make things easier in some cases, but it may not be what you want in other cases. Make sure that if you're using a do-while loop, it makes sense that the code inside the loop is executed at least once.

Example: Waiting for valid user input

Here's an example which repeatedly asks the user for an input until the input is a positive number:

 String input;
int userInput;
do { 
   input = JOptionPane.showInputDialog("Enter a positive integer");
   userInput = Integer.parseInt(input);
} while (userInput <= 0); 

Notice that the loop doesn't need priming like a while loop, since the test is not done at the top of the loop, only at the bottom, once a full execution of the body of the loop has been completed.


Unrolling Do-While Loops

While you can use do-while loops in COMP 1010, some people prefer not to read them or write them in their code. One explanation sometimes offered for this is it isn't obvious when you are reading the code top-to-bottom what a do-while loop does when you arrive at it: there's not explanation of the loop condition, and it can be difficult to search through the code to find the end of the while loop.

Luckily, we can unroll do-while loops and replace them with a while loop which has been properly primed. For instance, the do-while loop from the first example

 String input;
int userInput;
do { 
   input = JOptionPane.showInputDialog("Enter a positive integer");
   userInput = Integer.parseInt(input);
} while (userInput <= 0); 

can be rewritten as a while loop:

 String input;
int userInput;

input = JOptionPane.showInputDialog("Enter a positive integer");
userInput = Integer.parseInt(input);

while (userInput <= 0) { 
   input = JOptionPane.showInputDialog("Enter a positive integer");
   userInput = Integer.parseInt(input);
} 

To convert a do-while loop to a while loop this way, you need to:

  1. Take the entire body of the loop and paste it before the new while loop.
  2. Take the conditional expression from the end of the do-while loop and place it at the top of the while loop.