do while loop - Java Basic
What's the purpose of "do...while" loop? Can't the same result be achieved with just "while" loop?
do while - Java do loop repeat until valid entry - Stack Overflow
java - do-while and while comparison - Stack Overflow
What are looping statements in Java
What are the types of looping statements in Java
Can you nest loops in Java
Videos
It seems like all the examples I've seen of "do...while" loop can be done with just "while" loop.
For example:
'''
var number = 6
var factorial = 1
do {
factorial *= number
number--
}while(number > 0)
println("Factorial of 6 is $factorial")
'''
is the same as
'''
var number_ = 6
var factorial_ = 1
while (number_ > 0) {
factorial_ *= number_
number_--
}
print ("factorial_ is $factorial_")
'''
In general, use break inside a while loop is considered a bad practice. You should have something like this
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int minimum = 5;
int maximum = 15;
do{
System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":" );
number = input.nextInt();
if (number < minimum || number > maximum)
System.out.print("Sorry, invalid");
} while (number < minimum || number > maximum);
}
There is a logical error in your code in the placement of your break and scanner input. If you really want to stay true to what you have, I find it much simpler to just add a boolean check instead of using a break. Below you can see I also put final so that way you know, and is a best practice that, that variable should not be altered or tampered with in code. I added a boolean since you wanted to check the do while for false, then we set it to true if it passes the if statement you made.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
boolean check = false;
// Final, because they are constant through-out the program
final int minimum = 5;
final int maximum = 15;
do {
System.out.print("Enter a number between " + minimum + " and " + maximum + ":" );
number = input.nextInt();
if (number >= minimum && number <= maximum)
check = true;
else
System.out.println("Sorry, invalid");
break;
} while (check);
}
The difference between a do-while and a while is when the comparison is done. With a do-while, you'll compare at the end and hence do at least one iteration.
Equivalent code for your example
do
{
i++;
++j;
System.out.println( i * j );
}
while ((i < 10) && (j*j != 25));
is equivalent to:
i++;
++j;
System.out.println( i * j );
while ((i < 10) && (j*j != 25)) {
i++;
++j;
System.out.println( i * j );
}
General comprehension
A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop might not even be executed.
do {
<block>
} while (<condition>);
is equivalent to:
<block>
while (<condition>) {
<block>
};
Use case
A typical use case for a do-while is the following: you ask the user something and you want do repeat the operation while the input is not correct.
do {
// Ask something
} while (input is not correct);
In that case, you want to ask at least once and it's usually more elegant than using a while which would require either to duplicate code, or to add an extra condition or setting an arbitrary value to force entering the loop the first time.
At the opposite, while loops are much more commons and can easily replace a do-while (not all languages have both loops).
The key difference between do-while and while, with do-while you are guaranteed at least one run of your code before the checks.
*It does not need to get anymore complicated than that.