Find logic behind this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Find logic behind this code

using System; public class Program { public static void Main(string[] args) { int i, j = 1, k; for (i = 0; i < 5; i++) { k = j++ + ++j; Console.Write(k + " "); } } } Ans: 4 8 12 16 20

3rd May 2019, 3:33 PM
$@G@®️
$@G@®️ - avatar
7 Answers
+ 2
Maninder $ingh You can define variables without assigning values to them. The values can be assigned later. int a; a = 5 int b, c , d; b= 3, c =a+b Console.Write(a+b+c);
3rd May 2019, 4:51 PM
Lord Krishna
Lord Krishna - avatar
0
it's looping 5 times (counting the i from 0 to 4) and then j is incremented in a postfix manner which means use the current value of j then add 1 to j...then the next increment is a prefix which means add 1 to the value of j then use j...so that would be: 1 + 3 = 4 3 + 5 = 8 5 + 7 = 12 7 + 9 = 16 9 + 11 = 20
3rd May 2019, 3:55 PM
George Samanlian
0
Am noob in c#. Does in this line int i,j=1,k; Here i==1 and j==k but where he define k in programme am totally confuse.
3rd May 2019, 4:32 PM
Maninder $ingh
Maninder $ingh - avatar
0
~ swim ~ i know i have done half c# course but sometimes i confuse what people write python is so easy.
3rd May 2019, 4:38 PM
Maninder $ingh
Maninder $ingh - avatar
0
~ swim ~ can you tell me answer my question.
3rd May 2019, 4:39 PM
Maninder $ingh
Maninder $ingh - avatar
0
~ swim ~ i know all concepts which i have completed in my half c# course confusion is here what is meaning of line 6 which i never seen in course.where he define k variable.
3rd May 2019, 4:45 PM
Maninder $ingh
Maninder $ingh - avatar
0
~ swim ~ i know everything which you are mentioning in your answer. Am ask you he wrote a line int i,j=1,k Here the value of i is 1 and value of j is k here j is int means k should also be int but k is not defined in his programme am totally confuse.
3rd May 2019, 4:56 PM
Maninder $ingh
Maninder $ingh - avatar