do while loop - Java Basic
Java: When to use a While Loop?
java - do-while and while comparison - Stack Overflow
A very simple java do...while loop - Stack Overflow
Videos
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!
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.
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.