PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   18-10-2021

For loop

p114 You can use normal for-loop when you know how many times you want to loop.
 
/**
 * For loop can be used when you know ...
 * how many times you want to loop.
 * 
 * Those three expressions in the for loop are optional ... 
 * but this form is not recommended.
 */
package com.minte9.basics.loops;

public class ForLoop {            
    public static void main(String[] args) {
        
        for (int i=0; i<10; i++) {
            System.out.println(i); 
                // 0 1 2 .. 9 
        }

        int i=0; for ( ;; ) { // infinite loop
            if (i >= 10) break;
            System.out.println(i++); 
                // 0 1 2 .. 9
        }
    }
}


... 13 lines
 

Enhanced

Allows each you to visit each element without tracking the index.
 
/**
 * With Enhanced For Loop you don't need a loop index
 */
package com.minte9.basics.loops;

public class EnhancedFor {            
    public static void main(String[] args) {
        
        int[] numbers = {1, 2, 3, 4, 5};

        for (int no : numbers) {
            System.out.println(no); // 1 2 .. 5
        }
    }
}

... 5 lines
 

For Each

With Java 8 we can use forEach to loop a Map, List, Set or Stream.
 
/**
 * Starting with Java 8 we can use forEach ...
 * to loop over a Collection
 */

package com.minte9.basics.loops;

import java.util.Arrays;
import java.util.List;

public class ForEach {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3);

        // lambda
        numbers.forEach(x -> System.out.println(x)); // 1, 2, 3

        // method reference
        numbers.forEach(System.out::println); // 1, 2, 3
    }
}

... 9 lines
 

While

A while loop has only the boolean test.
 
/**
 * While loop is going ...
 * as long as a boolean condition is true
 */

package com.minte9.basics.loops;

public class While {
    public static void main(String[] args) {
        
        int i = 0;
        while(i<10) {
            
            System.out.println(i); 
                // 0 1 2 ... 9
            i++;
        }
    }
}

... 8 lines
 

Break

The break statement can be labeled.
 
/**
 * To exit a for loop use break statement.
 * 
 * For multiple loops use labels 
 */

package com.minte9.basics.loops;

public class Break {            
    public static void main(String[] args) {
        
        int[][] numbers = { 
            {1,2,8}, 
            {4,5,6}, 
            {7,8,9} 
        };

        a: for (int i=0; i<numbers.length; i++) {
            b: for (int j=0; j<numbers[i].length; j++) {

                System.out.println("i=" + i + " / j=" + j);
                
                if (numbers[i][j] == 4) {
                    System.out.println("Found 4 at " + i + ":" + j);
                    System.out.println("Break from b loop");
                    break b;
                }

                if (numbers[i][j] == 7) {
                    System.out.println("Found 7 at " + i + ":" + j);
                    System.out.println("Break from a loop");
                    break a;
                }
            }    
        }
    }
}

/*
    i=0 / j=0
    i=0 / j=1
    i=0 / j=2
    i=1 / j=0
    Found 4 at 1:0
    Break from b loop
    i=2 / j=0
    Found 7 at 2:0
    Break from a loop
*/
... 35 lines
 

Questions     github Basics
Numbers