<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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