Jump to content

Loops & Arrays...

- - - - -

  • Please log in to reply
10 replies to this topic

#1
///ViNcE

///ViNcE

    Newbie

  • Members
  • PipPip
  • 18 posts
Yes, I'm new but not to programming as I have been working with that for a while. My trouble is loops, I never can get them nor learn how to make them along with arrays. Is there any chance that someone could provide resources like practices for me so I can try to practice them a little more???

Thanks in advanced...

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I do not know so much of Java, so this code may not run, the aspects are correct though.

This example uses an array, and loops through it printing each element.
integer array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
integer i;
for (i = 0; i < 10; i++) {
  print array[i]; //will print array[0], array[1] .... array[9]
}
You can use a foreach like syntax, which will handle the array looping automatically:
integer array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (integer num as array) {
  System.out.println(num); //num will automatically be the next element in "array"
}

EDIT: D'oh.. C#..

Edited by Alexander, 30 January 2011 - 09:18 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Im not to sure of any websites like that but I can point you to some tutorials, and give you a small assignment?
http://forum.codecal...ial-arrays.html
http://forum.codecal...-looping-c.html

After you read those tutorials, you could try and make a small program that assigns the value's of an array through a for loop(say it assigns the numbers 1-10 into the array). Then prints it out through another for loop.

Once your done post your code, or if you get stuck show us what you got an we can try to help.

Good luck ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

I do not know so much of Java, so this code may not run, the aspects are correct though.
Dont worry this is the C# section. :D
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#5
///ViNcE

///ViNcE

    Newbie

  • Members
  • PipPip
  • 18 posts
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};


            foreach(int num in array)

            {

                Console.WriteLine("{0}", num.ToString());

            }


            Console.ReadLine();

Is this what you were thinking of for an assingment???

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};

            foreach(int num in array)
            {
                Console.WriteLine("{0}", num.ToString());
            }

            Console.ReadLine();

Is this what you were thinking of for an assingment???

Good job! But I ment using a for loop like this(Your loop is called a 'foreach' loop) :
for (i = 0; i < 10; i++) {
  //Some code here
}

Hope your learning. BTW the best way to get used to loops it to just work with them, loops confuse newer programmers all the time. ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#7
///ViNcE

///ViNcE

    Newbie

  • Members
  • PipPip
  • 18 posts
Maybe it's just me but when does the programmer actually learn & use loops without looking or thinking or really any code??? I can do a Console.Writeline & a Console.Readline() without thinking along with a if & then statement but not really a loop statement.

#8
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
All you need is patience. Just keep using them and practicing and you will be able to use them with ease just like Console.Write ect... Another tip if you cant quite remember how the loops it written but dont want to look you can right click and click 'Insert Snippet' then the C# folder, then find the if, do, for, foreach, or while loop and double click it. It will write the loop for you. Just a tip that might help you in remembering the loops.

~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#9
///ViNcE

///ViNcE

    Newbie

  • Members
  • PipPip
  • 18 posts
Ya, but I learn from just typing it in, not snippets. Yes, patients is what i use but now I got issues such as going into the Airforce & finishing up school too :/

#10
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Maybe I should rephrase it to, persistence. ;)

Dont worry, and dont give up, and you will reach your goals. ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#11
///ViNcE

///ViNcE

    Newbie

  • Members
  • PipPip
  • 18 posts
Yes, yes :D
Thank you very much again for the help :D




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users