Chapter 3 Programming in the Small II: Control

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The positions within a switch statement to which it can jump are marked with case labels that take the form:

"case <constant>:"

you'll never see a while loop standing by itself in a real program.

It will always be inside a subroutine which is itself defined inside some class.

The <exception-class-name> could be

NumberFormatException, IllegalArgumentException, or some other exception class.

The Basic If Statement

The Basic If Statement

Body of the loop

The body of the loop is repeated as long as the <boolean-expression> is true.

Java is what is called a free-format language

There are no syntax rules about how the language has to be arranged on a page

An empty block

a block that contains no statements at all

The <constant> here is

a literal of the same type as the expression in the switch.

A program is

an expression of an idea.

Arrays

are a basic and very commonly used data structure, and array processing is often an exercise in using control structures.

A do..while statement has the form

do { <statements> } while (<boolean-expression>);

An array

is a data structure in which the items are arranged as a numbered sequence, so that each individual item can be referred to by its position number. In Java all the items must be of the same type, and the numbering always starts at zero.

The type of the individual items in an array is called

the base type of the array.

A while loop has the form

while (<boolean-expression>) <statement>

Structured statement

A statement that contain one or more other statements inside itself.

Arrays are created with an operator named new.

Here are some examples: namelist = new String[1000]; A = new int[5]; prices = new double[100]; The general syntax is <array-variable> = new <base-type> [<array-length>];

Like all variables, an array variable has to be assigned a value before it can be used.

In this case, the value is an array.

The Basic While Loop

The Basic While Loop

Only expressions of certain types can be used.

The value of the expression can be one of the primitive integer types int, short, or byte. It can be the primitive char type. It can be String. Or it can be an enum type. In particular, note that the expression cannot be a 'double' or 'float' value.

the empty statement

This is a statement that consists simply of a semicolon and which tells the computer to do nothing.

When an exception is thrown, it is possible to "catch" the exception and prevent it from crashing the program.

This is done with a try..catch statement.

if <statement-1> is an if statement that has no else part.

This special case is effectively forbidden by the syntax of Java.

When the computer executes the variable declaration statement, it allocates memory to hold the value of the variable.

When the block ends, that memory is discarded (that is, made available for reuse). The variable is said to be 'local' to the block.

The semantics of the while statement go like this:

When the computer comes to a 'while' statement, it evaluates the <boolean-expression>, which yields either true or false as its value. If the value is false, the computer skips over the rest of the while loop and proceeds to the next command in the program. If the value of the expression is true, the computer executes the <statement> or block of <statements> inside the loop. Then it returns to the beginning of the while loop and repeats the process. That is, it re-evaluates the <boolean-expression>, ends the loop if the value is false, and continues it if the value is true. This will continue over and over until the value of the expression is false when the computer evaluates it; if that never happens, then there will be an infinite loop.

You should think of this as a single statement representing a three-way branch.

When the computer executes this, one and only one of the three statements—<statement-1>, <statement-2> i, or <statement-3> — will be executed.

The five remaining control structures (except blocks)

affect the flow control in a program

The two types of control structures, loops and branches,

can be used to repeat a sequence of statements over and over or to choose among two or more possible courses of action.

Much more interesting than this technicality is the case where <statement-2> i, the else part of the if statement, is itself an if statement.

if ( <boolean-expression-1> ) <statement-1> else if ( <boolean-expression-2> ) <statement-2> else <statement-3> this is almost always written in the format: if ( <boolean-expression-1> ) <statement-1> else if ( <boolean-expression-2> ) <statement-2> else <statement-3>

An if statement has the form:

if ( <boolean-expression> ) <statement1> else <statement2> The two statements represent alternative courses of action; the computer decides between these courses of action based on the value of the boolean expression.

The if Statement format

if (<boolean-expression>) <statement-1> else <statement-2>

Pseudocode

informal instructions that imitate the structure of programming languages without the complete detail and perfect syntax of actual program code.

A data structure

is an organized collection of data, chunked together so that it can be treated as a unit.

the <boolean-expression>

is called the continuation condition, or more simply the test , of the loop.

A variable declared inside a block

is completely inaccessible and invisible from outside that block.

Its purpose

is simply to group a sequence of statements into a single statement.

The scope of an identifier

is the part of the program in which that identifier is valid.

3.6 The switch Statement

is the second branching statement in Java. The switch statement is used far less often than the if statement, but it is sometimes useful for expressing a certain type of multi-way branch.

The block

is the simplest type of structured statement. The block statement by itself really doesn't affect the flow of control in a program.

The term exception

is used to refer to the type of error that one might want to handle with a try..catch. An exception is an exception to the normal flow of control in the program. The term is used in preference to "error" because in some cases, an exception might not be considered to be an error at all.

A while loop

is used to repeat a given statement over and over, but only so long as a specified condition remains true.

The do..while statement

is very similar to the while statement, except that the word "while," along with the condition that it tests, has been moved to the end. The word "do" is added to mark the beginning of the loop.

They can be divided into two classes:

loop statements and branching statements.

A switch statement, as it is most often used, has the form:

switch (<expression>) { case <constant-1>: <statements-1> break; case <constant-2>: <statements-2> break; . . // (more cases) . case <constant-N>: <statements-N> break; default: // optional default case <statements-(N+1)> } // end of switch statement

An if statement

tells the computer to take one of two alternative courses of action, depending on whether the value of a given boolean-valued expression is true or false. It is an example of a "branching" or "decision" statement.

One of the big advantages of arrays is

that they allow random access.

In Java, there are just six such structures that are used to determine the normal flow of control in a program

the 'block', the 'while loop', the 'do..while loop', the 'for loop', the 'if statement' , and the switch statement .

And the position number of an item in an array is called

the index of that item.

The number of items in an array is called

the length of the array.

A case label marks

the position the computer jumps to when the expression evaluates to the given <constant> value.

A switch statement allows you

to test the value of an expression and, depending on that value, to jump directly to some location within the switch statement.

As the final case in a switch statement you can, optionally,

use the label "default:", which provides a default jump point that is used when the value of the expression is not listed in any case label.

Before you can use a variable to refer to an array,

variable must be declared, and it must have a type. For an array of Strings, for example, the type for the array variable would be String[ ], and for an array of ints, it would be int[ ].

For example,

we say that Integer.parseInt(str) throws an exception of type NumberFormatException when the value of str is illegal.

When an exception occurs,

we say that the exception is "thrown".

A NumberFormatException can occur

when an attempt is made to convert a string into a number.

An IllegalArgumentException can occur

when an illegal value is passed as a parameter to a subroutine.

A while loop has the form:

while (<boolean-expression>) <statement>

Since the statement can be, and usually is, a block, most while loops have the form:

while (<boolean-expression>) { <statements> }

In order to have a completely general-purpose programming language

you really just need one control structure from each category

The format of a block is:

{ <statements> }


Set pelajaran terkait

Quiz 1 for Organization of Programming Languages

View Set

Strategy and Human Resources Planning Ch. 2

View Set

Biology Unit 1 Part 2 Test Study Guide

View Set

Social/Cultural Diversity (Purple Book)

View Set

Boaters Safety - Practice Exam cards

View Set