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);
}
Answer from alseether on Stack Overflowjava - do-while and while comparison - Stack Overflow
Java: When to use a While Loop?
Loops in Java
A very simple java do...while loop - Stack Overflow
Videos
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.
My Intro to programming class has covered how to use both for and while loops. As for the code I am good to go with it but I am having a little trouble understanding them.
When would I use a while loop? Why would that be the better choice for the loop over a for loop?
Though I know how to code them I do not quite have a full comprehension on determining which to use and why one would be better than the other.
Any examples and/or like laymen's break down would be much appreciated!
Loops are killing me, it’s hard to figure out when to use them, and how to use them. I’m currently looking through YouTube for videos and resources on how to better understand them, but if anyone can recommend a good video on YouTube that is helpful I would appreciate the help.
Because when you type a and hit Enter, then the in.read() method returns three characters - 'a', the character for carriage return \r and the character for line break ('\n'). (Note that the combination \r\n is considered as line-break under Windows).
Your program will have significantly different behavior if you run it under Linux or OSX, because line-breaking character(s) is specific to the OS you're running the program on. Under Linux, it will be \n, under OS X-9 it will be \r, for example.
As a work-around, you can read the whole line (by using a Scanner) and trim it, which will omit the line-breaking character (disregarding the OS type):
public static void main (String args[]) throws java.io.IOException {
String line;
Scanner sc = new Scanner(System.in);
do {
System.out.println("Please a key followed by ENTER:");
line = sc.readLine().trim();
} while (!"q".equals(line));
}
I assume what gets read as input is containing your ENTER keypress. Since you are on Windows, this includes the CRFL line ending.
Thus, each time you entered your char, you actually input 3 chars. For your first input:
- a
- CR
- LF
Try reading a full line via a BufferedReader and run a whitespace trim on that, or just evaluate its first char.
New to Java and trying to get more familiar with loops. I am trying to make a do while loop to do a simple loan calculation (user inputs amount of loan, yearly interest, monthly payment). The problem is, I want to use the new number after the equation is run the first time as the starting number for the next time the program runs the equation(x+y=z, use z as the starting point for z+y=a, so on and so forth) and I can’t figure it out. Suggestions?
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_")
'''
So i am in the middle of learning Java, and today i worked on loops. Specifically for and while loops. However, when i was given this problem to solve on codeacdemy, i noticed this.
While Loop
Examples 2:
class Coffee {
public static void main(String[] args) {
// initialize cupsOfCoffee
int cupsOfCoffee = 1;
// add while loop with counter
while(cupsOfCoffee <= 100) {
System.out.println("Fry drinks cup of coffee #" +cupsOfCoffee );
cupsOfCoffee ++;
}
}
}
Compared to:
For Loops:
class Coffee {
public static void main(String[] args) {
for (int cupsOfCoffee = 1;cupsOfCoffee <= 100;cupsOfCoffee++) {
System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}
}
}
For some reason, even though they pretty much resulted in the same output, for loops and while loops do pretty much the same thing, but for loops feel a tiny bit more simple(imo).