How it works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How it works?

What will be the result? int n; n = (7 * 5 > 6 * 6) ? (7 * 9 > 8 * 8) ? 11 : 12 : (7 * 8 > 9 * 6) ? 13 : 14; Console.Write(n); (output 13)

5th Aug 2018, 2:40 PM
pro
pro - avatar
4 Answers
+ 1
The ? : acts as a short version of if else statement so to write it out. int n; n = (7 * 5 > 6 * 6) ? (7 * 9 > 8 * 8) ? 11 : 12 : (7 * 8 > 9 * 6) ? 13 : 14; Console.Write(n); (output 13) will equal to int n; if (7 * 5 > 6 * 6){ if(7 * 9 > 8 * 8){ n = 11; } else{ n = 12; } } else if(7 * 8 > 9 * 6){ n = 13; } else{ n = 14; } Console.Write(n); (output 13) sence the first if statment is false it will skip the inside if statment and go to the else if that one is true so it makes n = 13. hope this helps and isnt to confusing. also did this fast so sorry if there are any small errors in what i wrote out.
5th Aug 2018, 3:01 PM
Mooaholic
Mooaholic - avatar
+ 1
this one is just because of the way it is adding the letters to make the word, if the letter in the function had ‘ ‘ then it is a char and will run the second function which is doing str(the word your making) = ‘u’ + str(currently “”) this will make str “u” now. second one uses “ “ which is a string so it will run in the first function which uses str(currently “u”) = str(“u”) + “n” this makes str now “un” now the last one is a ‘ ‘ again so it runs on the second char function so the last time we add will be str(currently “un”) = ‘f’ + str(“un”) making str = “fun” so its all about how its being added you have to watch if its a char or string and which side your adding to.
5th Aug 2018, 3:49 PM
Mooaholic
Mooaholic - avatar
0
#Mooaholic-great explanation,thanks,can you explain me another code
5th Aug 2018, 3:18 PM
pro
pro - avatar
0
l hoped to see output-unf,but really output is fun,l am so confused static string str = ""; static void Concat(string s) { str= str + s; Console.WriteLine("string"); } static void Concat(char c) { str = c + str; Console.WriteLine("char"); } static void Main() { Concat('u'); Concat("n"); Concat('f'); Console.WriteLine(str); }
5th Aug 2018, 3:19 PM
pro
pro - avatar