Programming Example: Classify Numbers
This program reads a given set of integers and prints the number of odd and even integers, as well as the number of zeros.
The input to this program is 20 integers (positive, negative, and zeros) but the number of integers read can be easily modified.
The output is the number of zeros, even numbers, and odd numbers.
The solution consists of using a for loop to get and evaluate 20 numbers from the user. Each number is put through a switch structure with cases depending on the remainder if the number is divided by 2. If the number is even the number of evens is incremented, if the number is 0, the number of zeros is incremented, and if the number is odd count is incremented.
The do…while Looping (Repetition) Structure
The third type of looping or repetition structure uses the reserved words do and while, and is called the do…while loop. The syntax of this loop is:
do{
statement(s);
}
while(expression);
The expression is called the loop condition.
The statement executes first, and then the expression is evaluated. If the expression evaluates to true, the statement executes again. As long as the expression in a do...while statement is true, the statement executes. To avoid an infinite loop, you must make sure that the body of the loop contains a statement that ultimately makes the expression evaluate to false and assures that it exits properly.
In a while or for loop, the loop condition is evaluated before executing the body of the loop. Therefore, while and for loops are called pre-test loops. On the other hand, the loop condition in a do...while loop is evaluated after executing the body of the loop. Therefore, do...while loops are called post-test loops.
break and continue Statements
The break and continue statements alter the flow of control in a program. The break statement is typically used for two purposes:
1. To exit early from a loop
2. To skip the remainder of a switch structure
After the break statement executes, the program continues to execute with the first statement after the structure.
The break statement can be placed within an if statement in the loop. If the certain condition is found, the loop will be exited immediately. This is similar to changing the value of a flag variable in an if statement within a flag-controlled loop.
The continue statement is used in while, for, and do...while structures. When the continue statement is executed in a loop, it skips the remaining statements in the loop and proceeds with the next iteration of the loop. In a while and do...while structure, the expression (that is, the loop-continue test) is evaluated immediately after the continue statement. In a for structure, the update statement is executed after the continue statement, and then the loop condition (that is, the loop-continue test) executes.
Teaching Tip
|
The continue statement cannot be used in a switch statement.
|
Teaching Tip
|
In a while loop, when the continue statement is executed, if the update statement appears after the continue statement, the update statement is not executed. In a for loop, the update statement always executes.
|
Teaching Tip
|
Java also supports labeled break and labeled continue statements. For more information, see:
|
Nested Control Structures
Nesting of control structures provides new power, subtlety, and complexity. if, if…else, and switch structures can be places within while loops. For loops can be found within other for loops as in the following example:
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= I; j++)
System.out.print(“*”);
System.out.println();
}
Analysis of this program will show how the outer loops provides the limiting condition for the inner loop which controls the number of stars printed during each iteration.
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