void main()
{
  int i,j;
  /*input values of i and j*/
  while(i*j<0&&++i!=7&&j++!=9)
  {
    do something;
  }

Dani AI

Generated

Short answer: No — assuming i and j are properly initialized and standard C semantics apply, the loop will not run forever. It either never enters (product non‑negative) or stops after a finite number of iterations because each pass increments both variables and the negative one moves toward zero.

Why: the condition first checks whether i * j < 0 (opposite signs). With that true, ++i is evaluated (pre‑increment) and then j++ (post‑increment) only if previous checks passed. Because the negative operand is incremented on every iteration, its absolute value strictly decreases and will reach zero in a finite number of steps, making i*j<0 false. The logical AND operator guarantees left‑to‑right evaluation and short‑circuiting; the increment operators behave as pre/post as expected — see Logical AND and Increment and decrement. Uninitialized locals or signed overflow can produce undefined behavior, so those are the only realistic ways the program could act unpredictably; see .

A clearer, safer style is to initialize variables and separate side‑effects from the compound condition:

int main(void) {
    int i = -3, j = 4; /* example initialization */
    while (i * j < 0) { /* opposite signs */
        if (++i == 7) break;
        if (j++ == 9) break;
        /* do something */
    }
    return 0;
}

Notes: prefer int main(void), always initialize locals, and enable compiler warnings and sanitizers (e.g., -Wall, -Wuninitialized, -fsanitize=undefined) when strange behavior is suspected. This aligns with points made by (initialization) and (main/overflow); the sign‑based reasoning complements comments from and .

Recommended Answers

All 8 Replies

> is this loop tends to infinity for any value of i and j
Like any pair of positive numbers?

1. int main() 2. Nope. For any initial i and j the loop runs until i or j comes to zero (for positive initial number - after integer overflow). Of course on 64-bit platform it's a long way to...

you need to initialize the values of i and j to something.

dhingra initialize i and j in the previous comment statement ;)

dhingra initialize i and j in the previous comment statement ;)

Oh, so the code he posted is not the same as the code that is giving the problem(s).

while(i*j<0&&++i!=7&&j++!=9)

That loop it is an absurdity.
In the best case if the product of i * j is a negative number the pre-increment of i would always be something other than seven and the post-increment of j something other than nine.

At the other hand. If the product it is a positive number then that's it, the loop doesn't need to check anything else.

May be it was a test on logics and theory of binary arithmetics?..
Sometimes I think that 9 of 10 questions on the C and C++ forums are 100% absurdity...

A la "Urgent help needed! I'm trying to assign void main to char then convert it to a pointer to 2.5D vector but my program do nothing and my compiler said that I have missed semicolon. Why?".

commented: Give me code, pleazzzzzzzzzzzzzz! ;) +10

Nope this loop doesn't go to infinity...
For positive and negative value of both i and j, it will not even enter the loop...
if either i or j is negative, it will enter the loop once... thats it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.