• Status: Solved
• Priority: Medium
• Security: Public
• Views: 256

# An alternate solution for this bad loop

I finally got this loop to change but I can't fiqure out another way to stop

while (x = 1)

{

cout << "x is: " << x << '\n';

}

The above code executes a never ending loop displaying x is: 1 out to the console.

This happens because condition x = 1 is reset to 1 (never changes) every time the loop is performed,  even if the  x++  increment is added.

One way to stop this loop is as follows&.

int x = 1;      // initialize interger x to equal 1

while (x < 2) // execute loop while x is less than 2

{
cout << "x is: " << x << '\n';      // console out x is: 1

x++;   // increment x by 1 (this will change the loop condition and terminate the loop

}

I need an alternate way to stop this loop.  Can you help me?
0
cooperk50
• 3
• 2
2 Solutions

Data Warehouse Architect / DBACommented:
Hi cooperk50,

while (x == 1)

That will enter the loop if x is 1, and keep looping until x has any other value.

If you want to execute the loop once, no matter what the value of x, do this:

do
{
cout << "x is:  " << x << \n";
++x;
} while (x == 1);

Good Luck,
Kent
0

Commented:
>> I need an alternate way to stop this loop.  Can you help me?

What do you mean by alternate ?

What do you want the code to do ?
0

Commented:
>>>> I need an alternate way to stop this loop.

You could do:

int x = 0;
while (true)
{
cout << "x = " << x << endl;
cin >> x;
if (x <= 0)
break;
}

Note, the break statement would exit the current loop. In case of a nested loop (loop in loop) you need a break for each of the loops. Another statement for a loop is the 'continue' statement. It will 'go to' end of loop but not breaking it. So, if you want to exit the loop you should break. Or you set the end condition and call continue.

int x = 0;
bool goon = true;
while (goon)
{
cout << " x = " << x << endl;
cout << "Do you want to go on? [Y/N] ";
char c;
cin >> c;
if (c == 'N' || c == 'n')
{
goon = false;
continue;
}
x = rand();  // get any randomized number
}

Shorter is the following (not using continue)

int x = 0;
bool goon = true;
while (goon)
{
cout << " x = " << x << endl;
cout << "Do you want to go on? [Y/N] ";
char c;
cin >> c;
goon = !(c == 'N' || c == 'n');
x = rand();  // get any randomized number
}

However, the difference is that x was set to a new random number if not calling continue or break.

Regards, Alex

0

Commented:

I would recommend to delete the q. without refund.
0

Data Warehouse Architect / DBACommented:
When tied to a similar question in another thread, my answer would seem to be sufficient.  The poster first asked this:

http://www.experts-exchange.com/Programming/Languages/CPP/Q_22999993.html

He then repeated his error in this thread.

Kent
0

Commented:
>>>> When tied to a similar question in another thread, my answer would seem to be sufficient.

You are right, Kent.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.