| View previous topic :: View next topic |
| Author |
Message |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 3:45 pm Post subject: |
|
|
| Quote: | | Why a do-while? I prefer pre-test loops. |
Sometimes you have to go through a loop once to get something to compare. Sometimes, you'd have to copy and paste your code before the while loop if you didn't want to use a do-while.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 5:51 pm Post subject: |
|
|
| HalfPrime wrote: | | Quote: | | Why a do-while? I prefer pre-test loops. |
Sometimes you have to go through a loop once to get something to compare. Sometimes, you'd have to copy and paste your code before the while loop if you didn't want to use a do-while. |
I understand that, but I don't see the need for it this time.
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 8:17 pm Post subject: |
|
|
In this example, the only difference that it would make is that if you used a while instead of a do-while, you'd have to set your input char to 'Y' initially.
Except, in your code, you use a while loop, but it basically is a do-while because
| Code: | | ((input = Console.ReadLine()) != "stop") |
takes in the input before being compared.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 9:07 pm Post subject: |
|
|
| HalfPrime wrote: | In this example, the only difference that it would make is that if you used a while instead of a do-while, you'd have to set your input char to 'Y' initially.
Except, in your code, you use a while loop, but it basically is a do-while because
| Code: | | ((input = Console.ReadLine()) != "stop") |
takes in the input before being compared. |
No...
A do-while would loop through the entire code before testing.
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 9:11 pm Post subject: |
|
|
What I'm saying is, you execute code that gets repeated everytime before you actually enter the loop.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 9:16 pm Post subject: |
|
|
| HalfPrime wrote: | | What I'm saying is, you execute code that gets repeated everytime before you actually enter the loop. |
In my while loop, the Console.ReadLine() is the only thing that is executed before the loop. It could be done with a for loop too, though it'd be obnoxious and pointless:
| Code: |
for (input = Console.ReadLine(); input != "stop"; input = Console.ReadLine())
{
//...
} |
In a do-while loop, the entire part of the loop is executed before the first test.
Look at this simulation:
| Code: |
blah blah...
Introduction...
Enter the first number, "stop" to exit.
stop
Invalid number
...
|
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 9:25 pm Post subject: |
|
|
eh, semantics.
Refferring to the original code, a do-while would've been better than using a while loop which was what the
| Quote: | | Ew...goto. Use a do while loop. |
was referring to.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 9:30 pm Post subject: |
|
|
| HalfPrime wrote: | eh, semantics.
Refferring to the original code, a do-while would've been better than using a while loop which was what the
| Quote: | | Ew...goto. Use a do while loop. |
was referring to. |
I just stated why a while would be better than a do-while. -_-;
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 9:38 pm Post subject: |
|
|
| Code: | | I just stated why a while would be better than a do-while. -_-; |
where?
the original code didn't put the input into a variable during the compare. In that example, a do-while would've been better.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 9:41 pm Post subject: |
|
|
| HalfPrime wrote: | | Code: | | I just stated why a while would be better than a do-while. -_-; |
where?
the original code didn't put the input into a variable during the compare. In that example, a do-while would've been better. |
| I wrote: | In a do-while loop, the entire part of the loop is executed before the first test.
Look at this simulation:
| Code: |
blah blah...
Introduction...
Enter the first number, "stop" to exit.
stop
Invalid number
...
|
|
While > do-while > original
in this situation
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 9:46 pm Post subject: |
|
|
| Code: |
do{...shit...}while(!stop); |
vs
| Code: |
stop = !stop
while(!stop)
{...shit...} |
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 9:57 pm Post subject: |
|
|
| HalfPrime wrote: | | Code: |
do{...shit...}while(!stop); |
vs
| Code: |
stop = !stop
while(!stop)
{...shit...} |
|
What's with the assignment at first?
I've cleared up the simulation if you didn't get it (I'm assuming you didn't, because you are acting like an idiot when I flatly put it out for you):
| Code: |
COMPUTER: <Introduction>
COMPUTER: Please enter first number. Type "stop" to exit.
USER : stop
COMPUTER: Invalid input.
COMPUTER: Please enter next number. Type "stop" to exit.
USER : stop
COMPUTER: Your average is ... Press enter to exit.
|
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 10:04 pm Post subject: |
|
|
A while loop will check the condition before it begins the loop. If you don't assign it to true first, or don't assign it at all, it'll fuck up.
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 10:08 pm Post subject: |
|
|
| HalfPrime wrote: | | A while loop will check the condition before it begins the loop. If you don't assign it to true first, or don't assign it at all, it'll fuck up. |
Did you not read my code?
| Code: |
//using System.Collections.Generic;
//...
List<int> toAverage = new List<int>();
string input = String.Empty; int buffer; int currentAverage;
while ((input = Console.ReadLine()) != "stop")
{
if (!int.TryParse(input, out buffer))
{
Console.WriteLine("Invalid response.");
Console.WriteLine("Enter next number, or \"stop\" to exit.");
continue;
}
toAverage.Add(buffer);
currentAverage = CalculateAverage(toAverage);
Console.WriteLine("Current average: " + currentAverage.ToString());
Console.WriteLine("Enter next number, or \"stop\" to exit.");
}
Console.ReadLine();
}
private int CalculateAverage(List<int> values)
{
int sum = 0;
for (int i = 0; i < values.Count; i++) sum += values[i];
return (sum / values.Count);
} |
Notice the
| Code: |
while ((input = Console.ReadLine()) != "stop")
|
First, it sets input to Console.ReadLine().
Then, it checks that to see if it isn't stop. If it isn't, then it goes on to do the code, if it is, it skips past the loop and doesn't run the code once.
_________________
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 10:10 pm Post subject: |
|
|
| Quote: | | the original code didn't put the input into a variable during the compare. In that example, a do-while would've been better. |
Did you stop reading my posts?
_________________
|
|
| Back to top |
|
 |
|