see here for examples
Just like in Alice or AppLab it is often useful to
use loops to repeat code statements. Even if something is being
repeated just 2x, it is useful to use loops because:
- Makes coding faster
- Better readability
- If you make a change to your code in the loop, you change it
in one place.
The two main types of loops are
while (condition is true)
are in the format: for (this counter variable staring here, while
this condition met, do this to the counter)
for (int i=0; i<10; i++)
System.out.println("We are at" + i);
this would repeat the inside 10 times (while i was 0 through 9)
Exiting (not used all that often, just reference)
To exit the current loop, you use the statement:
Lets create these methods: HERE IS STARTER CODE
- public void printNBA2KFor() that will print I played NBA2K all night 50 times.
Do this with a for loop
- public void printMadnessWhile() that will print I will not
talk about March Madness in class 50 times.
Do this with a while loop
- public void printFibonacci(int values) that will print x fibonacci numbers