0

May someone explain to me why the following blocks of code generate such different outputs?

public class hello { public static void main(String args[]) { int a,b,c; for (a = 0; a < 5; a++) { for (b = 4; b >= a; b--) { System.out.print(" "); } for (c = 0; c <= a - b; c++) { System.out.print("*"); } System.out.println(); } } } 

Output:

enter image description here

public class hello { public static void main(String args[]) { int a,b,c; for (a = 0; a < 5; a++) { for (b = 4; b >= 0; b--) { System.out.print(" "); } for (c = 0; c <= a - b; c++) { System.out.print("*"); } System.out.println(); } } } 

Output:

enter image description here

Shouldnt the outputs be the same since b >= a is equivalent to b >= 0 as b's value will be deducted by 1 for every loop?

4
  • These are nested loops, for each value a from 0 to 4 (smaller than 5), you loop over each value from 4 to a (first time that be 0, then 1, then 2, etc) Commented Mar 23, 2017 at 9:54
  • And when a == 1 or a == 2? its not the same as a == 0 Commented Mar 23, 2017 at 9:55
  • I thought b would have new value for each loop as b-- is there and that would mean the loop would be smaller every time... so it does nothing in this case? Commented Mar 23, 2017 at 10:02
  • Here in first program b is dependent to a so spaces are printed in decreasing order in each line . But in second program b is same throughout the loop but c is dependent to a so it printed 'star' in increasing order. Commented Mar 23, 2017 at 10:08

3 Answers 3

2

No, the outputs cannot be the same, since every time you are executing

for (a = 0; a < 5; a++) 

this gets executed as well (5 times)

for (b = 4; b >= a; b--) { System.out.print(" "); } 

But the value of a is changing with every iteration: a will be initially 0, then 1, 2, 3 and finally 4.

Hence, the number of spaces that you are printing in the first scenario will decrease with every iteration of a.

For a = 0 we have: b = 4, b = 3, b = 2, b = 1, b = 0 (for loop stops since b=-1 is not >= a=0)

For a = 1 we have: b = 4, b = 3, b = 2, b = 1 (for loop stops since b=0 is not >= a=1)

For a = 2 we have: b = 4, b = 3, b = 2 (for loop stops since b=1 is not >= a=2)

For a = 3 we have: b = 4, b = 3 (for loop stops since b=2 is not >= a=3)

For a = 4 we have: b = 4 (for loop stops since b=3 is not >= a=4)

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

4 Comments

I thought b would have new value for each loop as b-- is there and that would mean the loop would be smaller every time, and that would be the same as for the first block of code... so in this case the b-- does nothing, and b still have the same value for every loop?
b will start every time at value 4, but the loop will be smaller because the condition b >= a will be false earlier, with the increase of a from the first for loop.
I was pretty confused as the b-- is there but the value of b stays the same through out the loop, so now i know that it does not change the value of b. Thanks!
I edited my post to make it more clear: the value of b does change during the loop in which it is used as index due to the b-- expression. It will always start at value 4 and will stop depending on the value of a.
2

variable a will go from 0 to 4, so for each iteration you will have:

for (b = 4; b >= 0; b--) { [...] for (b = 4; b >= 1; b--) { [...] for (b = 4; b >= 2; b--) { [...] 

3 Comments

so the b-- does nothing in this case? sorry, I am new to java :(
I thought b would have new value for each loop as b-- is there and that would mean the loop would be smaller every time
@ThànhĐạt the best you can do is to write down each iteration on a paper with each variable value. You have to understand each parts of for-loop too. First is init, second is the condition before iteration and the last one is the statement executed after iteration.
1

Please read the comments in the code and you will se the difference!

 int a, b, c; for (a = 0; a < 5; a++) { for (b = 4; b >= a; b--) // Print every time b-a + 1 underscores... since you start every time with b=4 you have for each a one space fewer { System.out.print(" "); } for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time a-1)... // (first a=0 -b=-1)+1=2 and any time it will prit 2 stars scince { System.out.print("*"); } System.out.println(); } for (a = 0; a < 5; a++) { for (b = 4; b >= 0; b--) // Print every time b-a + 1=4 underscores { System.out.print(" "); } for (c = 0; c <= a - b; c++) // and then print (a-b)-c +1 stars (b is every time -1)... // first time ( a=0 -b=-1)+1 =2 , second time (a=1 - b=-1)+1=3 { System.out.print("*"); } System.out.println(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.