- 1

Why does this code output 8?

Can u please explain how does this code output 8 i dont get it int x = 2; for (int i=1; i <=3; i++) { x += i; } System.out.print(x); https://code.sololearn.com/cLqlXMbQEIU7/?ref=app

12th Oct 2021, 11:35 AM
Islam Puth
Islam Puth - avatar
13 ответов
+ 6
2 + 1 = 3 3 + 2 = 5 5 + 3 = 8 That is how you get x = 8
12th Oct 2021, 11:37 AM
Brain & Bones
Brain & Bones - avatar
+ 1
Elsa now i got it thnx so much u helped me alot 💜
12th Oct 2021, 11:51 AM
Islam Puth
Islam Puth - avatar
+ 1
int x = 2; int i=1; i <= 3 // 1 <= 3 true x = x + i; // 2 + 1 = 3 i = i + 1 // 1 + 1 = 2 i <= 3 // 2 <= 3 true x = x + i; // 3 + 2 = 5 i = i + 1 // 2 + 1 = 3 i <= 3 // 3 <= 3 true x = x + i; // 5 + 3 = 8 i = i + 1 // 3 + 1 = 4 i <= 3 // 4 <= 3 false .print(x) // 8
12th Oct 2021, 12:07 PM
zemiak
0
Brain & Bones i dont understand why did u combine 2+1 and then 3+2 and then 5+3:---:
12th Oct 2021, 11:39 AM
Islam Puth
Islam Puth - avatar
0
Islam Puth I wrote this code especially for you to clearly demonstrate how the "For" loop works: https://code.sololearn.com/cA9tGLDoM1Tf/?ref=app
12th Oct 2021, 12:45 PM
Solo
Solo - avatar
0
2+1=3 3+2=5 5+3=8 I is being added to x Everytime the when the code is run.
12th Oct 2021, 6:36 PM
Tonny Makondesa
Tonny Makondesa - avatar
0
Can anyone explain what is href
14th Oct 2021, 4:04 AM
Archit Chitode
Archit Chitode - avatar
0
href is used in html . In this attribute we paste our link for the element.
14th Oct 2021, 7:56 AM
Shivam Sahu
Shivam Sahu - avatar
14th Oct 2021, 8:35 AM
Archit Chitode
Archit Chitode - avatar
- 1
At start , x = 2 Inside for loop: i = 1<=3 (condition satisfied)=> x = 2+1=3 [x = x +i] i = 2<=3 (satisfied)=> x = 3+2 =5 i = 3 <=3(satisfied)=> x = 5+3=8 Hope this helps u!
14th Oct 2021, 3:14 AM
Dholu Hunn
Dholu Hunn - avatar
- 1
Thanks for answer
14th Oct 2021, 8:31 AM
Archit Chitode
Archit Chitode - avatar
- 1
Why is src used , what's the meaning of src in html
14th Oct 2021, 8:34 AM
Archit Chitode
Archit Chitode - avatar
- 1
src is used for the path of a file you use in html.
14th Oct 2021, 9:01 AM
Shivam Sahu
Shivam Sahu - avatar