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 


[C++]looping
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
samuri25404
Grandmaster Cheater
Reputation: 7

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

PostPosted: Mon Jan 28, 2008 10:23 pm    Post subject: Reply with quote

Ok, I'll go ahead and exaplain the for loop.

First of all, this is the format:

Code:

for (1; 2; 3)
{

}


1: An assignment that occurs once, right at the beginning of the loop.

2: A loop condition, loop until this happens. Its like this:

Code:

while(2)
{}


3: A statement to occur once after each loop through.

~~

Let's go through an example:

Code:

for (int i = 0; i < 10; i++)
{
     cout << "Hi!\n";
}


This prints out "Hi!" 10 times to the screen.

~~~

Let's analyze it:

1:

int i = 0;

This will happen once, right at the beginning. Note that it is an initialization. Remember that it doesn't have to be one, but it usually is.

2:

i < 10

Loop until this happens, it would be like

Code:

while (i < 10)
{}


3:

i++

This is an assignment statement to go on after each loop through.

~~

Note that if you don't want to do something, just put the semicolon there. An infinite loop that does nothing would be:

Code:

for (;;) {}


~~

Let's go back and analyze that previous loop:

[code]
for (; MyLoop(); Sleep(10))
{}
[code]

1: Nothing here. We don't need to do anything here, so just leave it blank.

2:

MyLoop()

Loop until MyLoop() returns true.

3:

Sleep(10)

An assignment statement. It just makes the computer wait for 10 ms, so it has a little bit of time to catch its breath, and you usually don't notice it most of the time; the computer's incredibly fast.

~~

That's not so hard, right?

Wink

_________________
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
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Mon Jan 28, 2008 10:30 pm    Post subject: Reply with quote

i understand now, but what benefit would it be to use that instead of while? just never mind me, ill accept the knowledge and apply it later. in this situation, ill use while.
_________________

georgezilka wrote:
im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ?
Back to top
View user's profile Send private message Send e-mail AIM Address
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Mon Jan 28, 2008 10:42 pm    Post subject: Reply with quote

guernica wrote:
i understand now, but what benefit would it be to use that instead of while? just never mind me, ill accept the knowledge and apply it later. in this situation, ill use while.


For and While are basically the same, except their starting method can be setup different. A for can be endless just like a while, but a for also gets initialized with 3 params that can set up a loop for a certain amount of times.

Code:
for( int x=0; x<10; x++ )


Would create a loop that will go 10 times. (Starting with 0, ending with 9)

You can do the same idea with a while by doing something like:

Code:
   int x = 0;
   while( x != 10 )
   {
      std::cout << x << std::endl;
      x++;
   }



A while is easy to toss in for a conditional loop. Such as you want to loop until something is true. You can do this with a while easily like this:

Define bWantsExit at the top of the project under the includes as a BOOL.
Code:
   while( !bWantsExit )
   {
      // Do Code Here
   }


Then outside methods can set bWantsExit to true and have the above loop finish up.

You can do the same thing with a for just with more work:

Define bWantsExit at the top of the project under the includes as a BOOL.

Code:
   for( ; ; )
   {
      if( bWantsExit )
         break;
   }


Each have their "optimization" of less code and ease of use. Using for(; Wink is seen as poor practice though when doing a conditional loop as you can do it more securely with something like the example I showed above with the while.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
samuri25404
Grandmaster Cheater
Reputation: 7

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

PostPosted: Tue Jan 29, 2008 3:19 pm    Post subject: Reply with quote

Wiccaan wrote:


Code:
   for( ; ; )
   {
      if( bWantsExit )
         break;
   }



What happened to your advertising "Sleep(10)" everywhere?

Code:

for (;;)
{
   if (bWantsExit) break;
   Sleep(10);
}


:p

Btw, grats on 1500 posts.

_________________
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
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Wed Jan 30, 2008 8:36 am    Post subject: Reply with quote

I was following the while( ) I wrote just about it with the // Other Code Here, just didn't write it in since it was just an example of how to exit the loop while it's going. But yea, sleeps are basically a must in loops like that Razz
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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
Page 2 of 2

 
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