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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
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