<< 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.

Back to Question 10

Answer 9

D. Goodbye World

Explanation 9

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.

Back to Question 9

Answer 8

D. Hello World

Explanation 8

A simple if statement

This program contains a simple if statement that

  • uses a relational expression
  • to return a value of type boolean inside its conditional clause

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 .

Back to Question 8

Answer 7

A. Compiler Error

Questions & Answers

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask