3.2. Loops

PHOTO EMBED

Thu Sep 15 2022 02:00:07 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

public static void main(String [] args) {
   for (int i = 0; i < 10; i++ ) {
      System.out.println(i);
   }
}
//output 0-9
for (start clause; stop clause; step clause) {
   statement1
   statement2
   ...
}
   //If you want to start at 100, stop at 0 and count backward by 5, the loop is written as:
   for (int i = 100; i >= 0; i -= 5) {
   System.out.println(i);
}
///for each-loop----Java also provides a syntax to iterate over any sequence or collection, such as an Array:
  public static void main(String [] args) {
   int nums[] = {1, 1, 2, 3, 5, 8, 13, 21};

   for (int i : nums) {
      System.out.println(i);
   }
}
  //This loop version also works with a String, where we can convert the String to an Array of characters:
  String msg = "Hello World";

for (char c : msg.toCharArray()) {
   System.out.println(c);
}
  //Java also supports the while loop, or indefinite loop. A while loop in Java:
  public static void main(String [] args) {
   int i = 0;
   while (i < 3) {
      i++;
   }
}
  //Java adds an additional, if seldom used, variation of the while loop called the do-while loop. The do-while loop is very similar to while except that the condition is evaluated at the end of the loop rather than the beginning. This ensures that a loop will be executed at least one time. Some programmers prefer this loop in some situations because it avoids an additional assignment prior to the loop.
  public static void main(String [] args) {
   do {
      System.out.println("Hello, World");
   } while (false);
}
  //There are instances where you may want to terminate a loop if a given condition is met. In these instances, the break statement comes in handy. For example, say you want to loop through an Array of integers to search for a given value. Once that number is found, you want to quit the loop. You can do the following:
  public class testBreak {

   public static void main(String [] args) {
      int[] someInts = {1, 10, 2, 3, 5, 8, 10};
      int searchTerm = 10;
      for (int oneInt : someInts) {
         if (oneInt == searchTerm) {
            System.out.println("Found it!");
            break;
         }
      }
   }
}
//In the code above, instead of the for loop iterating through all the integers in the array, it will stop after it finds the first matching instance. So once it finds the first 10 in the array, it prints “Found it!” and then terminates the loop. If the break statement weren’t there, the loop would continue and when it found the second 10, it would print “Found it!” a second time.


//The continue statement is similar to, but importantly different from, the break statement. Like break, it interrupts the normal flow of control of the loop. But unlike break, the continue statement only terminates the current iteration of the loop. So the loop will continue to run from the top (as long as the boolean expression that controls the loop is still true) after a continue statement. Here is an example:

public class testContinue {

   public static void main(String [] args) {
      int[] someInts = {1, 10, 2, 3, 5, 8, 10};
      int searchTerm = 10;
      for (int oneInt : someInts) {
         if (oneInt == searchTerm) {
            System.out.println("Found it!");
            continue;
         }
         System.out.println("Not here");
      }
   }
}
/*The above program will print “Not here” on every iteration of the for loop except where the number has been found. So the output looks like this:
Not here
Found it!
Not here
Not here
Not here
Not here
Found it!
Because of the continue statement, the final print statement in the for loop is skipped. If the continue statement weren’t there, the output would look like this instead (notice the extra “Not here” printouts):
Not here
Found it!
Not here
Not here
Not here
Not here
Not here
Found it!
Not here
content_copyCOPY

https://education.launchcode.org/java-web-development/chapters/control-flow-and-collections/loops.html