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

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

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?

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?

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

``````for (int a=0;a <= 5; a++){
for( int b= 0; b <= 5; b++){
println("b = …``````

## 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?

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?

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?

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

``````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 :)

@hiddepolen...
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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.