Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
13Activity
0 of .
Results for:
No results containing your search query
P. 1
Java Chap7 Iteration through Loops ( Prof. Ananda M Ghosh)

Java Chap7 Iteration through Loops ( Prof. Ananda M Ghosh)

Ratings:

4.0

(1)
|Views: 2,902|Likes:
Looping in Java language, for Loop, nested loops, while-do loops, do-while loop, brake in a loop,
Looping in Java language, for Loop, nested loops, while-do loops, do-while loop, brake in a loop,

More info:

Published by: Prof. (Dr) Ananda M Ghosh. on Jan 07, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

12/03/2012

pdf

text

original

 
Chapter 7:Iterations through Loops
7.0 IntroductionIteration means repetition or looping over a portion of a program
. For solving problems by a computer, it becomes very often necessary to loop over a portion of a program, again and again, so that a desired goal is achieved. For example, marks sheets for 30 students of a class are to be prepared by taking help of a computer program. A markssheet preparing procedure for every student will be the same but that procedure is got to berepeated 30 times to complete the total job. Thus the marks-sheet processing portion is gotto be repeated as many times as there are number of students. For the marks-sheets case, therepetition will be carried out for a known number (equal to the number of students) of times.The
for
loop is generally used
when it is known in advance how many times a loop is gotto be executed
.There are some kinds of problems where the number of iterations that will berequired to reach the final goal may not be known in advance. In such cases, number of iterations can be controlled by taking help of some condition expression and using
while
or 
do-while
statements. The
condition expression determines how many times the looping
will be carried out. For example, suppose you set a condition like (allowed error <= 0.5) toterminate a do-while loop. How many iterations will be required to reach that state (i.e. error <= 0.5) is not known to the programmer, but the looping required to keep error within thetarget, will automatically be controlled by the condition expression. Infinite looping mayoccur due to improper setting of the condition expression. We will see how a java programmer can also tackle such exceptional conditions.
7.1 For Fixed Number of Iterations
 – for loop
The
for loop
iteration is used when number of repetition is fixed and known in advance. Ithas a general form of:
for ( initialization; condition test; iteration) {// body of the loop}
If only one statement is being repeated, the curly brackets may not be used.For example,
 
 
for ( n = 0; n < 15; n++)
System.out.println (“ repeat count number = ” + n);Or 
for ( a = -3; a <= 3; a = a + .5)
{value = a*a + 2* a – 4;System.out.println (“result will be “ + value + “ for a = “ + a);}Let us now see a program where both the types of 
for loop
statements have been used – 
Example 7.1 Demo of a
 for loop
Statementpublic class ForDemo
{
public static void main()
{double a, value;int n; // multi-statement for loop
for
 
( a = -3; a <= 3; a = a + .5)
{value = a*a + 2* a - 4;System.out.println( " result will be " + value + " for a = " + a);}// single statement for loop
for ( n=0; n < 10; n++)
System.out.println (" count number is = " + n);} // main ends here} // class ends here Just enter this program, compile and execute to see what output appears on the terminalwindow. Try to interpret the outputs you obtain.
Loop variables
used in the above program are
a
and
n
, both of which have been
declared outside the loop statement
but not used anywhere else. In such cases,
variablesthat control for loops
can easily be
declared inside the initialization portion
. That is,
for ( double a = -3; a <= 3; a = a +.5)
could be used in the first
for
and 
for ( int n =0; n < 10; n++)
could also be used for the second
for
.
The scope of inside variables ends when the for statements end
. Outside that
for
loop,the variables will cease to exist can be used for some other purpose.
 
7.1.1
 
 for loop
– Some Variations
The three sections of 
a for loop
(namely,
initialization
,
condition test
,
iteration
) can beused for many other purposes. Either the initialization or the iteration expression or both may be kept blank. Even all three sections of a
for loop
can be kept blank creating an
infiniteloop
. For example – 
int i = 0;for ( ; i < 10, i++) {// first section absent// loop body for fixed number iterations}int i =0;for ( ; !done; ) {// first and third sections absent// loop body repeats until test condition not fulfilled}for ( ; ; ) {// loop body// for infinite iterations// all three sections absent}Time delay
loop can easily be created in the following fashion:-
for( int t=0; t < 600; t++);// having an empty loop body7.1.2
 
Multiple Initialization and Iteration Expressions in a
 for Loop
It is possible to use more than one statements in the
initialization and iteration portions of 
a
for loop
. Now see an example of such a usage.
Example 7.2 Demo of Multi-Statement
 for 
public class MultiStatmntFor
{
public static void main()
{int x, y; 
for ( x = 0, y =10; x < y; x +=2, y -=2)
{ System.out.println (" x = " + x + " y = " + y);System.out.println (" Values of x & y in next iteration");}

Activity (13)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Victor Petrescu liked this
Frances Campbell liked this
Partha Das liked this
hari_14_1987 liked this
Vikram Yadav liked this
Madhuri Datta liked this
tarunarun liked this
vpyadav7294 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->