3c

Advanced Placement

Computer Science Lab

 

 

Control Structures - Loops

 

 

 

Introduction

 

Often in a computer program there is a need to execute a block of code repeatedly.  While this could be accomplished with conditional statements, most programming languages have special control structures for this purpose called loop structures. Loop structures are of three types, the for loop, the while loop and the do loop.  The syntax for the for loop is as follows:

 

 

 

 

 

for (control variable initializer, continue condition adjustment)

{

     statements

}//end for

 

 

An example of a for loop is as follows:

 

int i

for (i = 0; i < 100; i++)

{

     System.out.println(“This is iteration number “ + i);

}//end for

 

 

 

 

 

 

The parentheses behind the for keyword consists of three statements.  The first sets the control variable to an initial value.  The second element establishes the condition which must be met to discontinue the loop and the third statement designates how the control variable will be incremented. That hese are statement is established by the semicolon that separates each  Note that the declaration of the control variable can be included in the parentheses.  Also, if only one statement is to be repeated, the curly braces can be omitted.  As with conditional statements, loop structures can also be nested.

 

The syntax for the while loop is as follows:

 

 

 

 

 

(control variable initializer)

 

while (continue condition)

{

     statements (include a condition adjustment)

}//end while

 

 

 

 

 

An example of a while loop is as follows:

 

char continue = ‘y’;

int i = 1;

while (continue == ‘y’)

{

     System.out.println(“This is iteration number “ + i);

     i = i +1;

     System.out.println(“Do you want to continue? y or n ”);

     try

     {

          continue = (char) System.in.read();

     }

     catch (IOException e)

     {

          System.out.println("Error reading from user.");

     }

 

}//end for

 

 

 

 

 

 

Notice that unlike the for loop, the while loop is indeterminate, i.e. it does not have a fixed number of iterations.  The continue condition must appear in the parentheses behind the while keyword.  Note that you should avoid equality testing floats and doubles in loops because the floating point number are always approximations and equality may never be attained thereby resulting in an infinite loop.

 

The do loop is a variation of the while loop with syntax as follows

 

 

 

(control variable initializer)

do

{

     statements (include a condition adjustment)

}

while (continue condition)

 

 

In the case of a do loop, the statements in the loop will be executed at least once and the continue condition is then evaluated.

 

 

 

 

 

There are two keywords that can modify the behavior of loops and these are break and continue.  A break statement within a loop will usually discontinue the loop even if the condition for leaving the loop has not been met.  The continue statement will terminate the current iteration, i.e. jump to the next iteration of the loop.

 

 

 

 

Procedure

 

A.  Experimenting with Loops

 

1.         Type the following code into a a new project named ExpLoop.

 

 

/*

ExpLoop – Application

(Your Name) Version (x.x) (date xx/xx/xx)

Prints a pattern based upon using loops

*/

 

class ExpLoop

{

     public static void main (String args[])

     {

          int i;

          int j;

 

          for (i = 0; i < 10; i++)

          {

              for (j = 0; j < i; j++)

              {

                   System.out.print(“*”);

              }

              System.out.println(“”);

          }

     }//end main

}//end ExpLoop

 

 

 

            This is an example of a nested loop. How many “stars” print in the last line? What would alter to get the pattern reversed, i.e. a long string of “stars” in the first row?

 

2.         Change the above program to use while loops instead of for             loops. 

 

 

 

 

Programming Assignment

 

Write a program that will accept a series of grades on an exam and find the average, the minimum and the maximum grade.  The program should be tested with an input of at least ten different grades in the run that you turn in for grading.  The program should grade as many tests as the user wishes until the user terminates the process.  The output should identify the total number of students, the average, the highest grade (max) and lowest grade (min):  The output should also compute and show the standard deviation.  An example of your output would be similar to the following:

 

Total Number of Students:               12

Average grade:                                 76.34

Standard deviation                           6.38

Highest grade:                                   98

Lowest grade:                                   69

 

Use the GUI interfaces discussed in Exercise 3a for your input and output. Output should be decimal formatted for the average and the standard deviation to two decimal places.  Turn in a program listing your source code.  Source code should use proper formatting.  Also turn in a copy of your output.  This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the clipboard. Then paste the contents of the clipboard into WordPad and print out the document.  In addition to the source code, the output, also turn in the answer to the questions listed below.