Why "2" > "12" greater ? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Why "2" > "12" greater ?

Anyone know why

22nd May 2021, 1:22 PM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
4 ответов
+ 8
Because "2" and "12" are both strings. In strings, the comparison is done character by character. The nth letter of the first string is compared with the nth letter of the second one. As soon as a character is found in the first string which is greater (for '>' operator) than the one in second string, true is returned. In the case of "2" and "12", first '2' and '1' will be compared. As the ascii value of '2' > ascii value of '1', true is returned.
22nd May 2021, 1:30 PM
XXX
XXX - avatar
+ 2
Por lo que veo estas evaluando cadenas de texto, que es muy diferente a evaluar valores bien numéricos
22nd May 2021, 3:43 PM
Cmurio
Cmurio - avatar
+ 1
mention any one language in the tags.
22nd May 2021, 1:25 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
I understand but "5">"4" in case this statement "12"<"2" , "2" is greater because compiler check 1st digit compare.
22nd May 2021, 2:02 PM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar