Java Reference
In-Depth Information
3. Click the OK button in the Input dialog box.
4. If necessary, click the Close button in the command prompt window
title bar.
The Commission program terminates and the command prompt window
closes.
OTHER WAYS
1. To compile, press
CTRL + 1
2. To compile at
command prompt,
type javac
Commission.java
3. To run, press CTRL + 2
4. To run at command
prompt, type java
Commission
When the program runs without any errors, line 33 converts the value
entered by the user to a data type of double. No other validation of the data
takes place. As you will see in the next section, however, Java provides many tools
that programmers can use to test or validate data.
The if…else Statement
The function of the selection structure is to state a condition that allows a
program to choose whether to execute one or more lines of code. The selection
structure used by Java, also called the if...else statement , is used to perform
selection or make a decision on whether to execute a particular piece of code
based on the evaluation of a condition. The general form of the if...else state-
ment is shown in Table 4-4 on the next page. The if…else logic also is described
in detail in Appendix A on page APP 8.
As noted in Table 4-4, the if statement is followed by a condition in paren-
theses, which is followed by a clause or clauses. A condition is a boolean expres-
sion that evaluates to true or false. If the condition in an if statement is true, Java
acts on the clause or clauses that follow the if statement. A single-line if
statement , as shown in example 1 in Table 4-4, is used to perform a single task
when the condition in the statement is true. A block if statement , as shown in
example 2, is used to execute more than one command if the condition in the
statement is true. Example 3 displays a block if…else specifying that, if the con-
dition in the statement is true, all of the commands in the if clause are executed.
If the condition is false, Java acts on the else clause. In either case, after executing
the statements in the if clause or else clause, control passes to the statement fol-
lowing the entire if…else statement.
In example 3, if the condition is true, Java takes a path to execute one set of
code; if it is false, Java takes a path to execute another set of code. These two
paths do not have to have the same number of lines of code; the false condition
may result in no action being performed, while the true condition might have
many lines of code that execute. The only restriction is that the two paths must
come back together after the selection structure, in order to continue processing.
double vs. Double
Java requires a lowercase d in double when referring to the
primitive data type, as programmers do when they declare
variables. An uppercase D is required when referring to its
wrapper class the - Double class of objects - and its associated
methods such as the Double.parseDouble() method. The same
is true for other primitive data types and their wrapper classes,
such as int and Integer and float and Float.
Search WWH ::




Custom Search