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

do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
what is system testing
what is the application of nanotechnology?
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
anybody can imagine what will be happen after 100 years from now in nano tech world
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
silver nanoparticles could handle the job?
not now but maybe in future only AgNP maybe any other nanomaterials
I'm interested in Nanotube
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?