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...