<< Chapter < Page Chapter >> Page >

Exercise According to a formula by Euler,

1 1 2 + 1 2 2 + 1 3 2 + 1 4 2 + = π 2 6

Write a program to compute the series until the difference between the two terms is less than 0.1.

Do-while loops

Concept A loop enables the execution of a statement (including a block of statements within braces) an arbitrary number of times.This statement is called the loop body . In a do-while loop, an expression is evaluated after each execution of the loop body, andthe loop body continues to execute if and only if the expression evaluates to true.

The loop body of a do-while loop will execute at least one time. This type of statement is particularly appropriate for processing input,because you need to input data at least once before you can test it in an expression.

Program: Control04.java

// Learning Object Control04 //    do-while loopspublic class Control04 {     public static void main(/*String[] args*/) {         int input;        do {              input = Input.nextInt();        } while (input <= 0);         System.out.println(input);    } }

The program reads interactive input until a positive number is entered.

  • The variable input is allocated but not initialized.
  • The loop body of the do-while loop is executed. Jeliot displays Entering the do-while loop .
  • A value is read interactively into the variable input . First, enter a negative integer.
  • The expression following the while is evaluated. Since it evaluates to true, the loop body is executed again.Jeliot displays Continuing the do-while loop .
  • Now enter a positive value into the variable input .
  • The expression following the while is evaluated. Since it evaluates to false, the execution of the do-while loop is completed.Jeliot displays Exiting the do-while loop .

Exercise Rewrite this program with a while loop. Compare it to the do-while loop.

Break statements

Concept The exit from a while loop occurs before the loop body and the exit from a do-while loop occurs after the loop body. The break statement can be used to exit from an arbitrary location or locations from within the loop body.

The break statement is useful when the expression that leads to exiting the loop cannot be evaluated until some statements from the loop body have been executed,and yet there remain statements to be executed after the expression is evaluated.

Program: Control05.java

// Learning Object Control05 //    break statementspublic class Control05 {     public static void main(/*String[] args*/) {         int input;        int sum = 0;         while (true) {            input = Input.nextInt();             if (input < 0) break;             sum = sum + input;        }         System.out.println(sum);    } }

The program sums a sequence of nonnegative integers read from the input and terminates when a negative value is read.

  • The two variables are allocated and sum is initialized with the value zero.
  • The while statement is executed with true as the loop expression. Of course, true will never evaluate to false, so the loop will never be exited at the while .
  • An integer value is read from the input. If it is negative the break statement is executed and Jeliot displays Exiting the while loop because of the break .
  • Otherwise, the following assignment statement is executed and Jeliot displays Continuing without branching .
  • After the assignment statement is executed, the loop starts again.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
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
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, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask