No. A for
loop is the best choice
for a counting loop.
The example used the do
in
a counting loop.
This was to show how it worked.
Usually you would use a for
loop.
A better application of a do
is where
the user is repeatedly asked if the program
should continue.
The user is required to answer,
so the loop body will execute at least once,
so using a bottom-driven loop is safe.
import java.io.* ; class SqrtCalc { public static void main( String[] args ) throws IOException { String chars; double x; BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) ); do { System.out.print("Enter a number-->"); chars = stdin.readLine(); x = Double.parseDouble( chars ); System.out.println("Square root of" + x + " is " + Math.sqrt( x ) ); System.out.print("Do you wish to continue? (yes or no) -->"); chars = stdin.readLine(); } while ( chars.equals( "yes" ) ); } }