do while loop

Soldato
Joined
1 Dec 2004
Posts
22,552
Location
S.Wales
Code:
do { pass = 1; pass<=arraysize-1; pass++;

	}


    while (swap == false);

I need abit of help with some c++, how would i say do the following while swap == false?

it needs to sort through an array of data whilst swap is equal to false, once everything is swapped bool swap will change to true and the programme will exit..

I have the rest of the code i just not sure what to put in this first do while loop.
 
If you're just looking to sort a load of data (i'm guessing here, from looking at what you've posted), why not just use the standard sort algorithms?
 
Dfhaii said:
The loops uses a boolean condition so you don't need while (swap == false), you can use while (!swap) instead.


Thats the way iv been shown how to do it im afaid, here is my code


Code:
#include <iostream.h>
#include <cstring.h>

void main()

{

//Variable declarations
int item[10];
int pass;
int comparison;
int temp;
int arraysize;
int i;
bool swap;

//input

item[0] = 2;
item[1] = 1;
item[2] = 3;
item[3] = 4;
item[4] = 7;
item[5] = 5;
item[6] = 9;
item[7] = 6;
item[8] = 8;
item[9] = 10;

arraysize = 10;

//sorting

pass = 1;
do {

	for (comparison=1; comparison <= arraysize-1; comparison++)
      {

        	if (item[comparison-1] > item[comparison])
         {
         swap = true;


         temp = item[comparison-1];
         item[comparison-1] = item[comparison];
         item[comparison] = temp;

			}



		}

    pass++;
    while ((swap == false) && (pass<=arraysize-1));






	}
//output



cout<<""<<endl;
cout<<""<<endl;
cout<<"State of array after sorting: "<<endl;

for (i = 0;  i<10; i++)
{
cout<<item[i]<<endl;


}






}

Im trying to find a place for that pass++, am i correct in saying that it goes somewhere before the while loop, because i only want to increment further if swap == true, if there are no swaps (swap== false) then there is no-need to increment any further and the loop will finish.

I want to try to keep with the structure im working with, any one telling me i shouldnt do this, replace this with this as it will only confuse me more.
 
if and while just evaluate the contents of the parentheses and see if they're true, so it's usually just as readable and much easier to negate the variable with !:

Code:
// in C++...
if(foo == false)
if(foo == 0)
if(!foo)
// ...are equivalent
 
robmiller said:
if and while just evaluate the contents of the parentheses and see if they're true, so it's usually just as readable and much easier to negate the variable with !:

Code:
// in C++...
if(foo == false)
if(foo == 0)
if(!foo)
// ...are equivalent

I dont get what you mean?

You mean replacing (statement == false) with (!statement)
 
Yes he is just saying they are the same. Just use the method you have been taught (foo == false) it its more natural/readable for you. The other way (!foo) is just a bit shorter. Dont use (foo==0) . . its a bit confusing.
 
Essentially, to get the code working you're going to need to change:
Code:
}

    pass++;
    while ((swap == false) && (pass<=arraysize-1));

}
//output
to
Code:
		}
        pass++;
   } while ( (swap == false) && (pass<=arraysize-1) );
However, instead of "while ((swap == false) && (pass<=arraysize-1));", you could also write
"while ( !swap && (pass<=arraysize-1) );" - both do exactly the same.

Btw, what are you using to compile this? as I had to change the top of the file to:
Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
to get it to compile.
 
Swanster said:
Essentially, to get the code working you're going to need to change:
Code:
}

    pass++;
    while ((swap == false) && (pass<=arraysize-1));

}
//output
to
Code:
		}
        pass++;
   } while ( (swap == false) && (pass<=arraysize-1) );
However, instead of "while ((swap == false) && (pass<=arraysize-1));", you could also write
"while ( !swap && (pass<=arraysize-1) );" - both do exactly the same.

Btw, what are you using to compile this? as I had to change the top of the file to:
Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
to get it to compile.

Thanks a lot mate, that makes more sence to me..Ill probably do it one way then add the alternative as a comment


Well we use a version of Borland C++ 5.02 i think, but i use bloodshed dev C++ at home
 
Last edited:
Why aint in compiling in bloodshed dev C++??

222.JPG


its complaning about the bits of code like cout<<""<<endl; which worked fine on borlands version of C++..

I have created a new project and put the code in

here is my code

Code:
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <istream>
using namespace std;

int main(int argc, char *argv[])
{

    //Variable declarations
int item[10];
int pass;
int comparison;
int temp;
int arraysize;
int i;
bool swap;

//input

item[0] = 2;
item[1] = 1;
item[2] = 16;
item[3] = 4;
item[4] = 7;
item[5] = 5;
item[6] = 9;
item[7] = 6;
item[8] = 8;
item[9] = 15;

arraysize = 10;

//sorting

pass = 1;
do {

	for (comparison=1; comparison <= arraysize-1; comparison++)
      {

        	if (item[comparison-1] > item[comparison])
         {
         swap = true;


         temp = item[comparison-1];
         item[comparison-1] = item[comparison];
         item[comparison] = temp;

			}



		}

    pass++;
   } while ( (swap == false) && (pass<=arraysize-1) );





	}
//output





cout<<" "<<endl;
cout<<"State of array after sorting: "<<endl;
for (i = 0;  i<10; i++)
{
cout<<item[i];<<endl;


}






}

    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
The error is not the cout line, but above it.
You need to remove the '{' just above '//output', and the one just above 'system("PAUSE");'. You also need to take the ';' out of 'cout<<item;<<endl'.
 
Iv noticed tho it does not sort my array items correctly

444.JPG


Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{

//Variable declarations
int item[10];
int pass;
int comparison;
int temp;
int arraysize;
int i;
bool swap;

//input

item[0] = 2;
item[1] = 1;
item[2] = 16;
item[3] = 4;
item[4] = 7;
item[5] = 5;
item[6] = 9;
item[7] = 6;
item[8] = 8;
item[9] = 15;

arraysize = 10;

//sorting

pass = 1;
do {

	for (comparison=1; comparison <= arraysize-1; comparison++)
      {

        	if (item[comparison-1] > item[comparison])
         {
         swap = true;


         temp = item[comparison-1];
         item[comparison-1] = item[comparison];
         item[comparison] = temp;

			}



		}

    pass++;
   } while ( (swap == false) && (pass<=arraysize-1) );





	
//output



cout<<""<<endl;
cout<<"State of array after sorting: "<<endl;

for (i = 0;  i<10; i++)
{
cout<<item[i]<<endl;






}
 system("PAUSE");
    return EXIT_SUCCESS;



}
 
Code:
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{

//Variable declarations
int item[10];

//input

item[0] = 2;
item[1] = 1;
item[2] = 16;
item[3] = 4;
item[4] = 7;
item[5] = 5;
item[6] = 9;
item[7] = 6;
item[8] = 8;
item[9] = 15;

int arraysize = 10;

//sorting

std::sort(item, item+arraysize) ;
	
//output
cout<<""<<endl;
cout<<"State of array after sorting: "<<endl;

for (int i = 0;  i<arraysize; i++)
{
cout<<item[i]<<endl;
}

 system("PAUSE");
    return EXIT_SUCCESS;
}
 
Last edited:
[Sniper][Wolf] said:
Iv noticed tho it does not sort my array items correctly

I take it you're trying to implement bubble sort yourself. In that case your loop logic should be "keep repeating doing a pass while a swapped in the last pass", not while "not swapped" as you had it. When you've done a pass without swapping, then you're done (since all pairs of elements are in the correct order.)

Fixed code:

Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	//Variable declarations
	int item[10];
	int pass;
	int comparison;
	int temp;
	int arraysize;
	int i;
	bool swap;

	//input
	item[0] = 2;
	item[1] = 1;
	item[2] = 16;
	item[3] = 4;
	item[4] = 7;
	item[5] = 5;
	item[6] = 9;
	item[7] = 6;
	item[8] = 8;
	item[9] = 15;

	arraysize = 10;

	//sorting
	pass = 1;

	do {
		// start the pass remembering that we've not swapped yet:
		swap = false;

		// loop over the array, if we swap, set swapped to true
		for (comparison=1; comparison <= arraysize-1; comparison++)
		{
			if (item[comparison-1] > item[comparison])
			{
				swap = true;
				temp = item[comparison-1];
				item[comparison-1] = item[comparison];
				item[comparison] = temp;
			}
		}
		// incremement the pass count at the end of the pass
		pass++;
		// if we've swapped, then we must do another pass (since there might
		// still be unsorted entries.  (If swap == false we're done.)
	} while ( (swap == true) && (pass<=arraysize-1) );

	//output
	cout<<""<<endl;
	cout<<"State of array after sorting: "<<endl;

	for (i = 0;  i<10; i++)
	{
		cout<<item[i]<<endl;
	}

	system("PAUSE");
	return EXIT_SUCCESS;
}

HTH
 
Last edited:
Back
Top Bottom