954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

The School of Hard Knocks is within each of us. Its experience, and millions of hours testing various algorithms to find the most efficient.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

But thats time-consuming and probably suitable for people who are not handy-fully

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Depends on how much sleep you need.

You can always stick a finger in a powersocket and hope your brain gets somehow rearranged (improved), but my instinct is telling me that the change of succes will be greater if you just practice , practice and practice ;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Oh well, easier said than done, but I will try

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Cleaner?

for(int sort = 1; sort == 1;) {
        
        sort = 0;
        
        for(int i = 0; i < n - 1; i++) {
        
            if(a[i] < a[i+1]) {
            
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                
                sort = 1;
            }
        }
    }
DigitalPackrat
Light Poster
35 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

I alwayz thought that there is only one way to do for loop statement which is for(int x = 0; x < 10; x++) but whatDigitalPackrat did demonstrate something else and it also proves me wrong..... OK now can you please explain to me what that for loop does or this for loop does??? for(int sort = 1; sort == 1; )

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

a for loop works like this:

for ( init-expression ; cond-expression ; loop-expression )
{ 
    statement 
}

Your example only uses the init-expression amd cond-expression. So if 'sort' isn't changed in the statement-block this is an endless loop (you set 'sort' to 1, then loop while it's 1). This is normally written as: while(1) or for(;;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

So all in all u are saying I cant use for loop the way DigitalPackrat Did, that means what he demonstrated is unacceptable or illigal in C++

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Sure you can. DigitalPackrat sets sort to 0 in the loop, so it won't be endless. I was just trying to give you an insite on for-loops .
I prefer another way to write this loop:

int sorted = 0;
while (!sorted)
{
}

It reads a bit easier.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Ok thanx, I just wanted to know coz really I thought there is no other way to do a for loop except the one I alwayz see

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

In niek_e's example, the init-expression, cond-expression, and loop-expression are all optional as long as you include the separating semicolons. That's why an infinite "forEVER" loop looks like this:

for (;;) {
  ...
}

As long as the loop does what you want, the syntax is very flexible. In Ed's experience, the usual culprits are a very long initialization that makes the loop line too long and a more complicated update:

// So long it makes more sense outside the loop
std::vector<std::pair<std::string, double> >::size_type index = 0;

for (; index != v.size(); ++index) {
  ...
}

// As a part of the loop (too long)
for (std::vector<std::pair<std::string, double> >::size_type index = 0; index != v.size(); ++index) {
  ...
}
// n isn't incremented with every loop
for (n = 0; n < 10 && std::getline(std::cin, input); ) {
  if (input[0] == '*')
    ++n;
}

Cool, huh? :)

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

I think thats clear enough, thanx a lot guys ;)

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Reiterating the point, niek_e made, its possible. All of them are not necessary, in fact none are.
Also, I did not want to use sort variable outside the loop, so I used it inside as, if there are more bubble sorts and I use the sort variable again and don't initialize it, well, that would give me some headache. Won't it?

DigitalPackrat
Light Poster
35 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

Thanx a lot dude!!!!!!

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Yup.

DigitalPackrat
Light Poster
35 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You