while loop


While Loop is used in the loops where you do not have to know how many times to repeat the action(If you need to know), but need to know when to end loop.

I have not understood the clear meaning.

When do you practically use which loop. what are advantages and disadvantages, limitations of each loop. Do we use DO-While loop, i have not clearly see any benefit of it over WHILE loop except the it exceutes once. What practical benefit we get by executing atleast once.

Please advise.

Any links resources ideas highly appreciated. Thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony GarciaDevops StaffCommented:
The three main kind of loops are

For Loops:
These are used when you know want to use the same code a certain number of times. This is useful for example, when you want to perform an operation for every item in an array. In these cases you usually know the size of the array, so you know how many times to perform the operation, one for each element.

While Loops:
While loops can be used to also do what you can do with a for loop, but they are used when the statement you are using is more complicated than counting a certain number of times.
This can be useful when dealing with dynamic variables that you do not know the size of. For example linked lists(http://en.wikipedia.org/wiki/Linked_list).
Say you want to get the last item in a linked list, but you don't know how long it is.
You can go through the list getting each item until the next item is null, at which point you know you are at the end of the list.

pseudo code
while(item->next != null)
   item= item->next;

Open in new window

Do While:
Do while are pretty much the same as while loops, but as you mentioned, the code is always executed at least once. This can be useful when you need to do something to the variable before you check it.

For made up example,
say you want to generate random numbers between 0 and 10 until you get a number higher than 10.

To do this with a while loop.
Pseudo code
x = random;
while (x <10)
   x = random;

Open in new window

In this example, you need to copy the code from inside the loop before it. In order to check what is in x, you need to generate the value for x first. The code to generate the value for x in this example is simple, but sometimes it can be more complicated and you have to copy most of the code inside the value to generate it first.

Using the do while loop simplifies it a bit.
while(x < 10)

Open in new window

These examples are using pseudo code, but hopefully they sort of explain the differences between them.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
See my comment in this EE Q
for an example of needing to use do - while().
gudii9Author Commented:
what is below code doing. can you please advise
        String input = null;
	char inChar;
	char[] ch = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

	Scanner s = new Scanner(System.in);

		System.out.print("Input a letter :");

		input = s.next();
		inChar = input.charAt(0);



		for(int d =0;d<=(int)inChar-97;d++){ // obtain (print) all chars up to and including the one entered.

Open in new window

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

The do - while just makes it easier to enter a loop when you don't yet have the value for the loop condition. The code above simply prompts the user for an alphabetic character, and the loop is there so that if the entry is multiple characters, (except for "exit"), then it keeps looping until the user enters just one. You could put the "input = s.next();" above the loop, and then use a plain while() loop, but in this case, the do allows you to do away with that. There are better examples, but mine was just one that happened to crop up so I posted it to you. In general, do - while is when you lack the stop condition var for the loop before entering it. Hope that helps.
gudii9Author Commented:
can you please point me to some simple example.
The important thing to remember  is only that the do while is useful when you don't know the stop condition in advance.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.