4 Replies - 296 Views - Last Post: 08 August 2009 - 02:07 AM Rate Topic: -----

#1 blaze212  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 06-August 09

while loop

Post icon  Posted 08 August 2009 - 01:13 AM

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	int i = 9;
	bool done = false;
	while (!done) {
		  cout << i << endl;
		  i++;
		  if ( i <= 10);
		  done = true;
		  }
		  cout << "All Finshed!" << endl;
	system("PAUSE");
	return EXIT_SUCCESS;
}




can someone explain this to me. i got it to run.

Is This A Good Question/Topic? 0
  • +

Replies To: while loop

#2 shelbzilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-August 09

Re: while loop

Posted 08 August 2009 - 01:17 AM

You wrote it, yet you don't understand it?
What do you not understand about it?
Was This Post Helpful? 0
  • +
  • -

#3 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: while loop

Posted 08 August 2009 - 01:30 AM

Well you have a pretty short loop, while !done (done == false)... run the loop

but you have done = true inside the loop, so it only runs the loop once

#include <iostream>
using namespace std;

int main()
{
	int i = 0;
	bool done = false;

	while(!done)
	{
		if (i == 5)
		{
			done = true;
			cout << "Done == true\n"; break;
		}
		cout << "i = " << i << endl;
		i++;
	}

	return 0;
}



maybe this helps you understand better?
Was This Post Helpful? 0
  • +
  • -

#4 blaze212  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 06-August 09

Re: while loop

Posted 08 August 2009 - 01:35 AM

View Postshelbzilla, on 8 Aug, 2009 - 12:17 AM, said:

You wrote it, yet you don't understand it?
What do you not understand about it?


the while (!done)

View Poststev3, on 8 Aug, 2009 - 12:30 AM, said:

Well you have a pretty short loop, while !done (done == false)... run the loop

but you have done = true inside the loop, so it only runs the loop once

#include <iostream>
using namespace std;

int main()
{
	int i = 0;
	bool done = false;

	while(!done)
	{
		if (i == 5)
		{
			done = true;
			cout << "Done == true\n"; break;
		}
		cout << "i = " << i << endl;
		i++;
	}

	return 0;
}



maybe this helps you understand better?


so if the done was fals it would keep on running, true
Was This Post Helpful? 0
  • +
  • -

#5 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: while loop

Posted 08 August 2009 - 02:07 AM

View Postblaze212, on 8 Aug, 2009 - 12:35 AM, said:

so if the done was fals it would keep on running, true


while(!done) //while(done == false)
{
//whatever is here will 'loop' untill done == true
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1