Friday, January 25, 2013

Java Control Structures Part 2-1


Why Is Repetition Needed?

There are many situations in which it is necessary to repeat a set of statements.  For example, when a certain formula is going to be used several times (obtaining an average of grades for students in a class). Java has three repetition, or looping, structures that let you repeat statements over and over again until certain conditions are met: while, for, and do…while.


while Looping (Repetition) Structure

The reserved word while can be used to repeat a set of statement until a certain condition is met.  The syntax for the while loop is:

            while (expression)
                      statement

The expression, called a loop condition, acts as a decision-maker and is a logical expression. The statement is called the body of the loop. Moreover, the statement can be either a simple or compound statement.
The expression provides an entry condition. If it initially evaluates to true, the statement executes. The loop condition—the expression—is then reevaluated. If it again evaluates to true, the statement executes again. The statement (body of the loop) continues to execute until the expression is no longer true.

A loop that continues to execute endlessly is called an infinite loop.

A variable in the loop condition that is used to determine whether the body of the loop will execute is called aloop control variable.

Counter-Controlled while Loops

When you know exactly how many times certain statements need to be executed, the while loop assumes the form of a counter-controlled while loop.  To do this, you set up a counter that is initialized to 0 before the while statement and then increment the counter in the while statement to keep track of how many times the body has been executed.  The syntax for a counter-controlled while loop is:

int N = some value either inputted by the user, or some specified value;
int counter = 0;
while (counter < N)
{
    statements
   
    counter++;
}

Sentinel-Controlled while Loops

You might not know exactly how many times a set of statements needs to be executed, but you do know that the statements need to be executed until a special value is met. This special value is called a sentinel.

In such cases, you read the first item before entering the while statement. If this item does not equal the sentinel, the body of the while statement executes. The while loop continues to execute as long as the program has not read the sentinel. Such a while loop is called a sentinel-controlled while loop.  The syntax for the sentinel-controlled while loop is:

input first data item into variable;
while (variable != sentinel)
{
            .
            .
            .
     input a data item into variable;
}

Flag-Controlled while Loops

Using a boolean value to control a while loop (called a flag variable) is called the flag-controlled while loop.  The syntax for this loop can be similar to the following:

boolean found = false; 
while(!found)
{
            .
            .
     if(expression)
         found = true;
            .
            .
}


EOF-Controlled while Loops

If the data file is frequently altered (for example, if data is frequently added or deleted), it’s best not to read the data with a sentinel value. Someone might accidentally erase the sentinel value or add data past the sentinel. Also, the programmer sometimes does not know what the sentinel is. In such situations, you can use an EOF (End Of File)-controlled while loop.

In Java, the form of the EOF-controlled while loop depends on the type of stream object used to input data into a program. It now follows that a general form of the EOF-controlled while loop that uses the Scanner object console to input data is of the following form:

while (console.hasNext())
{
// Get the next input (token) and store it in an appropriate variable.

// Process the data.
}

If the input source is a file, the condition might be something like infile.hasNext(), rather than console.hasNext().


Teaching Tip

In the Windows console environment, the end-of-file marker is entered using Ctrl+z. (Hold the Ctrl key and press z.) In the UNIX environment, the end-of-file marker is entered using Ctrl+d. (Hold the Ctrl key and press d.)



More on Expressions in while Statements

There are situations where the expression in the while statement may be controlled by multiple variables.  In such cases, relational operators are often used to create more complex logical conditions.


Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678



No comments:

Post a Comment