+ 1

Why the output of i is 3 instead of 2?

https://code.sololearn.com/c67It57bvLdb/?ref=app

22nd Aug 2017, 8:23 AM
NICKSON ELAI MWALONGO
8 odpowiedzi
+ 4
public class Arrays { public static void main(String[] args) { int a[] ={5,1,15,20,25}; int i,j,m; i = ++a[1]; // 👉here , i becomes ++1 ie 2 j = a[1]++; m = a[i++]; // 👉here , i becomes ++2 ie 3 System.out.println(i); System.out.println(j); System.out.println(m); System.out.println(i+j+m); } }
22nd Aug 2017, 8:26 AM
Changed
Changed - avatar
+ 3
no , see it again a [1] was 1 already , then i = ++ a [1] , ie a [1] becomes 2 now , j=a [1]++ ie j=2++ means j will store 2 hope u got it 😊
22nd Aug 2017, 8:56 AM
Changed
Changed - avatar
+ 3
int a = 1 ; int b = ++a; //b will store value 2 , ie first increase then assign int c =b++ ; //c will store value 2 , ie first assign then increase
22nd Aug 2017, 9:04 AM
Changed
Changed - avatar
+ 2
public class Arrays { public static void main(String[] args) { int a[] ={5,1,15,20,25}; int i,j,m; i = ++a[1]; System.out.println(a[1]); //👉here a [1] becomes 2 j = a[1]++; // 👉now j=a [1]++ = a [1] = 2 m = a[i++]; System.out.println(i); System.out.println(j); System.out.println(m); System.out.println(i+j+m); } }
22nd Aug 2017, 8:41 AM
Changed
Changed - avatar
+ 1
if that is a case why j didin't come to 15?...refer the increment operator at j...it look like that of i
22nd Aug 2017, 8:30 AM
NICKSON ELAI MWALONGO
+ 1
think of this...... if a = 2 b= 2 then ++a = 3 and b++ = 2..... you want to mean that it work different in arrays????
22nd Aug 2017, 8:45 AM
NICKSON ELAI MWALONGO
+ 1
so what is the effect of increment operator before and after the variable???
22nd Aug 2017, 8:58 AM
NICKSON ELAI MWALONGO
+ 1
thanx...guyz!!
22nd Aug 2017, 9:28 AM
NICKSON ELAI MWALONGO