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.
 
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.
 
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)
 
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;
}
 
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;



}
 
Back
Top Bottom