Solved

An alternate solution for this bad loop

Posted on 2007-12-04
8
244 Views
Last Modified: 2010-04-01
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
Comment
Question by:cooperk50
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 63 total points
ID: 20402417
Hi cooperk50,

You must have missed the answer in the other thread.

  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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20402421
>> 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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 62 total points
ID: 20402625
>>>> 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 25398644
Whatever the wished answer was. Kdo didn't post an 'alternate' answer to that the asker posted.

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

Expert Comment

by:Kent Olsen
ID: 25398977
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 25400199
>>>> When tied to a similar question in another thread, my answer would seem to be sufficient.  

You are right, Kent.

Sorry for misinterpreting your answer here in that thread.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question