04-StatementForms - The Art and Science of An Introduction...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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 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 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. 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....
View Full Document

This note was uploaded on 10/27/2009 for the course COMP SCI 1MD3 taught by Professor Various during the Winter '07 term at McMaster University.

Page1 / 35

04-StatementForms - The Art and Science of An Introduction...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online