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...

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

Edited 5 Years Ago by zeroliken: n/a

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"

Edited 5 Years Ago by zeroliken: n/a

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?

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,

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..

Good for you :)

@hiddepolen...
Thank you for the explanation

Edited 5 Years Ago by zeroliken: n/a

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
This question has already been answered. Start a new discussion instead.