04-StatementForms - TheArtandScienceof CHAPTER4...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Statement Forms C H A P T E R   4 The statements was interesting but tough. —Mark Twain, The Adventures of Huckleberry Finn, 1884 4.1 Statement types in Java 4.2 Control statements and problem solving 4.3 The if statement 4.4 The switch statement 4.5 The while statement 4.6 The for statement
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Statement Types in Java Programs in Java consist of a set of classes . Those classes contain methods , and each of those methods consists of a sequence of statements . Statements in Java fall into three basic types: Simple statements Compound statements Control statements S imple statements are formed by adding a semicolon to the end of a Java expression. C ompound statements (also called blocks ) consist of a sequence of statements enclosed in curly braces. Control statements fall into two categories: Conditional statements that specify some kind of test Iterative statements that specify repetition
Image of page 2
Control Statements and Problem Solving Before looking at the individual control statement forms in detail, it helps to look more holistically at a couple of programs that make use of common control patterns. The next few slides extend the Add2Integers program from Chapter 2 to create programs that add longer lists of integers. These slides illustrate three different strategies: Adding new code to process each input value Repeating the input cycle a predetermined number of times Repeating the input cycle until the user enters a special sentinel value
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Add4Integers Program public class Add4Integers extends ConsoleProgram { public void run() { println("This program adds four numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int n3 = readInt("Enter n3: "); int n4 = readInt("Enter n4: "); int total = n1 + n2 + n3 + n4; println("The total is " + total + "."); } } If you don’t have access to control statements, the only way you can increase the number of input values is to add a new statement for each one, as in the following example: This strategy, however, is difficult to generalize and would clearly be cumbersome if you needed to add 100 values.
Image of page 4
The Repeat-N-Times Idiom One strategy for generalizing the addition program is to use the Repeat-N-Times idiom, which executes a set of statements a specified number of times. The general form of the idiom is for (int i = 0; i < repetitions ; i++) { statements to be repeated } As is true for all idiomatic patterns in this book, the italicized words indicate the parts of the pattern you need to change for each application. To use this pattern, for example, you need to replace repetitions with an expression giving the number of repetitions and include the statements to be repeated inside the curly braces.
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern