if we use any varible in any if else statement as a true or false statements then does it overwrite the main value after ifelse? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

if we use any varible in any if else statement as a true or false statements then does it overwrite the main value after ifelse?

int a =5; int b =4; if ( a++ < b) { System.out.println(b)}; else {System.out.println(a)};

13th Apr 2021, 9:50 AM
Devil Coder
Devil Coder - avatar
10 Answers
+ 3
Devil Coder Yes because you didn't use pre increment or post increment. You have just added 2 with a but actually it will not add 2 in a. It will just check condition so a is remain same here but if you print a + 2 then output will be different.
13th Apr 2021, 10:15 AM
A͢J
A͢J - avatar
9th Jun 2021, 8:41 AM
Tharul Nejana
Tharul Nejana - avatar
+ 2
Devil Coder No it will not override it will increment by 1 as you used a++ According to your condition a++ < b is false because 5 < 4 is false so it will go in else part but remember there is Post increment a++ so after checking condition a will be 6. That's why you got output 6. This was your doubt. Am I right?
13th Apr 2021, 9:55 AM
A͢J
A͢J - avatar
+ 2
🅰🅹 (Challenge Accepted) yep kind of it I understand but if I do increment then the value of a change but if I try any other value then the value of remains unchanged like if(a+2>b) { System.out.println(b); } else{ System.out.println(a); } why it is changed in increments and unchanged in other value?
13th Apr 2021, 10:12 AM
Devil Coder
Devil Coder - avatar
+ 2
Devil Coder See a++ is post increment which first assign value then increment by 1 But a + 2 is not a post increment. Here you are just adding 2 in a but remember a will be remain same For example : a = 10 So if you print a + 2 then output will be 12 but if you just print a then output will be 10.
13th Apr 2021, 10:18 AM
A͢J
A͢J - avatar
+ 2
It is post increment so, it first compare and then increment the value. a++ < b is 5<4 which is false If(false) Then it will immediately go to else block In else block it will print the incremented value of a i.e, 6
15th Apr 2021, 6:14 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
+ 1
🅰🅹 (Challenge Accepted) thanks brother now I got it👍.
13th Apr 2021, 10:27 AM
Devil Coder
Devil Coder - avatar
+ 1
🅰🅹 (Challenge Accepted) it was by mistake😁
13th Apr 2021, 2:10 PM
Devil Coder
Devil Coder - avatar
0
Devil Coder Then why dislike. 😃😃
13th Apr 2021, 10:30 AM
A͢J
A͢J - avatar
0
Hello
13th Apr 2021, 7:59 PM
Rayane 2000
Rayane 2000 - avatar