0

I can't seem to understand why these two codes produce different outputs.

What is the difference of y<=x; and y<=5;. As you can see the x is 5 too, I don't understand why I get different outputs.

for (int x = 0; x < 5; x++) {
    for (int y = 1; y <=x ; y++) {
        System.out.print("x");
    }
                
    for (int g = 4; g >= x; g--) {
        System.out.print("*");
    }                       
    System.out.println();
}

Output:

*****

x****

xx***

xxx**

xxxx*

Code:

for (int x = 0; x < 5; x++) {
    for (int y = 1; y <= 5; y++) {
        System.out.print("x");
    }
                
    for (int g = 4; g >= x; g--) {
        System.out.print("*");
    }                       
    System.out.println();
}

Output:

xxxxx*****

xxxxx****

xxxxx***

xxxxx**

xxxxx*
2
  • 3
    x is a fixed value in the first, and a changing value in the second. Commented Jun 18, 2015 at 11:19
  • x changes value for each time you run the y loop in that first case Not the same as the second case Commented Jun 18, 2015 at 11:23

10 Answers 10

9

Basically the main difference is this line:

for(int y=1; y<=x; y++)

resp.

for(int y=1; y<=5; y++)

The number of times the loop is executed is different. Namely in the first case it is variable (so the number of 'x' increases), in the second case it is fixed (5 'x' printed each time).

Sign up to request clarification or add additional context in comments.

Comments

5

xstarts at 0 so the first iteration has the condition y<=0, the second will have y<=1 and so on .. till y<=5

While the second one will have y<=5in every iteration, thats why you get xxxxx in every line.

Comments

4

In the first code you print x times the "x" String in each row.

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

BTW, it prints the following (which is different than what you claim in the question):

*****
x****
xx***
xxx**
xxxx*

In the second code you print 5 times the "x" String in each row.

for(int y=1; y<=5; y++) {
    System.out.print("x");
}

As you can see the x is = 5 too

No, x iterates from 0 to 4, so in each iteration of the outer for loop, it has a different value.

Comments

3

In your first code your for(int y=1; y<=x; y++) for iterations of outer for loop is -
for(int y=1;y<=0;++y) (for first iteration of outer loop)
for(int y=1;y<=1;++y) (for second iteration of outer loop)
for(int y=1;y<=2;++y) (for third iteration of outer loop)
for(int y=1;y<=3;++y) (for fourth iteration of outer loop)
for(int y=1;y<=4;++y) (for fifth iteration of outer loop)

But in your second code its always -
for(int y=1; y<=5; ++y)
for all iterations of outer for loop.

Comments

2

It is very simple the will run for 5 time times and every itreation its value will be increamented by 1 i.e. from 0 to 4.

So in first loop inner loop will have the condition like this:

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

But since in first loop the value of x is 0 hence it literally means:

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

But in the last iteartion of outer loop the value of x is 4 hence this is equivalent to:

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

So it iterates 4 times.

Comments

1

In your first example y is first less than x = 1 and during the next iteration it will be less x= 2 ... Because x values changes with your first for loop.

For the second example however you state that y have to be less than 5 which doesn't change at all.

Comments

1

They are different because in the first case your x varies from 0 to 4 based on : for(int x=0; x<5; x++)

In the case second case x is fixed at 5.

1 Comment

That for loop on y is also to blame for the change in output. On the one hand he fixes the condition to y<=5 on the other ( the first example ) it's y<=x which value varies because of the main loop
0

I have replaced your two inner for loops with a new stringRepeat function. It might be easier to understand this way.

public class ForLoopStarsAndCrosses {

    public static void main(String[] args) {
        /// the total length ot the x's and stars
        final int length = 5;
        // start with 1 x until one less than the length (in this case 4)
        for(int x = 1; x < length; x++) {
            // the number of stars equals the total length minus the number of x's
            final int nbStars = length - x;
            // add the x's and the stars
            final String output = stringRepeat("x", x) + stringRepeat("*", nbStars);
            System.out.println(output);
        }
        System.out.println();
    }

    /** Repeat the string s n times */
    private static String stringRepeat(String s, int n) {
        String result = "";
        for (int i = 0; i < n; i += 1) {
            result += s;
        }
        return result;
    }
}

Comments

0

Firstly, you should understand the difference between value and variable. Value is a constant as you write 5 but variable can be changeable.

For your question, first code and first round:

x = 1, y<= 1 and output: x

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

but for the second code and first round:

y<=5 so output is: xxxxx

for(int y=1; y<=5; y++){
            System.out.print("x");
        }

it is very simple.

Comments

0

The purpose of loops (can be for, while, do-while) is that, how many number of times the same set of statements to be executed under a specific condition. "for" is a definite loop where there will be an index starting with an integer, keep increment it until the condition is achieved. "while" is a indefinite loop where there is no index and it will be executed until the condition is achieved. "do-while" is a loop similar to "while", which will be executed atleast once and then validates the condition for the next iteration.

Based on the above details,

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

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

The diff between these two conditions is that, First condition: In the first iteration, value of x is 0 and for the second loop y is started with 1. Here when it compares the value with x which is 0 and 1<=0 which is false, this condition is failed and the statements under Y will not be executed and the control will go back control of x.

Second condition: In the first iteration, value of x is 0 and for the second loop y is started with 1. Here when it compares the value with 5, 1<=5 which is true, this condition is valid and the statements under Y will be executed and the control will go back control of x until y becomes 6 and condition checked is 6<=5 which is false, this condition is failed and the statements under Y will not be executed and the control will go back control of x.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.