






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























(^) Output the 7x table.
(^) Counter-controlled loops execute for a fixed number
of times.
(^) A special counter variable is used to control the loop
and may be referred to within the loop.
(^) Java provides the “for” statement as a counter-
controlled loop.
for ( initialisation statement;
condition; increment statement )
{
statements …
}
int n;
for ( n=1; n<=12; n++ ) { System.out.println (n + “ x 7 = “ + (n*7)); }
Assign n=1 n <= 12?
Output (7*n)
no
yes n++
(^) We can define a variable in the initialisation section,
which is local to the body of the loop. (^) for ( int i=1; i<=10; i=i+1 )
(^) Multiple comma-separated expressions can appear
in the “increment” section, even decrements. (^) for (int i=10; i>0; i--) (^) for (int i=1,j=7; i<=12; i++,j+=7)
for ( int i=1; i<=12; i++ ) System.out.println (i+" x "+n+" = "+(n*i));
Initialisation Condition?
Statements
false
Incr.
(^) Calculate ab^ using a for loop, assuming that a is a
float and b is an integer.
(^) for and if are both statements, therefore they can
each appear within the statement body.
for ( int i=1; i<=10; i++ ) { if (a<b) max=b; }
if (a<b) { for (int i=1; i<=10; i++ ) ... }
for ( int i=1; i<=10; i++ ) for ( int j=1; j<=10; j++ )
(^) Write programs to generate (on the screen) the
following triangles of user-specified height:
(^) If we do not know the number of iterations a priori (in
advance), we can use a condition-controlled (or event-controlled) loop - where the loop executes while a condition is true
(^) Two statements:
while (
do {
(^) Approximate the logarithm (with a base of 10) of an
integer using repeated division.
(^) Approximate the logarithm (with a base of 10) of an
integer using repeated division.
(^) Design a user interface where the user can continue
to ask for logarithms until a value of 0 is supplied.