Jump to content

Is there a better way to understand how to write the code in C#? I've been using visual academy from the microsoft site, and for the first few lessons, I've understood it completely. Then I got to arrays, and the for statement (int i = 0; i < 10; i++), but I have no idea what they mean, or even how to use them in a program. What do they do? What are they for?

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/
Share on other sites

Link to post
Share on other sites

An array is just a bunch of variables stored in memory beside each other. They are accessed by giving the index of the array you want.

 

example: To get the 4 element of the array b you write b[3], since arrays starts indexing at 0

 

A for loop is used when you want to iterate over a list or something that you know how many times you want to loop.

Perfect for using with arrays.

 

example: int i will be the index variable, so we know where we are in the array, the second statement in the for loop is the the condition that has to be met for the block to be run and the final statement is what to do after each iteration. This code will print the index and value of all elements in the array b.

for(int i = 0; i < b.length; i++) {     print(i + " " + b[i]);}

Let me know if you have any questions

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/#findComment-7049490
Share on other sites

Link to post
Share on other sites

Java beginner here(correct me if I'm wrong), not C++ but I think I can explain as they're both Object Orientated and the fundamentals are essentially the same. An array is a better way to group your variables into a tidier thing(not the most technical word, whatever), think of creating an excel file spreadsheet for data instead of making a separate file for each entry. The for statement is declaring a variable that it uses as a counter in it's first section separated by semicolons. So int i = 0 is saying that the integer type variable "i" is equal to 0. Then it's checking in the second section to see if it should run; the check should probably return a boolean(I think). So i < 10 is checking to see if the integer type variable"i" is smaller than 10, if it is it runs the last section, i++ which means add 1 to "i". the for loop will repeat until "i" no longer is smaller than 10.

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/#findComment-7049522
Share on other sites

Link to post
Share on other sites

Is there a better way to understand how to write the code in C#? I've been using visual academy from the microsoft site, and for the first few lessons, I've understood it completely. Then I got to arrays, and the for statement (int i = 0; i < 10; i++), but I have no idea what they mean, or even how to use them in a program. What do they do? What are they for?

Dude if you can afford, just go to Lynda.com

 

Freakin' worth it...

CPU: Intel Core i5 3570K ( raped to 4.4Ghz) | Motherboard: Gigabyte GA-Z77X-UD3H | RAM: Corsair Vengeance 8GB | GPU: ASUS GTX 980 Strix


Case: Fractal Design DefineR3Storage: 256GB Crucial MX100 & 1TB Samsung Whatevs | Display(s): ASUS MG279Q1x Cheap LG Shit


PSU: Corsair HX650 or something | Cooling: Corsair H80i | Keyboard: Ducky Shine 3 | Mouse: Roccat Kone Pure | Sound: SoundBlaster Recon3D PCI-e

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/#findComment-7049798
Share on other sites

Link to post
Share on other sites

Java beginner here(correct me if I'm wrong), not C++ but I think I can explain as they're both Object Orientated and the fundamentals are essentially the same. An array is a better way to group your variables into a tidier thing(not the most technical word, whatever), think of creating an excel file spreadsheet for data instead of making a separate file for each entry. The for statement is declaring a variable that it uses as a counter in it's first section separated by semicolons. So int i = 0 is saying that the integer type variable "i" is equal to 0. Then it's checking in the second section to see if it should run; the check should probably return a boolean(I think). So i < 10 is checking to see if the integer type variable"i" is smaller than 10, if it is it runs the last section, i++ which means add 1 to "i". the for loop will repeat until "i" no longer is smaller than 10.

Arrays in Java are actually objects with properties like length but in C/C++ they are essentially just a pointer to an address in memory. And to get the length of this array you need to either calculate sizeof(array) or outside of the scope of the array, the length must be passed to another function, for example.

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/#findComment-7050762
Share on other sites

Link to post
Share on other sites

Arrays in Java are actually objects with properties like length but in C/C++ they are essentially just a pointer to an address in memory. And to get the length of this array you need to either calculate sizeof(array) or outside of the scope of the array, the length must be passed to another function, for example.

Ah I see

Link to comment
https://linustechtips.com/topic/531391-understanding-it-better/#findComment-7050791
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×