CS 146: Introduction to Programming and Algorithms

Lab 4

In this lab we will review

·  The increment and decrement operators

·  The while loop

·  The do-while loop

·  The for loop

The increment operator

There are many times in the development of a program where it is necessary to add 1 to an existing variable. The increment operator provides a short and simple way of doing so.

The following two lines of code are equivalent:

var1 = var1 + 1;

var1++;

The increment operator (++) is called a unary operator because it can act on a single operand, in contrast to some other operators (e.g. + or /), which work on two operands.

The increment operator can be used before (prefix notation) an operand or after (postfix notation) an operand. The position of the increment operator is important because it affects the order in which processing is performed.

As an example consider the following two lines of code:

var1 = var2++;

var1 = ++var2;

In the first case, using postfix notation, var2 is assigned to var1 and then var2 is incremented. If var2 initially contains the value 6, than the result will be that var1 contains 6 and var2 contains 7.

In the second case (prefix notation) var2 is incremented prior to being assigned to var1. If var2 initially contains the value 6, than the result will be that var1 contains 7 and var2 contains 7.

The decrement operator

The decrement operator (--) has exactly the same characteristics as the increment operator except that it decreases the value of a variable by one rather than increasing.

So,
var1--;

reduces the value of the contents of var1 variable.

The while loop

A while loop repeats the processing of statements written inside a block (within curly braces) so long as a Boolean expression remains true. The format of the while loops is as follows:

while (boolean_expression)

{

execute statement;

execute statement;

:

}

The Boolean expression is evaluated and, if it results in TRUE, then the statements within the curly brace are executed. The Boolean expression is then reevaluated. If it remains TRUE then the same statements are executed a second time. This repeats until the Boolean expression results in FALSE.

This implies that there must be something within the while loop that changes things so that at some point the expression results in FALSE.

If the Boolean expression results in FALSE the first time it is evaluated then the contents of the loop are never executed. The increment or decrement operators are very often used to control a while loop:

int x = 10;
while (x > 0){

execute_statement;

execute_statement;

x--;

}

The Boolean expression can be a compound expression.

while (x > 0 & x < 10) {…..}

The Boolean expression can contain the mechanism for controlling the loop:

while (x-- > 0){ …….}

The do-while loop

The do-while loop is similar to the while loop except that the test expression is provided at the end of the loop:

do

{

execute statement;

execute statement;

:

} while (boolean_expression);

There are a couple of things to note. First, the loop is terminated with a semicolon after the Boolean expression. This is important and failure to put the semicolon in place will result in a compiler error.

The second thing to note is that the code in the curly braces is executed once before the test is performed. A do-while loop is always executed one time at least. This is valuable if you are using your code to obtain a value to test in the Boolean expression controlling the loop. An example of when this might prove valuable is if you are asking the user for a value (say in a menu system) and then using the response from the user to determine whether or not you should repeat the process.

The for loop

The for loop allows the programmer to specify how many times a block of code is executed. The structure of the for loop is as follows:

for (initialize_loop_counter; test_loop_counter_value; change_loop_counter_value)

{

execute_statement;

execute_statement;

:

}

The “for loop” contains three components:

·  A statement that initializes the loop counter

·  An expression that tests to see whether or not the loop should be executed

·  A statement to modify the loop counter.

An example for loop is shown below:

for (int count = 0; count < 10; count++)

{

System.out.printf(“Count is %d“, count);

}

In this example, count is initialized to 0. The value of count is then tested to see if it is less than 10. Because it is less than 10, the contents of the loop block are executed. The value of count is then incremented.

The value of count is repeatedly tested, the loop block executed and count incremented until the test expression is false.

There are three problems that can arise with a for loop

1.  The test expression can be false to begin with so the loop is never executed

2.  The test expression might never evaluate to false so the loop is infinite

3.  There is a difference in the number of iterations between test expressions such as count < 10 and count <= 10. Sometimes programmers make mistakes on the number of times a loop executes.

Exercise 1: The Arithmetic Mean

Write a program called ArithmeticMean that prompts the user to

1.  Enter the number of values that should be processed

2.  A set of values to be processed

And then calculate the arithmetic mean of the values entered. You should use a while loop to perform the summation part of the calculating the arithmetic mean.

The program should output the initial data and the labeled arithmetic mean. How could you format the output to make it look good? Consider printf, \t and DecimalFormat.

Exercise 2: The do-while loop

Write a program called PasswordChecker that does the following:

1.  prompts the user to enter an ID and password

2.  prompts the user to renter the password

3.  checks to ensure that the two password entries are identical

4.  repeats steps 1 through 3 until the password is correctly entered twice.

You should use a do-while loop to implement your solution and use the scanner for I/O.

Exercise 3: The for loop

Write a program that does the following:

1.  Accepts a positive integer between 5 and 15 from the user

2.  Uses for loop(s) to draw a triangle on the screen with the correct number of rows. If the user supplies the number 7 to the program, the output will look like this:
x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx