System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə16/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   12   13   14   15   16   17   18   19   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

statement sequence
}
else
{
statement sequence
}
If the conditional expression is true, the target of the if will be executed; otherwise, if it
exists, the target of the else will be executed. At no time will both of them be executed.
The conditional expression controlling the if must produce a boolean result.
To demonstrate the if (and several other control statements), we will create and
develop a simple computerized guessing game that would be suitable for young
children. In the first version of the game, the program asks the player for a letter
between A and Z. If the player presses the correct letter on the keyboard, the program
responds by printing the message ** Right **. The program is shown here:
This program prompts the player and then reads a character from the keyboard. Using
an if statement, it then checks that character against the answer, which is K in this case.
If K was entered, the message is displayed. When you try this program, remember that
the K must be entered in uppercase.


Taking the guessing game further, the next version uses the else to print a message
when the wrong letter is picked.
NESTED IFS
nested if is an if statement that is the target of another if or else. Nested ifs are very
common in programming. The main thing to remember about nested ifs in Java is that
an else statement always refers to the nearest if statement that is within the same
block as the else and not already associated with an else. Here is an example:
As the comments indicate, the final else is not associated with if(j < 20), because it is
not in the same block (even though it is the nearest if without an else). Rather, the
final else is associated with if(i == 10). The inner else refers to if(k > 100), because
it is the closest if within the same block.
You can use a nested if to add a further improvement to the guessing game. This
addition provides the player with feedback about a wrong guess.


A sample run is shown here:
THE IF-ELSE-IF LADDER
A common programming construct that is based upon the nested if is the if­else­if
ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;


The conditional expressions are evaluated from the top downward. As soon as a true
condition is found, the statement associated with it is executed, and the rest of the
ladder is bypassed. If none of the conditions are true, the final else statement will be
executed. The final else often acts as a default condition; that is, if all other conditional
tests fail, the last else statement is performed. If there is no final else and all other
conditions are false, no action will take place.
The following program demonstrates the if­else­if ladder:
The program produces the following output:
As you can see, the default else is executed only if none of the preceding if statements
succeeds.
THE SWITCH STATEMENT
The second of Java’s selection statements is the switch. The switch provides for a
multiway branch. Thus, it enables a program to select among several alternatives.
Although a series of nested if statements can perform multiway tests, for many
situations the switch is a more efficient approach. It works like this: the value of an


expression is successively tested against a list of constants. When a match is found, the
statement sequence associated with that match is executed. The general form of the
switch statement is
switch(expression) {
case constant1:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   12   13   14   15   16   17   18   19   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə