/* * ============================================================= * DemoBranching.java: Demo features of branching constructs.... * * Written By: Mark Austin August 2004 * ============================================================= */ public class DemoBranching { // Constructor method. public DemoBranching() {}; // main method : this is where the program execution begins. public static void main ( String [] args ) { int i = 0, j = 0, k = 3; // [a] Create a simple branching construct System.out.println("Demo 1: Simple branching construct"); System.out.println("=============================================="); System.out.println("Part 1: i = " + i); if (i == 0) { System.out.println("In block 1!"); } else { System.out.println("In block 2!"); } System.out.println(" (i == 0) evaluates to " + (i==0) ); i = 3; System.out.println("\nPart 2: i = " + i); if (i == 0) { System.out.println("In block 1!"); } else { System.out.println("In block 2!"); } System.out.println(" (i == 0) evaluates to " + (i==0) ); System.out.println("=============================================="); // [b] Branching construct with more complicated conditional // expression.... System.out.println("\nDemo 2: Branching construct with multiple args"); System.out.println("=============================================="); i = 0; j = 1; k = 3; System.out.println("Part 1: i = " + i + " j = " + j + " k = " + k); if (i == 0 && j <= 1 && (k == 3 || k == 4) ) { System.out.println("In block 1!"); } else { System.out.println("In block 2!"); } System.out.println(" (i == 0) evaluates to " + (i==0) ); System.out.println(" (j <= 4) evaluates to " + (j<=4) ); System.out.println(" (k == 3 || k == 4) evaluates to " + (k == 3 || k == 4)); i = 3; k = 5; System.out.println("\nPart 2: i = " + i + " j = " + j + " k = " + k); if (i == 0 && j <= 1 && (k == 3 || k == 4) ) { System.out.println("In block 1!"); } else { System.out.println("In block 2!"); } System.out.println(" (i == 0) evaluates to " + (i==0) ); System.out.println(" (j <= 4) evaluates to " + (j<=4) ); System.out.println(" (k == 3 || k == 4) evaluates to " + (k == 3 || k == 4)); System.out.println("=============================================="); } }