Hi everyone,

I'm doing a little experiment on Java loops.. I came across these infinite loops and they never wanted to stop .... Could anyone explain these loops to me, please. Thanks in advance.

for (int a=0;a <= 5; a++){
			
		int c;
		int b= 6;

		for( c=0; c <= b; b++){
			println("b = " + b);
				if(b ==5){
				      println("loop terminated " + c++);
				}
		}
		
	}

The idea was to get something to look like this
0
1
2
3
4
5
Loop terminated 1
0
1
2
3
4
5
Loop terminated 2 and so on till it gets to loop number 5 then breaks...

Dani AI

Generated

Quick diagnosis and fix (summary of what went wrong and how to avoid it)

The infinite loop happened because the inner loop’s update expression changed the wrong variable. In the original snippet the loop condition depended on one variable while the update clause incremented another, so the condition never moved toward false and the loop ran forever — which also explains why b eventually showed very large values (it was being incremented repeatedly). Initializing b to 6 also makes the test b == 5 impossible, so that branch never runs. pointed out the wrong-variable update; and showed the correct intent (print 0..5 then a single “Loop terminated N” after each inner run).

Concrete rules to follow

  • For for(init; condition; update) make sure update changes the loop variable used in condition.
  • Keep inner-loop counters declared inside the inner loop so they can’t be accidentally reused.
  • If printing a loop number that counts iterations of the outer loop, use the outer counter (or outer+1 if starting from 0).
  • Note: c++ in a print expression returns the old value and then increments; use ++c or c+1 if the incremented value should be printed.

A simple alternative (while-based) that avoids the common mistake:

int outer = 1;
while (outer <= 5) {
    int inner = 0;
    while (inner <= 5) {
        System.out.println(inner);
        inner++;
    }
    System.out.println("Loop terminated " + outer);
    outer++;
}

Quick debugging tips

  • Add a temporary safety counter to detect runaway loops while debugging.
  • Use an IDE breakpoint or small prints to check which variable actually changes each iteration.
  • Prefer consistent indexing (0..n-1 with < or 1..n with <=) to reduce off-by-one confusion.

Recommended Answers

All 17 Replies

for( c=0; c <= b; b++){
println("b = " + b);

Since nothing happens to c it will never be equal to b which is why it loops forever

if(b ==5){
println("loop terminated " + c++);
}

Also since b = 6 it will never be equal to 5

But why does b start at 30000 and something? Shouldn't b increment by 1?

for (int a=0;a <= 5; a++){
			
			for( int b= 0; b <= 5; b++){
				println("b = " + b);
				for(int c=1; c<=b; c++){
					if(b ==5)
						println("loop terminated " + c);
				}
			}
		
		}

This code doesn't do what I want to do..


Cheers,

But why does b start at 30000 and something? Shouldn't b increment by 1?

What I meant to say was your using b as an increment while your using c in your initialization and your loop condition in your previous post

for(int c=1; c<=b; c++){
if(b ==5)
println("loop terminated " + c);
}

Why use c in a loop condition.. why not use it as a counter?

*"I'm not gonna explicitly say the answer to your problem"

Why do you have the inner loop with c?
You don't do anything with b, and all you will get Loop terminated 0-5 printed 6 times.

What exactly do you want?

How would I get the result I want? Would you suggest something?

Why use c in a loop condition.. why not use it as a counter?

Your not thinking from what I just said, the answer is already in front of you

I want my program to print Loop terminated ** when the condition(b == 5) for the outer loop evaluates to true.. For example, after the first iteration, it will print "Loop terminated 1" and for the second, it will be "Loop terminated 2" and so on.

To avoid having five lines that tell me Loop terminated 1,2,3,4,5 after the first iteration and second and third...

cheers,

@ zeroliken .. I ain't sure how to use a counter in Java ;p

A counter is the variable in the for loop (a in the first, b in the second, c in the third).

I would suggest the following, made from your code:

for (int a=0;a <= 5; a++){
	for( int b= 0; b <= 5; b++){
		println("b = " + b);
		if(b ==5)
			println("loop terminated " + a);
	}
		
}

This is what I had before looking at the code you posted ... Thumb's up, buddy..

for (int a=0;a <= 5; a++){
			
			for( int b= 0; b <= 5; b++){
				println("b = " + b);
				if(b ==5)
					println("loop terminated " + b);
			}
		
		}

Great, one thing, this will always print 'loop terminated 5' at the end of each loop (you just specified that b should be 5).
If you use a, the 'loop terminated x' will increment from 0 to 5.

I figured it out by now..

I figured it out by now..

Good for you :)

...
Thank you for the explanation

Here is the result that you need.

for(i = 1; i <= 5; i++)
		{	
			for(j = 1; j <= 5; j++)
			{
				System.out.println(j);
				if(j == 5)
				{
					System.out.println("Loop terminated at " + i);
				}
			}
		}
1
2
3
4
5
Loop terminated at 1
1
2
3
4
5
Loop terminated at 2
1
2
3
4
5
Loop terminated at 3
1
2
3
4
5
Loop terminated at 4
1
2
3
4
5
Loop terminated at 5

That's exactly what I said, but thanks for the addition...

People seem not to pay a lot of attention to previous posts. ROFL!!

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.