Download presentation
Presentation is loading. Please wait.
Published byJoella Marshall Modified over 9 years ago
2
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.
3
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.
4
public class Output0501 { public static void main (String args[]) { for (int x = 1; x < 8; x++) System.out.println("x = " + x); }
5
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
6
public class Output0502 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); }
7
public class Output0502 { 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
8
public class Output0503 { public static void main (String args[]) { for (int x = 100; x > 0; x-=20) System.out.println("x = " + x); }
9
public class Output0503 { public static void main (String args[]) { for (int x = 100; x > 0; x-=20) System.out.println("x = " + x); } xOutput 100x = 100 80x = 80 60x = 60 40x = 40 20x = 20
10
public class Output0504 { public static void main (String args[]) { for (int x = 1; x < 100; x*=2) System.out.println("x = " + x); }
11
public class Output0504 { 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
12
public class Output0505 { public static void main (String args[]) { for (int x = 729; x >= 1; x/=3) System.out.println("x = " + x); }
13
public class Output0505 { public static void main (String args[]) { for (int x = 729; x >= 1; x/=3) System.out.println("x = " + x); } xOutput 729x = 729 243x = 243 81x = 81 27x = 27 9x = 9 3x = 3 1x = 1
14
public class Output0506 { public static void main (String args[]) { int x = 100; if (x == 100) System.out.println("Hello"); }
15
public class Output0506 { public static void main (String args[]) { int x = 100; if (x == 100) System.out.println("Hello"); } xx == 100?Output 100trueHello
16
public class Output0507 { public static void main (String args[]) { int x = 100; if (x > 100) System.out.println("Hello"); else System.out.println("Goodbye"); }
17
public class Output0507 { 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
18
public class Output0508 { public static void main (String args[]) { int x = 100; if (x >= 100) System.out.println("Hello"); else System.out.println("Goodbye"); }
19
public class Output0508 { 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
20
public class Output0509 { public static void main (String args[]) { int x = 0; int y = 0; for (y = 1; y <= 25; y++) { y+=5; System.out.println(y); }
21
public class Output0509 { public static void main (String args[]) { int x = 0; int y = 0; for (y = 1; y <= 25; y++) { y+=5; System.out.println(y); } xyOutput 00 01 066 07 012 013 018 019 024 025 030
22
public class Output0510 { public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); }
23
public class Output0510 { public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); } xyOutput 00y = 0
24
public class Output0511 { 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); }
25
public class Output0511 { 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 00 25 710y = 7
26
public class Output0512 { public static void main (String args[]) { int x = 2; do { if (x % 2 == 0) x+=2; else x++; } while (x < 10); System.out.println("x = " + x); }
27
public class Output0512 { public static void main (String args[]) { int x = 2; do { if (x % 2 == 0) x+=2; else x++; } while (x < 10); System.out.println("x = " + x); } xx % 2 == 0Output 2true 4 6 8 10truex = 10
28
public class Output0513 { public static void main (String args[]) { int x = 10; int y = 1; do { if (x % 2 == 0) x += 5; else y += 2; } while (y < x); System.out.println("x = " + x); }
29
public class Output0513 { public static void main (String args[]) { int x = 10; int y = 1; do { if (x % 2 == 0) x += 5; else y += 2; } while (y < x); System.out.println("x = " + x); } x % 2 == 0xyOutput 101 true15 false3 5 7 9 11 false13 false15x = 15
30
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); }
31
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 135 813-5x = 8 y = 13 z = -5
32
public class Output0515 { public static void main (String args[]) { int x = 168; int y = 90; int z = 0; do { z = x % y; if (z == 0) System.out.println("y = " + y); else { x = y; y = z; } while (z != 0); }
33
public class Output0515 { public static void main (String args[]) { int x = 168; int y = 90; int z = 0; do { z = x % y; if (z == 0) System.out.println("y = " + y); else { x = y; y = z; } while (z != 0); } xyzOutput 168900 78 907812 78126 60y = 6
34
public class Output0516 { public static void main(String args[]) { for (int x = 1; x < 3; x++) { for (int y = 1; y < 4; y++) System.out.print(x*y + " "); System.out.println(); }
35
public class Output0516 { public static void main(String args[]) { for (int x = 1; x < 3; x++) { for (int y = 1; y < 4; y++) System.out.print(x*y + " "); System.out.println(); } xyOutput 111 122 133 CRLF 212 224 236
36
public class Output0517 { public static void main(String args[]) { int x = 5; int k,y; while (x <= 8) { y = 1; while (y <= 7) { y++; k = x + y; } x += y; System.out.println("x = " + x); }
37
public class Output0517 { public static void main(String args[]) { int x = 5; int k,y; while (x <= 8) { y = 1; while (y <= 7) { y++; k = x + y; } x += y; System.out.println("x = " + x); } ykxOutput 5 1 27 38 49 510 611 712 81313 x = 13
38
An OBOB is an Off By One Bug error. This problem occurs at the boundaries of control structures. Carefully check your program code at the boundaries to avoid OBOBs. OBOB Warning!
39
public class Output0518 { public static void main(String args[]) { int a = 1, b = 2, c = 3, d = 4; while (a < b && c < d) { a = c + d; c = a + b; } System.out.println(a + b + c + d); }
40
public class Output0518 { public static void main(String args[]) { int a = 1, b = 2, c = 3, d = 4; while (a < b && c < d) { a = c + d; c = a + b; } System.out.println(a + b + c + d); } abcdOutput 1234 79 22
41
public class Output0519 { public static void main(String args[]) { int a; int b = 2; for (int k = 0; k < 4; k++) { a = k; b += k; while (a < b) { System.out.println(a + " " + b); a += b; b += k; }
42
public class Output0519 { public static void main(String args[]) { int a; int b = 2; for (int k = 0; k < 4; k++) { a = k; b += k; while (a < b) { System.out.println(a + " " + b); a += b; b += k; } kabOutput 2 0020 22 1131 3 44 2262 68 33113 1114
43
public class Output0520 { public static void main(String args[]) { int a = 0; int b = 10; for (int k = a; k < b; k++) { a++; b--; System.out.println(a + " " + b + " " + k); }
44
public class Output0520 { public static void main(String args[]) { int a = 0; int b = 10; for (int k = a; k < b; k++) { a++; b--; System.out.println(a + " " + b + " " + k); } kabOutput 010 0191 9 0 1282 8 1 2373 7 2 3464 7 3 4555 5 4 5
45
public class Output0521 { public static void main(String args[]) { int t = 0; for (int p = 1; p <= 4; p++) for (int q = 1; q <= 4; q++) t++; System.out.println("t = " + t); }
46
public class Output0521 { public static void main(String args[]) { int t = 0; for (int p = 1; p <= 4; p++) for (int q = 1; q <= 4; q++) t++; System.out.println("t = " + t); } pqtOutput 0 111234 215 26 37 48 319 210 311 412 4113 214 315 416 t = 16
47
public class Output0521 { public static void main(String args[]) { int t = 0; for (int p = 1; p <= 4; p++) for (int q = 1; q <= 4; q++) t++; System.out.println("t = " + t); } Shortcut This question essentially boils down to determining how many times the t++; statement is executed. The outer loop repeats 4 times. The inner loop repeats 4 times. 4 * 4 = 16
48
public class Output0522 { public static void main(String args[]) { int n1, n2, n3; n1 = n2 = n3 = 1; for (int k = 3; k <= 10; k++) { n3 = n1 + n2; n1 = n2; n2 = n3; } System.out.println("n3 = " + n3); }
49
public class Output0522 { public static void main(String args[]) { int n1, n2, n3; n1 = n2 = n3 = 1; for (int k = 3; k <= 10; k++) { n3 = n1 + n2; n1 = n2; n2 = n3; } System.out.println("n3 = " + n3); } kn3n1n2Output 111 3212 4323 5535 6858 713813 8211321 9342134 10553455 n3 = 55
50
public class Output0523 { public static void main(String args[]) { int a = 5; int b = 10; int c = 13; while (a < b && b != c) { a += 2; b++; if ((a + b) % 2 == 0) c++; } System.out.println(a + " " + b + " " + c); }
51
public class Output0523 { public static void main(String args[]) { int a = 5; int b = 10; int c = 13; while (a < b && b != c) { a += 2; b++; if ((a + b) % 2 == 0) c++; } System.out.println(a + " " + b + " " + c); } aba+ba+b evenca < bb != c Output 51015false13truetrue 71118true14truetrue 91221falsetruetrue 111324true15truetrue 131427falsetruetrue 151530true16falsetrue 15 15 16
52
// THE MYSTERY PROGRAM import java.util.Scanner; public class Output0524 { public static void main (String args[]) { Scanner input = new Scanner(System.in); int a, b, c; System.out.print("Enter integer 1 ===>> "); a = input.nextInt(); System.out.print("Enter integer 2 ===>> "); b = input.nextInt(); if (a < b) if (b < a) c = 1000; else c = 2500; else if (b > a) c = 2000; else c = 2500; System.out.println("c = " + c); }
53
// THE MYSTERY PROGRAM import java.util.Scanner; public class Output0524 { public static void main (String args[]) { Scanner input = new Scanner(System.in); int a, b, c; System.out.print("Enter integer 1 ===>> "); a = input.nextInt(); System.out.print("Enter integer 2 ===>> "); b = input.nextInt(); if (a < b) if (b < a) c = 1000; else c = 2500; else if (b > a) c = 2000; else c = 2500; System.out.println("c = " + c); } abc Output ??2500c = 2500
54
public class Output0525 { public static void main(String args[]) { for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 4; y++) System.out.print((x + y) + " "); System.out.println(); }
55
public class Output0525 { public static void main(String args[]) { for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 4; y++) System.out.print((x + y) + " "); System.out.println(); } xyx + y 112 123 134 145 213 224 235 246 314 325 336 347 415 426 437 448
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.