/* * File: Commands.java * Author: Stephen Gilmore * Copyright: School of Computer Science, The University of Edinburgh * * Commands.java,v 1.2 2002/10/24 14:00:43 da Exp */ public class Commands { static void conditionals() { int x = 0; System.out.println("\nConditionals"); if (x == 0) System.out.println ("zero"); // printed if (x != 0) System.out.println ("non-zero"); // not printed if (x == 0) System.out.println ("zero"); // printed else System.out.println ("non-zero"); // not printed } static void switches() { int x = 0; char c = 'o'; System.out.println("\nSwitches"); switch (x) { case 0: System.out.println ("zero"); // printed default: System.out.println ("non-zero"); // printed }; switch (c) { case 'a': System.out.println ("vowel"); // not printed case 'e': System.out.println ("vowel"); // not printed case 'i': System.out.println ("vowel"); // not printed case 'o': System.out.println ("vowel"); // printed case 'u': System.out.println ("vowel"); // printed default: System.out.println ("consonant"); // printed } } static void switchesWithBreaks() { int x = 0; char c = 'o'; System.out.println("\nSwitches with breaks"); switch (x) { case 0: System.out.println ("zero"); // printed break; default: System.out.println ("non-zero"); // not printed break; }; switch (c) { case 'a': System.out.println ("vowel"); // not printed break; case 'e': System.out.println ("vowel"); // not printed break; case 'i': System.out.println ("vowel"); // not printed break; case 'o': System.out.println ("vowel"); // printed break; case 'u': System.out.println ("vowel"); // not printed break; default: System.out.println ("consonant"); // not printed break; } } static void iteration() { int x = 3; System.out.println("\nIteration"); while (x > 0) { System.out.println("decreasing"); x--; } System.out.println("finished"); } public static void main(String[] args) { conditionals(); switches(); switchesWithBreaks(); iteration(); } }