1 / 32

Worked Out Exercises

Chapter 5 Slides. Worked Out Exercises. Worked Out Exercises. These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter.

wyatt
Download Presentation

Worked Out Exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Slides Worked Out Exercises

  2. Worked Out Exercises 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, develop some methods to determine program output correctly, which involves control structures. You can expect that on quizzes, tests and the APCS Examination only a program segment or a method is shown.

  3. public class Ex0501 { public static void main (String args[]) { for (int x = 1; x < 8; x++) System.out.println("x = " + x); } }

  4. public class Ex0501 { public static void main (String args[]) { for (int x = 1; x < 8; x++) System.out.println("x = " + x); } }

  5. public class Ex0502 { public static void main (String args[]) { for (int x = 1; x <= 8; x++) System.out.println("x = " + x); } }

  6. public class Ex0502 { public static void main (String args[]) { for (int x = 1; x <= 8; x++) System.out.println("x = " + x); } }

  7. public class Ex0503 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); } }

  8. public class Ex0503 { public static void main (String args[]) { for (int x = 0; x <= 8; x+=2) System.out.println("x = " + x); } }

  9. public class Ex0504 { public static void main (String args[]) { int x = 0; int y = 0; for (y = 1; y <= 25; y++) { y+=5; System.out.println(y); } } }

  10. public class Ex0504 { public static void main (String args[]) { int x = 0; int y = 0; for (y = 1; y <= 25; y++) { y+=5; System.out.println(y); } } }

  11. public class Ex0505 { public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); } }

  12. public class Ex0505 { public static void main (String args[]) { int x = 0; int y = 0; for (x = 1; x > 1; x--) y++; System.out.println("y = " + y); } }

  13. public class Ex0506 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 5) { y++; x = y; } System.out.println("y = " + y); } }

  14. public class Ex0506 { public static void main (String args[]) { int x = 0; int y = 0; while (x < 5) { y++; x = y; } System.out.println("y = " + y); } }

  15. public class Ex0507 { 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); } }

  16. public class Ex0507 { 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); } }

  17. public class Ex0508 { 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); } }

  18. public class Ex0508 { 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); } }

  19. public class Ex0509 { public static void main (String args[]) { int x = 2; while (x < 10) { if (x % 2 == 0) x+=2; else x++; } System.out.println("x = " + x); } }

  20. public class Ex0509 { public static void main (String args[]) { int x = 2; while (x < 10) { if (x % 2 == 0) x+=2; else x++; } System.out.println("x = " + x); } }

  21. public class Ex0510 { 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); } }

  22. public class Ex0510 { 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); } }

  23. public class Ex0511 { public static void main (String args[]) { int x = 10; int y = 20; do { x = y + 2; y = x - 2; } while (x < y); System.out.println("x = " + x); } }

  24. public class Ex0511 { public static void main (String args[]) { int x = 10; int y = 20; do { x = y + 2; y = x - 2; } while (x < y); System.out.println("x = " + x); } }

  25. public class Ex0512 { 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); } }

  26. public class Ex0512 { 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); } }

  27. public class Ex0513 { 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); } }

  28. public class Ex0513 { 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); } }

  29. public class Ex0514 { public static void main (String args[]) { int x = 1; int y = 2; int z = 3; for (int k = 1; k <= 10; k++) { x = y + z; y = x + z; z = x - y; } System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("z = " + z); } }

  30. public class Ex0514 { public static void main (String args[]) { int x = 1; int y = 2; int z = 3; for (int k = 1; k <= 10; k++) { 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 Ex0515 { 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); } }

  32. public class Ex0515 { 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); } }

More Related