Chapter 2 Self Test
1.
Why does Java strictly specify the range and behavior of its primitive types?
2.
What is Java’s character type, and how does it differ from the character type used by
some other programming languages?
3.
A boolean value can have any value you like because any nonzero value is true.
True or False?
4.
Given this output,
using a single string, show the println( ) statement that produced it.
5.
What is wrong with this fragment?
6.
Explain the difference between the prefix and postfix forms of the increment
operator.
7.
Show how a shortcircuit AND can be used to prevent a dividebyzero error.
8.
In an expression, what type are byte and short promoted to?
9.
In general, when is a cast needed?
10.
Write a program that finds all of the prime numbers between 2 and 100.
11.
Does the use of redundant parentheses affect program performance?
12.
Does a block define a scope?
Chapter 3
Program Control Statements
Key Skills & Concepts
●
Input characters from the keyboard
●
Know the complete form of the if statement
●
Use the switch statement
●
Know the complete form of the for loop
●
Use the while loop
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out
I
●
Use the dowhile loop
●
Use break to exit a loop
●
Use break as a form of goto
●
Apply continue
●
Nest loops
n this chapter, you will learn about the statements that control a program’s flow of
execution. There are three categories of program control statements: selection
statements, which include the if and the switch; iteration statements, which include
the for, while, and dowhile loops; and jump statements, which include break,
continue, and return. Except for return, which is discussed later in this book, the
remaining control statements, including the if and for statements to which you have
already had a brief introduction, are examined in detail here. The chapter begins by
explaining how to perform some simple keyboard input.
INPUT CHARACTERS FROM THE KEYBOARD
Before examining Java’s control statements, we will make a short digression that will
allow you to begin writing interactive programs. Up to this point, the sample programs
in this book have displayed information to the user, but they have not received
information from the user. Thus, you have been using console output, but not console
(keyboard) input. The main reason for this is that Java’s input capabilities rely on or
make use of features not discussed until later in this book. Also, most realworld Java
applications will be graphical and window based, not console based. For these reasons,
not much use of console input is found in this book. However, there is one type of
console input that is relatively easy to use: reading a character from the keyboard. Since
several of the examples in this chapter will make use of this feature, it is discussed here.
To read a character from the keyboard, we will use System.in.read( ). System.in is
the complement to System.out. It is the input object attached to the keyboard. The
read( ) method waits until the user presses a key and then returns the result. The
character is returned as an integer, so it must be cast into a char to assign it to a char
variable. By default, console input is line buffered. Here, the term buffer refers to a
small portion of memory that is used to hold the characters before they are read by your
program. In this case, the buffer holds a complete line of text. As a result, you must
press
ENTER
before any character that you type will be sent to your program. Here is a
program that reads a character from the keyboard:
Here is a sample run:
In the program, notice that main( ) begins like this:
Because System.in.read( ) is being used, the program must specify the throws
java.io.IOException clause. This line is necessary to handle input errors. It is part of
Java’s exception handling mechanism, which is discussed in
Chapter 9
. For now, don’t
worry about its precise meaning.
The fact that System.in is line buffered is a source of annoyance at times. When you
press
ENTER
, a carriage return, line feed sequence is entered into the input stream.
Furthermore, these characters are left pending in the input buffer until you read them.
Thus, for some applications, you may need to remove them (by reading them) before
the next input operation. You will see an example of this later in this chapter.
THE IF STATEMENT
Chapter 1
introduced the if statement. It is examined in detail here. The complete form
of the if statement is
if(condition) statement;
else statement;
where the targets of the if and else are single statements. The else clause is optional.
The targets of both the if and else can be blocks of statements. The general form of the
if, using blocks of statements, is
if( condition)
{
Dostları ilə paylaş: |