Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] Started C# and trying to make unending Average Calc
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
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

PostPosted: Wed Jun 18, 2008 3:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 5:51 pm    Post subject: Reply with quote

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 8:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 9:07 pm    Post subject: Reply with quote

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 9:11 pm    Post subject: Reply with quote

What I'm saying is, you execute code that gets repeated everytime before you actually enter the loop.
_________________
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 9:16 pm    Post subject: Reply with quote

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

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 9:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 9:30 pm    Post subject: Reply with quote

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

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 9:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 9:41 pm    Post subject: Reply with quote

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

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 9:46 pm    Post subject: Reply with quote

Code:

do{...shit...}while(!stop);

vs
Code:

stop = !stop
while(!stop)
{...shit...}

_________________
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 9:57 pm    Post subject: Reply with quote

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 10:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 18, 2008 10:08 pm    Post subject: Reply with quote

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Wed Jun 18, 2008 10:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites