Link

Introduction to control-flow (if/else, switch, for, while and do/while loops)

Table of contents

  1. If-Else
  2. Switch
  3. For loop

If-Else

🚧 Pending 🚧
package demo;

import java.util.ArrayList;
import java.util.List;

public class App {
  public static void main( final String[] args ) {
    List<String> names = new ArrayList<>();
    names.add( "Jade" );
    names.add( "Aden" );

    if ( names.size() == 0 ) {
      System.out.println( "List is empty" );
    } else if ( names.size() == 1 ) {
      System.out.println( "List has one element" );
    } else {
      System.out.println( "List has many elements" );
    }
  }
}
List has many elements

Switch

package demo;

import java.util.ArrayList;
import java.util.List;

public class App {
  public static void main( final String[] args ) {
    List<String> names = new ArrayList<>();
    names.add( "Jade" );
    names.add( "Aden" );

    switch ( names.size() ) {
      case 0:
        System.out.println( "List is empty" );
      case 1:
        System.out.println( "List has one element" );
      default:
        System.out.println( "List has many elements" );
    }
  }
}
List has many elements

For loop

package demo;

import java.util.ArrayList;
import java.util.List;

public class App {
  public static void main( final String[] args ) {
    List<String> names = new ArrayList<>();
    names.add( "Jade" );
    names.add( "Aden" );

    System.out.println( "Names in the list" );
    for ( int i = 0; i < names.size(); i++ ) {
      System.out.printf( "[%d] %s%n", i, names.get( i ) );
    }
  }
}
Names in the list
[0] Jade
[1] Aden
package demo;

public class App {
  public static void main( final String[] args ) {
    String[] names = { "Jade", "Aden" };

    System.out.println( "Names in array" );
    for ( int i = 0; i < names.length; i++ ) {
      System.out.printf( "[%d] %s%n", i, names[i] );
    }
  }
}
Names in array
[0] Jade
[1] Aden