0

I can't understand how and what is added here :( Не могу понять как и что тут прибавляется :( how to understand sum+=i++

I welcome everyone. dear java gurus, tell a beginner on the fingers how to calculate this operator expression.I was reading Herbert Schildt's Java 8 beginner's guide and then I saw an interesting line. I don't understand how the final amount is calculated. Help me understand, thanks in advance int i; int sum = 0; for(i = 1; i <= 5; sum += i++) ; <--------- System.out.println("Sum: " + sum); 15 🤔 /************************************************/ Приветствую всех. Уважаемые java-гуру, расскажите новичку на пальцах, как вычислить этот оператор выражение. Я читаю книжку Герберта Шильдта руководстводля начинающих Java 8, и тут я увидел интересную строку. Я не понимаю, как рассчитывается окончательная сумма. Помогите разобраться, заранее спасибо int i; int sum = 0; для (i = 1; i <= 5; sum + = i ++); <--------- System.out.println ("Sum:" + sum); 15 🤔 как получилось 15?? 1+1+1=3 *5? Так? Но когда меняю на другие цифры я считаю не правильно. Буду благодарен за помощь

18th Mar 2021, 4:29 PM
Азизов И. A.
Азизов И. A. - avatar
14 Réponses
+ 1
and last b = 3 + 3
18th Mar 2021, 5:29 PM
visph
visph - avatar
+ 3
oh, if you don't know, a += b is a shortcut for a = a + b...
18th Mar 2021, 4:44 PM
visph
visph - avatar
+ 2
for (i=1; i<=5; sum+=i++); is a loop, wich do nothing at each body iteration (notice the semi-colon next to closing parenthesis)... however, that doesn't mean the loop run without doing anything: the parenthesis contains 3 parts separated by semi-colons: 1) initialization (executed only once just before loop start), usually initializing a counter variable 2) condition (checked at each iteration start) 3) part executed at each iteration end (usually dec/incrementing counter) in this case, counter i is initialized to 1, and loop run while i<=5... at each iteration, i is added to sum, then incremented (post-increment) so the loop compute sum of all whole number from 1 to 5 (included) in variable sum (wich is initialized to zero just before the 'for' statement ;)
18th Mar 2021, 4:43 PM
visph
visph - avatar
+ 2
if you initialize b to zero, each steps are: b = 0, a = 1 (start) b = 0 + 1, a = 2 (loop 1) b = 1 + 2, a = 3 (loop 2) (end, as a >= 3) so there's only two iterations, b ends holding 3 and a hold 3 also ;)
18th Mar 2021, 5:11 PM
visph
visph - avatar
+ 1
yes if you put ++ after b...
18th Mar 2021, 4:52 PM
visph
visph - avatar
0
after i have added a + b then i do increment b?
18th Mar 2021, 4:50 PM
Азизов И. A.
Азизов И. A. - avatar
0
Int a,b; For(a=1; a<3; b+=a++); I think at first: a = 1 + b = 1 a ++ == 3? and so every 3 iteration?
18th Mar 2021, 5:05 PM
Азизов И. A.
Азизов И. A. - avatar
0
I just started to see the light at the end of the tunnel🥴 I take more information on examples, as you did the last
18th Mar 2021, 5:18 PM
Азизов И. A.
Азизов И. A. - avatar
0
a < 4 b = 0, a = 1 (start) b = 0 + 1, a = 2 (loop 1) b = 1 + 2, a = 3 (loop 2) b = 2 + 3, a = 6 (loop 3) (end, as a >= 4) did I make the right decision?😍
18th Mar 2021, 5:26 PM
Азизов И. A.
Азизов И. A. - avatar
0
yes ;)
18th Mar 2021, 5:27 PM
visph
visph - avatar
0
oh, not exactly: last a == 4 not 6 (a get only 1 added each turn ^^)
18th Mar 2021, 5:28 PM
visph
visph - avatar
0
Yeaaaaaaaahhhhhhh🥰🕺🕺🕺thank you friend
18th Mar 2021, 5:29 PM
Азизов И. A.
Азизов И. A. - avatar
0
a < 4 b = 0, a = 1 (start) b = 0 + 1, a = 2 (loop 1) b = 1 + 2, a = 3 (loop 2) b = 2 + 3, a = 4 (loop 3) b = 3 + 4, a = 5 B = 4 + 5, a = 6, loop5 B = 5+6, a = 7, loop6 B = 6 +7,a = 8 loop 7 (end, as a >= 7) B = 6+7=13, a = 8 (13+8=21)🤩🤩🤩🤩🤩🤩
18th Mar 2021, 5:38 PM
Азизов И. A.
Азизов И. A. - avatar
0
if your condition is a<7 not a<4, then there's no 7th loop... and your b is false since 4th loop: a = 1, b =0 for (; a<7; b += a++); 1) b = 0 (b) + 1 (a) = 1, a = a + 1 = 2 2) b = 1 (b) + 2 (a) = 3, a = a + 1 = 3 3) b = 3 (b) + 3 (a) = 6, a = a + 1 = 4 4) b = 6 (b) + 4 (a) = 10, a = a + 1 = 5 5) b = 10 (b) + 5 (a) = 15, a = a + 1 = 6 6) b = 15 (b) + 6 (a) = 21, a = a + 1 = 7 end as a>=7
18th Mar 2021, 5:46 PM
visph
visph - avatar