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!

+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)};

4/13/2021 9:50:17 AM

Devil Coder

10 Answers

New Answer

+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.

+3

https://www.sololearn.com/Discuss/2247390/?ref=app

+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?

+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?

+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.

+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

+1

🅰🅹 (Challenge Accepted) thanks brother now I got it👍.

+1

🅰🅹 (Challenge Accepted) it was by mistake😁

0

Devil Coder Then why dislike. 😃😃

0