-1

This code was given by my faculty for printing pattern. Why does this code go into an infinite loop?

public static void main(String[] args)
{

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter the number>");
    int num = scan.nextInt();

    for (int i = 0; i < num; i++) 
    {
       for (int j =0; j <=i; j ++)
       {
           num = num +1;
           System.out.print(num+" ");
       }
       System.out.println();
    }

}
Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43

4 Answers4

1

The outer loop will only end when i >= num. It increments i by 1 each time, while inside the inner loop you also increment the variable num by 1. num gets incremented multiple times by the inner loop for each time that the outer loop runs. Thus num will always be greater than i and the loop runs forever.

see sharper
  • 11,505
  • 8
  • 46
  • 65
0

The loop is infinite because within the second for the variable num always increases by 1 (num = num +1) and when evaluated in the first for (i

José Eras
  • 90
  • 4
0

This function will enter a infinite loop for any integer input > 0. Let's walk through it to see why.

First let's assume the input is 1. So num = 1. Then we check the loop condition i < num ( i is initialized to 0 and num is 1), the condition is True and we enter the loop.

Then we check the next loop condition j <= i (j is initialized to 0 and i is 0), the condition is true.

now we finally get to the meat of the issue where num is increased by 1. Now num = 2.

Checking the inner loop condition is now false because j was incremented at the end of the loop.

Now checking the outer loop condition is still true as num = 2 and i = 1 (remember i was incremented at the end of the loop). So i < num is true and the loop is entered again

It should now be clear that the outer loop condition will never be false and the loop will continue forever.

-1

Actually, your code does not go into an infinite loop. The other answers already told you why your code doesn't end in the time you've expected it to end (since you increment num in the inner loop, thus rendering i < num less useful than expected), but let me show you that your loop still is able to finish.

I have changed your code a bit:

int num = new Scanner(System.in).nextInt();
int i;
for (i = 0; i < num; i++) {
    for (int j =0; j <=i; j ++) {
        num = num +1;
    }
}
System.out.println("final i: " + i);
System.out.println("final num: " + num);

As you can see I moved the declaration of the i out of the for block in order to be able to access it later and I removed the System.out.println calls inside the loop, because they are time consuming (Java synchronizes the access to the console resource). This program now doesn't even take that long to finish, just one or two seconds (depending on your system performance).

The result for the input "500" looks like this:

final i: 65536
final num: -2147450380

As you can see, the program finished and i has the value 65536 (it has that for any number you enter for the initial value of num) and num is negative. This happens, because int has a value range from -2,147,483,648 to 2,147,483,647 and when you increment a number over the maximum, it "overflows" (if you decrease a number under the minimum value, it "underflows"). The code increments num inside the inner loop, thus more often than i in the outer loop. That means that num reaches the maximum value for integer earlier than i and overflows earlier. In that moment i < num becomes true and the outer loop will be exited.

Tom
  • 16,842
  • 17
  • 45
  • 54