PDA

View Full Version : Difference between for, while and do while loops?




stevento
Mar 4, 2007, 10:33 PM
What is the difference between those loops?

i did this:

int count;
int noExpenses;
no Expenses = 5; //info that i read it with scan.nextInt
do while (count = 0, count < 5, count++)
and that didn't work.
help?



Spritey
Mar 4, 2007, 11:45 PM
What is the difference between those loops?

i did this:

int count;
int noExpenses;
no Expenses = 5; //info that i read it with scan.nextInt
do while (count = 0, count < 5, count++)
and that didn't work.
help?

Not quite sure what you're going to do, but in a do/while loop, you set a condition when you want the loop to stop. a do/while loop is not counter controlled, rather it checks to see if a condition is met.

You use a do/while loop when you know you have to enter the loop at least once, such as if you're getting input from a user, and if you need to verify the user's entry

YOu need to set up a do/while loop like this


do {
Insert whatever needs to be done in the loop
} while(insert condition that needs to be true for the loop to continue)

i.e. you'll have the user input something 5 times

do{

ask user to input whatever
counter++
} while(counter<=5)

Here you insert whatever needs to occur after you've gotten 5 user inputs total

To summarize:

do/while loop: Use when you have to enter the loop at least once, very often used when you have to parse user input from string to int/double, where it's easy for number format exception to occur (in this case you'll most likely use a boolean value in combination with structured exception handlers/try- catch)

while: when you need to check a condition. You will ONLY enter the loop if the condition is true, unlike a do/while, where the condition is checked at the bottom of the loop, meaning you'll enter the loop AT LEAST once.

for (traditional for-loop): Generally use when you know how many times you need to enter the loop. Depending on what you're doing, it's easily confusable with the do/while loop. However, depending on where you declare your counter/condition, you might not necessarily enter a for-loop, whereas you'll always enter the do/while loop at least once since the condition is checked at the bottom of the loop, and not the beginning.


Hope it helps some. Only have one semester of java, and it's been a couple o months...

Spritey
Mar 4, 2007, 11:52 PM
And just as a general tip which I've found very useful once your programs become more complex and you need to parse values etc:

Use prefixes like i, d, etc like: String sCharacters, int iCounter, int iValue, double dNumber, aiIntegerArray,


Also use as descriptive variable names as you can, even though your variable names become a bit longer. It also makes it easier for people to understand your code and for yourself to understand the flow of your code.


Anyway, depends on your programming standards and it's just something that I've found useful myself as a new java programmer.

LtRammstein
Mar 5, 2007, 01:12 PM
Seeing that you probably already got the answer, here's what I think of it:

index
while(condition) {
task
index++
}

for(index; condition; index++){
task
}

The do actually makes the while loop go through once. So if you know the condition in the while loop will be false once, but it must occur, the do will jump into the while loop and do it once.

Example:

index
do {
while(condition) {
task
index++
}
}

savar
Mar 5, 2007, 02:00 PM
What is the difference between those loops?

i did this:

int count;
int noExpenses;
no Expenses = 5; //info that i read it with scan.nextInt
do while (count = 0, count < 5, count++)
and that didn't work.
help?

Wow, you are way off...what are you trying to do? (And in what language?)

The various loops (for, while, and do-while) can all be used to achieve the same thing. The reason for having three equivalent notations is that some are more compact for expressing certain kinds of loops.

for: execute a loop with initial, intermediate, and end states
while: execute a loop while a condition is true
do-while: execute a code block, if the condition is true then execute again

Examples (Java):

for

for (int i=0; i<10; i++) {
System.out.println("i="+i);
}

Prints:
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9


while

int n = 100;
while (n > 20) {
System.out.println("n="+n);
n = n/2;
}

Prints:
n=100
n=50
n=25


do-while

int p = 10;
do {
System.out.println("p="+p);
p = p/2;
} while (p > 20);

Prints:
p=10

bbarnhart
Mar 5, 2007, 02:36 PM
FWIW: For and while are syntactically different, but achieve the same thing. For clarity, I always use for loops that have the initializer, loop test and counter all next to each other.

While loops just have a loop test. The initializer is done before the loop and the counter is done inside the loop.

You also don't need the initializer or the counter;

For example


for (; Test() == true; )

while (Test() ==true)



performs the same;

The do while loops are slightly different in that the loop test is done at the end. The initializer would be before the loop and the counter would be in the loop. I generally don't use these.

gnasher729
Mar 5, 2007, 02:50 PM
What is the difference between those loops?

i did this:

int count;
int noExpenses;
no Expenses = 5; //info that i read it with scan.nextInt
do while (count = 0, count < 5, count++)
and that didn't work.
help?

What does your C book say about it?

What about typing "c source code" into google and checking out the results? You'll find tons and tons of C code that you can look at and study, and that should answer your questions much better.

zimv20
Mar 5, 2007, 03:56 PM
And just as a general tip which I've found very useful once your programs become more complex and you need to parse values etc:

Use prefixes like i, d, etc like: String sCharacters, int iCounter, int iValue, double dNumber, aiIntegerArray

ugh! no! no hungarian notation! besides the obvious flaw of needing to change the variable name every time its type is adjusted (which doesn't really make sense, and almost never gets done), its limited use was negated the instant user-defined types were introduced to whatever language is being used.

otherwise we get non-sense like:

EuroStyleOptionExpiry esoeExpiry = ....


the correct technique, as you mentioned, is to make variable (and function) names as descriptive as practical. no encoding!