i was looking at this code and thinking when is it a good idea to use the do-while method.

import java.io.*;
class ValidHex
{
	public static void main(String[] args)
	{
		Console console=System.console();
		String hex;
		boolean valid;
	     do{
		System.out.println("Please enter a valid hexadecimal number");
		
		hex=console.readLine();
		if(hex.length()==0)
			valid=false;
		else
			valid=true;

		for(int index=0;index<hex.length() && valid; index++)
		{
			switch(hex.charAt(index))
			{
				case '0': case '1': case '2': case '3': case '4': case '5': case '6':
				case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd':
				case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E':
				case 'F':
					break;
				default:
					valid=false;
					break;
			}
		}
		if(!valid)
			System.out.println("Illegal number");
	      }while(!valid); // why do i need to use that do-while? what is it good for?

	}



}

Recommended Answers

All 7 Replies

You have a block of code in a do { } while(! valid); loop
The code will be executed once.
If, after that, the boolean valid is not true this indicates that something was not correct about the execution, so the loop is executed again.
The do/while will keep on executing the code over and over again until it results in valid == true.
This is a very common way to use do/while.

Should we delete the break; in the line 29
since the

default:
valid=false;
break;

is the last case.

That last break; is redundant but harmless. On balance it may be a good idea to get in the habit of always remembering to put in a break; because when people forget the break; (ecept on the last case, of course) they end up with bugs that can be very hard to find.

so, james, what would happen if it wasnt do while and while, would it work any differently?

Yes, of course. The code inside the loop would execute exactly once, regardless of whether the final reult was valid or not. If the user entered an invalid char they would not have a chance to try again.

The difference between do-while{} and while{} is simply that the while{} evaluates the test condition before the first iteration, and only executes the first time if it's true, where do-while{} executes once before evaluating.

I usually avoid do-while{} for readability, because I'm used to finding the loop condition at the top of the loop. My usual idiom for do-while is something like this:

boolean notDoneYet=true;
while (notDoneYet) 
{ do stuff}

Obviously, this is equivalent to the do-while, in that it will always take the first time through, but to me it's easier to read. Performance should be identical.

ooh . alright, now it is clear to me..

i was worried that do while was put inside on purpose cause it was different enough from teh while method to make the code work

thank you jon and james, you helped me understand my question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.