简体   繁体   中英

Why is this for loop giving this output?

Why is the output of the following code: 13 15 17

I think it should be: 15 17 19

这是我的解释

Here's the code:

package com.example.barker;
class dog {
}

public class Bark {
    public static void main(String[] args) {
        Bark o = new Bark();
        o.go();
    }

    void go(){
        int y =7;
        for(int x = 1; x<8; x++) {
            y++;
            if(x>4) {
                System.out.print(++y + " ");
            }
        }
    }
}

I think the Anwer is right.

I will Explain the working of the code to get understand.

First

y=7

x=0

and after first iteration

y=8 (y++;) and x=1 (int x = 1;) (not printing because x not greater then 4)

after Second Iteration

y=9 (y++;) and x=2 (x++;) (not printing because x not greater then 4)

after Third Iteration

y=10 (y++;) and x=3 (x++;) (not printing because x not greater then 4)

after Fourth Iteration

y=11 (y++;) and x=4 (x++;) (not printing because x not greater then 4)

after Fifth Iteration

y=12 (y++;) and x=5 (x++;)

Now x is greater than 4 and going to System.out.print(++y + " ");

Here you are writing ++y ,means pre-increment

ie, increment y and printing

ie, y=13 and x=6 printing(13)

After next Iteration

y=14(y++;) and before printing the value of y doing ++y ie, y=15 (++y;) printing(15)

After next Iteration

y=16(y++;) and before printing the value of y doing ++y ie, y=17 (++y;) printing(17)

So the Output is 13 15 17

Thanks and happy coding.

Change the conditions like below, to get your desired result.

 for(int x = 1; x<10; x++) { y++; if(x>6) { System.out.print(++y + " "); } }

Let's first understand the operations happening within the for loop:

  • y++ will perform the +1 operation after the line of code finishes executing

  • ++y will first perform the +1 operation and then execute the rest of the line of code

Now, let's look at the for loop:

for(int x = 1; x<8; x++) {
        y++;
        if(x>4) {
            System.out.print(++y + " ");
        }
    }

For each iteration, we will look at the values for x and y:

x = 1, y = 7 (start of first iteration)

x = 1, y = 8 (end of iteration)

x = 2, y = 8 (start of first iteration)

x = 2, y = 9 (end of iteration)

x = 3, y = 9 (start of first iteration)

x = 3, y = 10 (end of iteration)

x = 4, y = 10 (start of first iteration)

x = 4, y = 11 (end of iteration)

x = 5, y = 11 (start of first iteration)

x = 5, y = 12 (y++ line)

x = 5, y = 13 (x > 4, and because of that, ++y executes)


The logic has become pretty apparent, but you can continue to debug this yourself.

Hope this helps. Here, goes the explanation :

y=7 //At Start

When you enter in the loop:

x = 1 and y = 8
x = 2 and y = 9
x= 3 and y = 10
x = 4 and y = 11
x = 5 // which makes the if condition true

y will become 12 and when it goes into if System.out.print(++y + " ") It will first increment then prints. So, it will print y = 13 .

Similarly, when x = 6 y will be 14 and in if it becomes 15 and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM