Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Feeling Loopy
- While Loop:
- Psuedo-Code: //simple
- anyQuestions = ask "Are there any questions?"
- while anyQuestions
- answers the questions
- anyQuestions = ask "Any more questions?"
- next slide //steps out and goes to the next slide
- Do-While Loop: like a While Loop, do the stuff in bracket at least once.
- do { //at least once }
- while ();
- Random random = new Random();
- int numberOfPutts = 0; //<- the counter
- boolean ballInHole = false; //<- ball is !ballInHole until we get the ball in hole. //simple
- // Do-While loop
- do {
- System.out.println("Action: Putts ball");
- ballInHole = random.nextBoolean();
- numberOfPutts++; //<-- increment counter
- } while (!ballInHole); //basically loop until the ball goes into the hole.
- System.out.println("you got the ball in " + numberOfPutts);
- For Loop: use when you know the # times you want to loop. An old school way of looping through an Array.
- Psuedo-Code: 99 Bottles of Beer on the Wall
- for (initialize; check condition if True; increment) {
- println(i + "how many bottles of beer on the wall")
- }
- // Real code
- for (int i = 99; i > 0 ; i--) {
- System.out.println(i + " bottles of beer on the wall, take one down, pass it around...");
- }
- Arrays: used to store many objects of the same type in a single variable.
- >Use a for loop to print out each Jackson in the JacksonFive Array
- String[] jacksonFive = {"Michael", "Jackie", "Tito", "Jermaine", "Marlon", "Randy"};
- for (int i = 0; i < jacksonFive.length; i++){
- //pull out a jackson
- String jackson = jacksonFive[i];
- System.out.println("Jackson: " + jackson);
- }
- ^So that is the old way of doing it, its not-so-good pratice now in JAVA although other programming languages still use it.
- >Now we use... *drum roll please*
- For-Each loop aka "for-in" loop: cleaner, faster.
- String[] jacksonFive = {"Michael", "Jackie", "Tito", "Jermaine", "Marlon", "Randy"};
- for (String jackson : jacksonFive){
- //pull out a jackson
- System.out.println("Jackson: " + jackson);
- }
- // When you see the colon (:), read it as βin.β Thus, the loop above reads as βfor each String jackson in jacksonFive.β
- Practice complex loop examples.
- Jim has three tries (ball throws) to get Craig in the dunk tank.
- //While Loop method:
- Random random = new Random();
- boolean dunkedIn = false;
- int timesBallThrown = 0;
- while (!dunkedIn && timesBallThrown < 3 ) {
- timesBallThrown++;
- dunkedIn = random.nextBoolean();
- System.out.println("Time ball is thrown: " + timesBallThrown + " times. " + "Is it dunked in? " + dunkedIn );
- }
- if(dunkedIn){
- System.out.println("Celebrate Craig falling in Water");
- } else {
- System.out.println("Be sad");
- }
- //For Loop method:
- Random random = new Random();
- boolean dunkedIn = false;
- for(int timesBallThrown = 1; !dunkedIn && timesBallThrown <= 3; timesBallThrown++) {
- dunkedIn = random.nextBoolean();
- System.out.println("Times thrown: " + timesBallThrown + " times." + "| Is Craig dunked in? " + dunkedIn);
- }
- if(dunkedIn){
- System.out.println("Celebrate Craig falling in Water");
- } else {
- System.out.println("Be sad");
- }
- Nested loops: Mix IceCream sour and sweet
- String[] sweetFlavors = {"Caramel", "Cinnamon", "Watermelon", "Baked Beans"};
- String[] savoryFlavors = {"Sea Salt", "Potato Chip", "Carrot", "Barbeque Sauce"};
- for(String sweet: sweetFlavors) {
- for (String savory: savoryFlavors){
- System.out.println(sweet + ", AND " + savory);
- }
- }
- //Results:
- Caramel AND Sea Salt
- Caramel AND Potato Chip
- Caramel AND Carrot
- Caramel AND Barbeque Sauce
- Cinnamon AND Sea Salt
- Cinnamon AND Potato Chip
- Cinnamon AND Carrot
- Cinnamon AND Barbeque Sauce
- Watermelon AND Sea Salt
- Watermelon AND Potato Chip
- Watermelon AND Carrot
- Watermelon AND Barbeque Sauce
- Baked Beans AND Sea Salt
- Baked Beans AND Potato Chip
- Baked Beans AND Carrot
- Baked Beans AND Barbeque Sauce
- // Branching statements
- // break - exits the iteration
- // continue - stops executing the current iteration and jumps back to the top to start over again
- // return - if you are inside of the loop, it will immediately exit the method.
Add Comment
Please, Sign In to add comment