hehe haba noh..pero mas simple explanation ko:) For loops naman its really just how it is a loop paulit ulit lang hehe
Thing is the syntax allows you to control how much it repeats. Loops save you time becuase it lets you code without typing the same thing over and over again.
For example if i want to pring the numbers 1 to 10. I can do it the long way by typing println then numbers 1 to 10 using it ten times and ten lines. But with a loop you can do it in just a few lines. The basic loop is the for loop.
for(declaration; condition; increment) -> as i said in arrays everything needs to be declared so in a for loop you decide how its declared most common is how many times it will loop so most ppl use an integer. But the secret with loops is you assign a variable meaning like a letter to the number. That way you can use it any time. for example :
for(int i = 1 )(first part) --> meaning im going to start the loop at number 1. where i represents the number 1 also.
next part is the condition meaning how many times will you loop before you stop so its be like this :
for(int i = 1; i<10)(second part) --> meaning im starting the loop at 1 and until my count is still less than 10 so here it will loop until 9 becuase thats the last possible value i can go to thats less than 10. Unless the its
< meaning less than or equal to i can go up to 10.
lastly is the one that makes the loop work. If i start at 1 what makes it go to 2? this is where you put the increment.
for( int = 1; i < 10; i++) --> i++ is the java syntax used to denote i = i +1. its the same. So remember our i = 1 right so when i say I++ it means after the loop is over add 1 to it so i = 1+1
so when the loop starts again its set to 2 and thats your second loop. so when its i++ again. i = 2+1
then goes to 3. gets?
Now the beauty of loops. Il now combine our array lesson and loops together.
remember i can assign values to the array one by one right. with a loop you can assign the numbers 1-5 for example in simple code.
class assignArray{
int[] myArray = new int[5];
public static void main(string[] args) {
for(int c = 0; bc <5; c++) { --> i can use different variable names so Ill use C instead hehe
myArray[c] = c;
System.println("Count :" + myarray[c]);
c++:
}
}
}
Basically what i did was simple. i created the array right of 5 spaces. then i made a loop which will count starting from 0 until the the condition is satisfied for until 4 which is the last possible value. then remember i said you can use variable in different ways since our b = 0 is also like the number 0 itself. so under the loop saying myArray[c] = c is the same as myArray[0] = 0. Start seeing the wonder?hehe if used numbers then wala parang andun lang cia pero by using c. numbers are always changing as b increments so whatever the value of i is at any time so will the other b's in the code. So after assigning to the array i print it out to see so first count should be Count: 0 and so on.
after remember c++? well in the loop statement that just sets the rules but to increment you have to do it manually. thats why i put i++ at the bottom meaning after everything is done. do the add before the loop ends. so on the next round i will be equal to 1 and the statements myArray[c] = c will now be equal to myArray[1] = 1.
the other type of loop is the while loop. While loops are simpler. They dont stop while the condition is still true.
example of this is :
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
so here in this example i declared a variable named count and gave it the value of 1. now i put in a while loop with a condition count <11 so this doesnt stop until the condition is satisfied. meaning il keep printing count is : 1 all the way til 10 since thats when the condition is satisfied. You might be asking whats the difference of for and while. Well maybe youll learn that in class soon hehe pero while loops arent as controlled as for loops are. while loops can be infinite depending on the condition until you stop it manually. for loops you set its limit already.
Anyways others might have better comments to help you out pero i hope this small tutorial helps you
back to the music forums for now hehehe:)