Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern will be with the output of each program, and more importantly, to develop a way to determine program output correctly for programs that involve control structures. You can expect that on quizzes and/or tests only a program segment or a method is shown.
Teacher/Student Versions, Tablet PCs, and Inking The “For Teachers” version of this presentation has 2 slides for each program. The first slide only shows the program. The second shows the program, worked out solution, and output. The “For Students” version only has 1 slide for each program with no provided solution or output. Students are expected to work out the solutions either on paper, or ideally they can “ink” directly on their laptops.
public class Output0501 { public static void main (String args[]) { for (int x = 1; x < 8; x++) System.out.println("x = " + x); }
public class Output0501 { public static void main (String args[]) { for (int x = 1; x < 8; x++) System.out.println("x = " + x); } xOutput 1x = 1 2x = 2 3x = 3 4x = 4 5x = 5 6x = 6 7x = 7
public class Output0502 { public static void main (String args[]) { for (int x = 1; x <= 8; x++) System.out.println("x = " + x); }
public class Output0502 { public static void main (String args[]) { for (int x = 1; x <= 8; x++) System.out.println("x = " + x); } xOutput 1x = 1 2x = 2 3x = 3 4x = 4 5x = 5 6x = 6 7x = 7 8x = 8
public class Output0503 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); }
public class Output0503 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); } xOutput 0x = 0 2x = 2 4x = 4 6x = 6 8x = 8
public class Output0504 { public static void main (String args[]) { for (int x = 100; x > 0; x-=20) System.out.println("x = " + x); }
public class Output0504 { public static void main (String args[]) { for (int x = 100; x > 0; x-=20) System.out.println("x = " + x); } xOutput 100x = x = 80 60x = 60 40x = 40 20x = 20
public class Output0505 { public static void main (String args[]) { for (int x = 1; x < 100; x*=2) System.out.println("x = " + x); }
public class Output0505 { public static void main (String args[]) { for (int x = 1; x < 100; x*=2) System.out.println("x = " + x); } xOutput 1x = 1 2x = 2 4x = 4 8x = 8 16x = 16 32x = 32 64x = 64
public class Output0506 { public static void main (String args[]) { for (int x = 729; x >= 1; x/=3) System.out.println("x = " + x); }
public class Output0506 { public static void main (String args[]) { for (int x = 729; x >= 1; x/=3) System.out.println("x = " + x); } xOutput 729x = x = x = 81 27x = 27 9x = 9 3x = 3 1x = 1
public class Output0507 { public static void main (String args[]) { int x = 100; if (x == 100) System.out.println("Hello"); }
public class Output0507 { public static void main (String args[]) { int x = 100; if (x == 100) System.out.println("Hello"); } xx == 100?Output 100trueHello
public class Output0508 { public static void main (String args[]) { int x = 99; if (x == 100) System.out.println("Hello"); }
public class Output0508 { public static void main (String args[]) { int x = 99; if (x == 100) System.out.println("Hello"); } xx == 100?Output 99false[none]
public class Output0509 { public static void main (String args[]) { int x = 101; if (x > 100) System.out.println("Hello"); else System.out.println("Goodbye"); }
public class Output0509 { public static void main (String args[]) { int x = 101; if (x > 100) System.out.println("Hello"); else System.out.println("Goodbye"); } xx > 100?Output 101trueHello
public class Output0510 { public static void main (String args[]) { int x = 100; if (x > 100) System.out.println("Hello"); else System.out.println("Goodbye"); }
public class Output0510 { public static void main (String args[]) { int x = 100; if (x > 100) System.out.println("Hello"); else System.out.println("Goodbye"); } xx > 100?Output 100falseGoodbye
public class Output0511 { public static void main (String args[]) { int x = 100; if (x >= 100) System.out.println("Hello"); else System.out.println("Goodbye"); }
public class Output0511 { public static void main (String args[]) { int x = 100; if (x >= 100) System.out.println("Hello"); else System.out.println("Goodbye"); } xx >= 100?Output 100trueHello
public class Output0512 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x + 2; x = y + 3; } System.out.println("y = " + y); }
public class Output0512 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x + 2; x = y + 3; } System.out.println("y = " + y); } yxOutput y = 7
public class Output0513 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x * 2; x++; } System.out.println("x = " + x); System.out.println("y = " + y); }
public class Output0513 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 10) { y = x * 2; x++; } System.out.println("x = " + x); System.out.println("y = " + y); } yxOutput x = 10 y = 18
public class Output0514 { public static void main (String args[]) { int x = 1; int y = 3; int z = 5; while (z > x + y) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); }
public class Output0514 { public static void main (String args[]) { int x = 1; int y = 3; int z = 5; while (z > x + y) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); } xyzOutput x = 8 y = 13 z = -5
public class Output0515 { public static void main (String args[]) { while (2 + 2 == 4) { System.out.print("EXPOJAVA"); }
public class Output0515 { public static void main (String args[]) { while (2 + 2 == 4) { System.out.print("EXPOJAVA"); }