Difference between revisions of "Do-While Loops"

From CompSciWiki
Jump to: navigation, search
(Example: showInputDialog doesn't need the null)
(Edited text of entire page for readability / grammar / syntax etc.)
 
(5 intermediate revisions by 2 users not shown)
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=
  
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.
+
==Introduction==
 +
A do-while loop is similar to a while loop, except that the conditional statement and the boolean test is done at the '''bottom''' of the loop. This can be more natural in some cases, but the do-while loop is perhaps not as frequently used as the while loop, since with a little manipulation, any do-while loop can be written 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 15: Line 21:
 
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.
 
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 ==
+
=== 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:
+
Here's an example which repeatedly asks the user for input until the input is a positive number:
  
<pre>
+
{{CodeBlock
 +
|Code=
 +
int userInput;
 +
Scanner keyboard= new Scanner(System.in);
 +
 
 +
do {
 +
 
 +
  System.out.print("Please enter a positive integer to continue:");
 +
  userInput = keyboard.nextInt();
 +
 
 +
} while (userInput <= 0);
 +
}}
 +
 
 +
Notice that the loop doesn't need to be primed like a [[While_Loops|while loop]], since the test is not done at the top of the loop, only at the bottom. A full execution of the body of the loop will always be completed before the test condition is ever checked.
 +
 
 +
 
 +
== 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 offered for this is that when reading code top-to-bottom, it isn't obvious what a do-while loop does when you arrive at it: there's no 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:
 +
 
 +
{{CodeBlock
 +
|Code=
 
String input;
 
String input;
 
int userInput;
 
int userInput;
Line 26: Line 55:
 
   userInput = Integer.parseInt(input);
 
   userInput = Integer.parseInt(input);
 
} while (userInput <= 0);
 
} while (userInput <= 0);
</pre>
+
}}
 +
 
 +
Can be rewritten as a while loop:
 +
 
 +
{{CodeBlock
 +
|Code=
 +
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, you need to:
 +
# 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.
 +
}}

Latest revision as of 17:43, 7 December 2011

COMP 1010 Home > Loops


Introduction

A do-while loop is similar to a while loop, except that the conditional statement and the boolean test is done at the bottom of the loop. This can be more natural in some cases, but the do-while loop is perhaps not as frequently used as the while loop, since with a little manipulation, any do-while loop can be written 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 input until the input is a positive number:

 int userInput;
Scanner keyboard= new Scanner(System.in); 

do { 
   
   System.out.print("Please enter a positive integer to continue:");
   userInput = keyboard.nextInt();

} while (userInput <= 0); 

Notice that the loop doesn't need to be primed like a while loop, since the test is not done at the top of the loop, only at the bottom. A full execution of the body of the loop will always be completed before the test condition is ever checked.


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 offered for this is that when reading code top-to-bottom, it isn't obvious what a do-while loop does when you arrive at it: there's no 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, 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.
Previous Page: While Loops Next Page: Do-While Loops