-4

I started coding in C++ (VS 2015).

Wrote a little program which using a while statement.

#include <iostream>

using namespace std;

int main() {
    int x = 1;
    int y = 2;
    int z = 0;
    while (x != y || z != y) {
        cout << "x: " << x << "\n" << "z: " << z << "\n";
        x++;
        z++;
    }
    cin.get();
}

If i get this correctly it should work in that way (but since its going to an infinite loop i guess my logic is not as correct as i think).

So in my mind the while statement should work in that way.

While X not equal to y OR z not equal to y.

add +1 to X and +1 to Z.

Do this until the statment became true (or atleast the left side of the expression).

  • Please debug your code, and the answer will become obvious. Insert a breakpoint at `z++` and then you'll be able to see the values of those variables on each iteration. On Stack Overflow, you need to have made efforts to solve the problem *before* you post it. – AStopher Feb 04 '17 at 09:46
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Feb 04 '17 at 09:47
  • This is a `while` loop, not an `until` loop. It loops until its condition becomes *false*. Also, please get rid of `using namespace std;`. – Quentin Feb 04 '17 at 09:51
  • Well, used the thebugger already. Might my question is not proper because of the language barrier. And both of my (debbuging method with cout) and the VS debugger did the same result. breakpointed at the Z++ call. x=2; y=2; and z will be 1 after it. then the function recheck it. if i get this properly (at least the OR operator) "if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated)." X will be equal to Y and it should stop the loop. – Richie G. Edward Feb 04 '17 at 09:52
  • 1
    @RichieG.Edward To stop your loop, the condition needs to be `false` – πάντα ῥεῖ Feb 04 '17 at 09:58
  • @Quentin May I ask whats wrong with `using namespace std;` – Richie G. Edward Feb 04 '17 at 10:17
  • @RichieG.Edward: Glad you're asking! http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Christian Hackl Feb 04 '17 at 11:28
  • By the way, you should also get rid of the `cin.get()` call at the end. – Christian Hackl Feb 04 '17 at 11:28

4 Answers4

1

There's no iteration where both of your conditions become evaluated to false at a certain point, hence the loop doesn't stop.

You can easily investigate that behavior by stepping through your code using a debugger, and watch how the variable values change.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Loop iterations

  1. x=1, y=2, z=0, condition: true
  2. x=2, y=2, z=1, condition: true
  3. x=3, y=2, z=2, condition: true

. . . and so on

The condition never evaluates to false, and leads to infinite loop. Consider changing to and operator to make the loop finite, or maybe change some values.

while (x != y && z != y) {

Output

x: 1
z: 0
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

The 1st expression of while condition is becoming false after first iteration. the 2nd expression is becoming false after a couple of iteration, but after first execution the first expression is becoming true again, and your program runs forever because infinite loop.

0

the loop will keep iterates because whenever the x and y value is increased, it will never become 0, which is equals to the value that is assigned to y. (simple logic)

Change this statement-> while(x!=y||z!=y) into this-> while(x!=y && z!=y) you should get the output: x:1 z:0