The System.out.println
indicates this is Java.
In Java, the int
type is a 32-bit signed integer type, with values ranging from -2,147,483,648 to 2,147,483,647. Decreasing a variable that is already at the minimum value wraps it around to the maximum.
This means that the inner loop will run for 2,147,483,648 iterations before the variable j
wraps around, becomes larger than 42
, and terminates the inner loop. The inner loop is thus not infinite, as indicated in the question, but instead finite and constant.
Since the inner loop always execute this many steps, it is constant, and thus the big-o notation is wholely dictated by the outer loop.
So the big-o notation for this piece of code is O(n).
If the compiler/runtime can be configured to crash when you try to underflow an int
, instead of wrapping it, the program will always crash, and it will manage to just run 1 iteration of the outer loop before it crashes at the end of the inner loop. It would thus be O(1). I do not know enough Java, however, so I don't know if this is possible.
If instead of Java, this is supposed by be in context of another theoretical programming language, or seen upon as a pure algorithmic description, in which int
does not actually have a lower bound, then the program does not terminate and it does in fact not have a big-o notation at all.
More about this last paragraph here: O-notation, O(∞) = O(1)?.