CSU Banner

CS 163/164, Summer 2018

Lab 7 - Control Loops

Monday, Jun. 25th


Objectives of this Lab

  1. Learn about using loops as another way to control the execution flow of a program,
  2. understand for, while, and do-while loops, and
  3. write simple examples of all three types of loops.

Understanding for, while and do-while loops

WITHOUT RUNNING THIS CODE, answer the following questions using comments in R7.java:

Question 1) What does the following loop output?
for(int i = 5; i < 30; i+=5)
    System.out.println(i);
Question 2) What does the following loop output?
int j = 0;
while (j <= 8) {
    System.out.println(++j); // preincrement operator!
}
Question 3) What does the following loop output?
for (int k = 7; k >= 0; k--);
    System.out.println(k); // are you sure?
Question 4) What does the following loop output?
for (int l = 0; l > 0; l++)
    System.out.println(l);

Question 5) What does the following loop output?
int m = 0;
do {
    System.out.println(m--); // postincrement operator
}
while (m > 0);

Your teaching assistant will discuss the output of each of the loops.


Today's assignment: Writing simple loops

Complete the following tasks and show your program to a TA when you are finished.

  1. Create a new project in Eclipse called R7, and a new class called R7.java.
  2. Cut and paste the following code inside the curly braces for R7:
    public static void reverseString(String s) {
        // add code here
    }
    
    public static void printAscii(char start, char end) {
        // add code here
    }
    
    public static double raiseToPower(double number, int exponent) {
        // add code here
    }
    
    
  3. Complete reverseString() using a for loop, print out all characters of of the String parameter in reverse order. All characters should print on one line, and the method should end by printing a newline.
  4. Complete printAscii() using a for loop, print out all characters in the ascii table between the codes: start and end (inclusive). Each character should be on a separate line that contains the ascii code followed by a colon, space, then the character itself. HINT: Use a loop counter of type char.
  5. Complete raiseToPower() using a while loop. DO NOT USE Math.POW()!!!! The method computes and returns the specified floating-point number raised to the supplied integer exponent, and does not print anything.

Test Code

You can test your own methods by adding calls from main, and printing the results.
    // Test reverseString
    String forward = "Java Programming rules!";
    System.out.print("Forward string: ");
    System.out.println(forward);
    System.out.print("Reverse string: ");
    reverseString(forward);
        
    // Test printAscii
    System.out.println("ASCII Table");
    printAscii('!', '9');
        
    // Test raiseToPower
    System.out.printf("3.0 to the 5 = %.3f\n", raiseToPower(3.0, 5));
    System.out.printf("2.5 to the 2 = %.3f\n", raiseToPower(2.5, 2));
        

Sample Output

Here is the output from our test code:
    Forward string: Java Programming rules!
    Reverse string: !selur gnimmargorP avaJ
    ASCII Table
    33: !
    34: "
    35: #
    36: $
    37: %
    38: &
    39: '
    40: (
    41: )
    42: *
    43: +
    44: ,
    45: -
    46: .
    47: /
    48: 0
    49: 1
    50: 2
    51: 3
    52: 4
    53: 5
    54: 6
    55: 7
    56: 8
    57: 9
    3.0 to the 5 = 243.000
    2.5 to the 2 = 6.250

Show your output to the TA for credit.


CS Banner
CS Building