+ 7

String output

Please explain why the first two numers add up, but the last two don't? https://code.sololearn.com/cZ973t5fmqy0/?ref=app

9th Sep 2019, 5:49 PM
Iryna
Iryna - avatar
11 odpowiedzi
+ 7
Because after the first two numbers, there is a string and everything is evaluated as string and every number after the quotes is concatenated.
9th Sep 2019, 6:04 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 5
int: 1+1 = 2 String: 2+"1+1" = "21+1" String: "21+1"+1 = "21+11" String: "21+11"+1 = "21+111"
9th Sep 2019, 9:49 PM
zemiak
+ 4
No, but the program didn't recognize the assignment as string until you wrote +"" then it started behaving like a string the same as if you will use the () as I hava already said the behavior will be like int because it doesn't care about the part outside
9th Sep 2019, 6:34 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 2
That is, despite the fact that this is String, the first two numbers are perceived as int?
9th Sep 2019, 6:32 PM
Iryna
Iryna - avatar
+ 2
Thanks everyone!
9th Sep 2019, 6:43 PM
Iryna
Iryna - avatar
+ 2
Because the "+" operator in the quoted string acts as a string delimiter operator :-) (two strings are concatenated)
11th Sep 2019, 11:30 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
In addition to TheWh¡teCat you can add () on the last two digits and see that they will evaluate to 2
9th Sep 2019, 6:28 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
0
2+1+2=21+2
11th Sep 2019, 8:25 AM
Alin Ally
Alin Ally - avatar
0
Easy
11th Sep 2019, 9:22 AM
Zia
Zia - avatar
0
Print("halo world")
11th Sep 2019, 4:26 PM
João Fernandes
João Fernandes - avatar
0
Hello lryna
12th Nov 2020, 9:26 AM
Adarsh Singh