Iterative Statements .edu
Iterative Statements 39
The while Statement 39
Nested while Loop 46
The do…while Statement 58
The for Statement 60
Nesting for Loops. 65
Scope of a Loop control variable 66
PitFall 67
Pitfall I – Updating the loop variable incorrectly 67
Pitfall II – Modifying the loop variable in loop body 67
Iterative Statements
So far in our studies, we have discussed and have been using two forms of Java statements – sequence and selection. There is a third type of Java statement – the iterative statements, commonly called loops. Iterative statements cause a program to repeat certain statement or block of statements. The statement or block of statements is referred to as the loop body. You may be wondering how the program knows to execute the loop body repeatedly. The answer is, a conditional expression is used to determine if the loop is to continue to execute, or to terminate.
If you were asked to add all the integers from 1 to 1000 and the only method that you know was addition, you would more than likely write something like this:
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
5 + 1 = 6
:
:
:
Surely it will not be long before you realize that this approach is repetitive and laborious. The concept, iteration, which is a fundamental tool to programming, relieves us from this drudgery. In likewise manner, the computer is capable of carrying out tasks on a repetitive basis.
Java provides three forms of iterative statements. These are the while statement, the do…while statement, and the for statement.
The while Statement
The format of the while statement is as follows:
while ( conditional_expression )
body;
Where the word while is the Java keyword indicating a repetitive construct. Within the pair of parentheses is a conditional expression that must be tested, in order to determine if the loop body must be executed again, and again, until a certain condition stops it.
The while statement specifies that the conditional expression must be tested at the beginning of the loop. If the conditional expression is initially false, then the loop body is skipped. Figure 4.14 shows a diagrammatical representation of the while loop.
false
true
Figure 4.14 symbolic representation of the while loop.
Example 4.8
Write a while loop that prints all the integers from 1 and 1000.
Solution
The solution to this problem focuses on two key issues - counting from 1 to 1000, and print the number. The solution is depicted in Figure 4.15.
false
true
Figure 4.12 a diagrammatic solution t the problem.
Listing 4.15 shows the solution in code form.
1. public class Integers
2. public static void main(String[] arg)
3. {
4. int counter = 1;
5. while (counter 0
The for Statement
The for statement, like the while and the do..while statements, is a looping construct that continually executes a section of code, if a given condition is true. When the condition becomes false the section of code is skipped, and the next statement following it is executed. The general format of the for statement is as follows:
for ( data_type id = initialValue; conditional_expression; adjust_id )
statements;
The for loop is a single executable statement consisting of two major parts – loop heading, following the keyword for, and the loop body. The loop heading is enclosed within the pair of parentheses and consists of three parts, each separated from the other by a semi-colon.
• The first expression - data_type id = initial - constitutes the declaration and initialization of the control variable for the loop.
• The second expression - conditional_expression - constitutes the condition under which the loop iterates. Any valid conditional expression is acceptable.
• The third expression - adjust_id – updates the value of the loop variable.
The loop body constitutes the statement or statement block to be executed. The loop body executes only if the conditional expression is true, other than that the for loop terminates. Figure 4.14 shows a diagram of the for loop.
true
false
Figure 4.13 a representation of the for loop.
The for statement behaves the following way:
1. First, the control loop variable is declared and initialized.
2. Second, the condition is tested. If the condition is true, the loop body is executed, if not, it terminates.
3. Third, the loop variable is updated. That is, the control variable is re-assigned, and step 2 is repeated.
Example 4.12
Write a program that lists the integers from 1 to 10, along with their squares and their cubes. That is, the program produces:
1 1 1
2 4 8
3 9 27
:
:
Let us represent the solution using a flow diagram, as shown in Figure 4.14.
true
false
Figure 4.14 for loop representation
Listing 4.25 shows a translation of Figure 4.14.
1. public class Powers
2. {
3. public static void main(String[] arg)
4. {
5. System.out.println("N\tN*N\tN*N*N");
6. for (int i = 1; i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- minecraft edu edition
- minecraft edu download free
- minecraft edu edition recipes
- minecraft edu mod download
- minecraft edu launcher download
- iterative fibonacci python
- fibonacci sequence iterative python
- iterative fibonacci
- iterative model in software engineering
- iterative software development
- iterative development definition
- iterative software development methodology