GNU Java Training Wheels 1.1: Tutorial 4 Four looping constructs

Next: Tutorial 5 A beer drinking song, Previous: Tutorial 3 superfor loops and for loops, Up: J.T.W. Tutorials [Contents][Index]

### 4.4 Tutorial 4 Four looping constructs

Question 4.4.1: Study, compile and run the following code:

    class LoopTest
    begin
        function int powerOf2A(int n)
        begin
            var int counter = n;
            var int result  = 1;
            while (counter > 0)
            begin
                result = 2 * result;
                counter = counter - 1;
            end
            return result;
        end

        function int powerOf2B(int n)
        begin
            var int counter = n;
            var int result  = 1;
            do
            begin
                result = 2 * result;
                counter = counter - 1;
            end while (counter > 0);
            return result;
        end

        function int powerOf2C(int n)
        begin
            var int result = 1;
            for (var int counter = n; counter > 0; counter = counter - 1)
            begin
                result = 2 * result;
            end
            return result;
        end

        function int powerOf2D(int n)
        begin
            var int result = 1;
            superfor (var int counter=n downto 1)
            begin
                result = 2 * result;
            end
            return result;
        end

         /**
          * Prints a row of stars of a given length.
          */
        function void printLineC(int length)
        begin
            for (var int i = 0; i<length; i=i+1)
            begin
                System.out.print("#");
            end
            System.out.println();
        end

        beginMain
            // For Question 4.4.1 add some code here...
        endMain
    end

Question 4.4.2: To the `main` `function` add some code to call the functions `powerOf2A`, `powerOf2B`, `powerOf2C` and `powerOf2D` to verify that they all return the same result. To inspect the result you will need to apply the `System.out.println()` statement to the values returned by those functions.

Question 4.4.3: There is a bug in the `powerOf2B` `function` because it does not behave correctly in the case when `n` is zero or less. Put an `if` (...) `then` ... statement at the top of this `function` to make it handle the case of zero properly. Also make it return 1 in the case that `n` is less than zero.

Question 4.4.4: There is a bug in the `powerOf2D` `function` because it does not behave correctly in the case when n is zero or negative. Make it return 1 if `n <= 0`. Put an `if` (...) `then` ... statement at the top of this `function` to make it handle these cases properly. Since this `function` returns an `int`, make it return 1 in these cases.

Question 4.4.5: By copying the pattern of `powerOf2A`, `powerOf2B`, `powerOf2C` and `powerOf2D`, write functions `printLineA`, `printLineB` and `printLineD` that work identically to the `function` `printLineC`, except that they use `while` loops, `do` loops and `superfor` loops, respectively. Add some code to the `main` `function` to test them out.

Question 4.4.6: Based on the previous three questions, is there a best looping construct? Or does it depend on what the looping construct is going to be used for?

Next: Tutorial 5 A beer drinking song, Previous: Tutorial 3 superfor loops and for loops, Up: J.T.W. Tutorials [Contents][Index]