In the do-while syntax code block will always be run at least once. It is very useful when you want to execute at least first statement of particular code.
Syntax of Do while statement is as follows.
Code:
do {
code_block;
} while (condition);
If you don't understand how to use do-while statement then try to understand following example.
Code:
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
int k = -1;
do {
if (k == -1) System.out.print("Hello to all ");
else {
System.out.print(args[k]);
System.out.print(" ");
}
k = k + 1;
} while (k < args.length);
System.out.println();
}
}
Bookmarks