Johnny McKenzie
Johnny McKenzie

Reputation: 307

Java won't print inside a for loop

I've been toying around with Java recently and I've encountered a problem:

Whenever I put System.out.println() or System.out.print() inside a for loop, there is no output on the console.

For example:

package experimental;

public class Main {
    public static void main(String args[]) {
        recur();
    }

    public static void recur() {
        for(int x = 0; x == 10; x++) {
            for(int y = 0; y == 10; y++) {
                System.out.print(x + "-" + y + " ");
            }
        }
    }
}

Outputs:

*crickets*

Upvotes: 2

Views: 5048

Answers (7)

mssb
mssb

Reputation: 878

You can't use x == 10. The comparison needs to express this way:

for(int x = 0; x <= 10; x++) {
   for(int y = 0; y <= 10; y++) {
     System.out.print(x + "-" + y + " ");
   }
}

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83587

Since everyone has already answered your question, I would like to add some debugging tips. First of all, I suggest using an IDE such as NetBeans or Eclipse. The debuggers in both of these will allow you to step through your code line by line to see how it executes. In doing so, you would have quickly noticed that your for loops are skipped completely. If you are unable or unwilling to do this. Then the next best thing is to add SOP's (System.out.println()) in your code. Even if it's just a simple message such as "Here" to tell you where you are in the code, this additional output can help you track down errors in logic.

Upvotes: 0

assylias
assylias

Reputation: 328923

Your problem is with the condition in your for loop. It says:

start with x = 0 and loop while x is 10

That is not true so the loop exits immediately.

You need to replace it with:

for (int x = 0; x < 10; x++)

(assuming you want to loop 10 times)

Upvotes: 3

Cody
Cody

Reputation: 2482

Your code never gets to the body of the for loop because the condition x==10 is already false before it starts the loop. I think you should be writing x != 10 instead. Same goes for the inner loop.

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38561

for(int x = 0; x == 10; x++) {
    for(int y = 0; y == 10; y++) {
        System.out.print(x + "-" + y + " ");
    }
}

The x == 10 condition will fail, the loop will break, crickets. Same with the other loop condition.

Upvotes: 2

pb2q
pb2q

Reputation: 59667

Those for loops will never loop.

They'll only loop so long as the termination condition is true. As termination conditions you have: x == 10, y == 10. Since you initialize x, y to 0, these conditions are never true, and the loops are a no-op.

I think you want, e.g.: for (int x = 0; x < 10; x++)

Upvotes: 2

obataku
obataku

Reputation: 29656

The loop condition isn't true to even begin with... x == 10 is not true when x = 0. Had it been, you would've also noticed the second loop condition is also not true to begin with; similarly , y == 10 is not true as y = 0. You probably confused the condition with one for stopping.

for(int x = 0; x == 10; x++) {
     for(int y = 0; y == 10; y++) {

Change to the following if you want x, y to vary from [0, 10):

for(int x = 0; x < 10; x++) {
    for(int y = 0; y < 10; y++) {

... or the following for [0, 10]:

for(int x = 0; x <= 10; x++) {
    for(int y = 0; y <= 10; y++) {

Upvotes: 9

Related Questions