<< Chapter < Page | Chapter >> Page > |
A switch structure
There is a somewhat more specialized, control structure named switch that can also be used to make decisions in a multiple choice sense under certain fairlyrestrictive conditions.
However, the structure shown in this program can always be used to replace a switch. Therefore, I find that I rarely use the switch structure, opting instead for the more general form of multiple-choicestructure.
D. Goodbye World
An if-else control structure
This program contains a simple if-else control structure.
Behavior of if-else structure
If the expression in the conditional clause returns true, the block of code following the conditional clause is executed, and the block of code followingthe word else is skipped.
If the expression in the conditional clause returns false, the block of code following the conditional clause is skipped, and the block of code following theword else is executed.
This program executes the else block
In this program, the expression in the conditional clause returns false.
Therefore, the block of code following the word else is executed, producing the words Goodbye World on the computer screen.
Can result in very complex structures
While the structure used in this program is relatively simple, it is possible to create very complex control structures by nesting additional if-else structures inside the blocks of code.
D. Hello World
A simple if statement
This program contains a simple if statement that
Tests for x less than y
The relational expression tests to determine if the value of the variable named x is less than the value of the variable named y .
Since the value of x is 5 and the value of y is 6, this relational expression returns true.
Behavior of an if statement
If the expression in the conditional clause returns true, the block of code following the conditional clause is executed
What is a block of code?
A block of code is one or more statements surrounded by matching curly brackets.
For cases like this one where the block includes only one statement, the curly brackets can be omitted. However, I prefer to put them there anyway. Theydon't cause any harm and help me avoid programming errors if I come back later and add more statements to the body of the if statement.
Display the word Hello
In this program, execution of the code in the block causes the print method to be called and the word Hello to be displayed followed by a space, but without a newline following the space.
What if the conditional clause returns false?
If the expression in the conditional clause returns false, the block of code following the conditional clause is bypassed.
(That is not the case in this program.)
After the if statement ...
After the if statement is executed in this program, the println method is called to cause the word World to be displayed on the same line as the word Hello .
A. Compiler Error
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?