Why "2" > "12" greater ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why "2" > "12" greater ?

Anyone know why

22nd May 2021, 1:22 PM
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ - avatar
4 Answers
+ 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