yo2man

Java Feeling Loopy Cheat Sheet

Mar 15th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. Feeling Loopy
  2.  
  3. While Loop:
  4.  
  5.     Psuedo-Code:  //simple
  6.    
  7.     anyQuestions = ask "Are there any questions?"
  8.     while anyQuestions
  9.         answers the questions
  10.         anyQuestions = ask "Any more questions?"
  11.    
  12.     next slide //steps out and goes to the next slide
  13.    
  14.    
  15. Do-While Loop: like a While Loop, do the stuff in bracket at least once.
  16.  
  17. do { //at least once }
  18. while ();
  19.  
  20.  
  21.         Random random = new Random();
  22.         int numberOfPutts = 0;           //<- the counter
  23.         boolean ballInHole = false;      //<- ball is !ballInHole until we get the ball in hole. //simple
  24.        
  25.         // Do-While loop
  26.         do {
  27.             System.out.println("Action: Putts ball");
  28.            
  29.             ballInHole = random.nextBoolean();
  30.            
  31.             numberOfPutts++; //<-- increment counter
  32.            
  33.         } while (!ballInHole);      //basically loop until the ball goes into the hole.
  34.        
  35.         System.out.println("you got the ball in " + numberOfPutts);
  36.        
  37.        
  38.        
  39. For Loop: use when you know the # times you want to loop.  An old school way of looping through an Array.
  40.  
  41.     Psuedo-Code: 99 Bottles of Beer on the Wall
  42.  
  43.     for (initialize; check condition if True; increment) {
  44.        println(i + "how many bottles of beer on the wall")
  45.     }
  46.    
  47.     // Real code
  48.     for (int i = 99; i > 0 ; i--) {
  49.         System.out.println(i + " bottles of beer on the wall, take one down, pass it around...");
  50.     }
  51.    
  52.    
  53.    
  54.    
  55. Arrays: used to store many objects of the same type in a single variable.
  56.  
  57.    >Use a for loop to print out each Jackson in the JacksonFive Array
  58.    
  59.         String[] jacksonFive = {"Michael", "Jackie", "Tito", "Jermaine", "Marlon", "Randy"};
  60.         for (int i = 0; i < jacksonFive.length; i++){
  61.             //pull out a jackson
  62.             String jackson = jacksonFive[i];
  63.             System.out.println("Jackson: " + jackson);
  64.         }
  65.  
  66.        
  67. ^So that is the old way of doing it, its not-so-good pratice now in JAVA although other programming languages still use it.
  68. >Now we use... *drum roll please*
  69.  
  70. For-Each loop aka "for-in" loop: cleaner, faster.
  71.  
  72.  
  73.         String[] jacksonFive = {"Michael", "Jackie", "Tito", "Jermaine", "Marlon", "Randy"};
  74.         for (String jackson : jacksonFive){
  75.             //pull out a jackson
  76.             System.out.println("Jackson: " + jackson);
  77.         }
  78.        
  79. // When you see the colon (:), read it as β€œin.” Thus, the loop above reads as β€œfor each String jackson in jacksonFive.”
  80.  
  81.  
  82.  
  83.  
  84.  
  85. Practice complex loop examples.
  86.  
  87. Jim has three tries (ball throws) to get Craig in the dunk tank.
  88.  
  89. //While Loop method:
  90.  
  91.         Random random = new Random();
  92.         boolean dunkedIn = false;
  93.         int timesBallThrown = 0;
  94.        
  95.         while (!dunkedIn && timesBallThrown < 3 ) {
  96.             timesBallThrown++;
  97.             dunkedIn = random.nextBoolean();
  98.             System.out.println("Time ball is thrown: " + timesBallThrown + " times. " + "Is it dunked in? " + dunkedIn );
  99.         }
  100.        
  101.         if(dunkedIn){
  102.             System.out.println("Celebrate Craig falling in Water");
  103.         } else {
  104.             System.out.println("Be sad");
  105.         }
  106.  
  107.  
  108. //For Loop method:
  109.    
  110.         Random random = new Random();
  111.         boolean dunkedIn = false;
  112.        
  113.         for(int timesBallThrown = 1; !dunkedIn && timesBallThrown <= 3; timesBallThrown++) {
  114.             dunkedIn = random.nextBoolean();
  115.             System.out.println("Times thrown: " + timesBallThrown + " times." + "| Is Craig dunked in? " + dunkedIn);
  116.             }
  117.             if(dunkedIn){
  118.                 System.out.println("Celebrate Craig falling in Water");
  119.             } else {
  120.                 System.out.println("Be sad");
  121.             }
  122.  
  123.  
  124.  
  125. Nested loops: Mix IceCream sour and sweet
  126.  
  127. String[] sweetFlavors = {"Caramel", "Cinnamon", "Watermelon", "Baked Beans"};
  128.         String[] savoryFlavors = {"Sea Salt", "Potato Chip", "Carrot", "Barbeque Sauce"};
  129.        
  130.         for(String sweet: sweetFlavors) {
  131.             for (String savory: savoryFlavors){
  132.                 System.out.println(sweet + ", AND " + savory);
  133.             }
  134.         }
  135.  
  136. //Results:
  137. Caramel AND Sea Salt
  138. Caramel AND Potato Chip
  139. Caramel AND Carrot
  140. Caramel AND Barbeque Sauce
  141. Cinnamon AND Sea Salt
  142. Cinnamon AND Potato Chip
  143. Cinnamon AND Carrot
  144. Cinnamon AND Barbeque Sauce
  145. Watermelon AND Sea Salt
  146. Watermelon AND Potato Chip
  147. Watermelon AND Carrot
  148. Watermelon AND Barbeque Sauce
  149. Baked Beans AND Sea Salt
  150. Baked Beans AND Potato Chip
  151. Baked Beans AND Carrot
  152. Baked Beans AND Barbeque Sauce
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. // Branching statements
  161. // break - exits the iteration
  162. // continue - stops executing the current iteration and jumps back to the top to start over again
  163. // return - if you are inside of the loop, it will immediately exit the method.
Add Comment
Please, Sign In to add comment