1

Can someone please explain this piece of code for me, I am a beginner and I couldn't understand the For Loop here, This code can arrange the numbers from smallest to largest.

int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); int c = Convert.ToInt32(Console.ReadLine()); int d = Convert.ToInt32(Console.ReadLine()); int [] sayilarr ={a,b,c,d}; Array.Sort(sayilarr); string ss = ""; for (int i = 0; i < 4; i++) { ss += sayilarr[i]; } Console.WriteLine(ss); 
2
  • numbers are arranged with Array.Sort(sayilarr); Statement, not with for loop. Commented Jan 10, 2016 at 20:16
  • Just try and run the code. Step through it in debug mode when you still not get it. Don't post this kind of questions on StackOverflow. Commented Jan 10, 2016 at 21:39

1 Answer 1

5

This code takes 4 inputs from the Console, converts them to Int and saves them into 4 variables

 int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); int c = Convert.ToInt32(Console.ReadLine()); int d = Convert.ToInt32(Console.ReadLine()); 

Then you declare an array, which will contain the four ints from the variables

 int [] sayilarr ={a,b,c,d}; 

Now this array is sorted, no magic loop trick ...

 Array.Sort(sayilarr); 

Here a string variable is declared, initially being an empty string

 string ss = ""; for (int i = 0; i < 4; i++) { 

in each of the four loops the content of the array on index "i" is concatenated

 ss += sayilarr[i]; } 

Finally, the string is shown to you

 Console.WriteLine(ss); 

Hope this is clear now :-)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.