lesson_07.2

Lesson_07.2 - Iterative Statements Introduction The while statement The do/while statement The for Statement Iterative Statement Objective To

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Iterative Statements Introduction The while statement The do / while statement The for Statement
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iterative Statement Objective To understand iteration as means of controlling program flow To know the three forms of iterations: while do/while for
Background image of page 2
Iteration We have discussed two forms of Java statements – sequence and selection. There is a third type of Java statement – the iterative statements, commonly called loop. Iterative statements cause a certain statement or block of statements to be repeated. The statement or block of statements is referred to as the loop body . How does the program knows to execute the loop body repeatedly? Answer - a conditional expression is used to determine this.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iteration Problem - add all the integers from 1 to 1000 . Here is one approach 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
Background image of page 4
Iteration Java provides three forms of iterative statements: while do…while , and for
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The while Statement The format of the while statement is as follows: while ( conditional_expression ) body; Where - while is the Java keyword indicating a repetition. The conditional expression determines if the loop body must be executed 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.
Background image of page 6
Iteration - symbolic representation of the while loop. Loop Body Program execution continues conditional expression Initialize the loop variable false true
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iteration Example 1 Write a while loop that prints all the integers from 1 and 1000. Solution The solution to this problem focuses on two key issues: 1. Count from 1 to 1000, and 2. Print the number.
Background image of page 8
Iteration - while counter <= 1000 Print the number Update counter: counter = counter + 1 Program execution continues counter = 1
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Program code 1. public class Integers 2. { 3. public static void main(String[] arg) 4. { 5. int counter = 1; 6. 7. while (counter <=1000 ) 8. { 9. System.out.println(counter); 10. counter = counter + 1; 11. } 12. } 13. }
Background image of page 10
Example Design a class that accepts an integer value and prints the digits of the number in reverse order. For example, if the number is 234 , the program should print 432 , or if the number is 24500 , it should print 00542 .
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Analysis: 1. Based on the problem definition, we cannot tell in advance the number of digits contained in a given number. 2. Focus is on printing the digits from the rightmost to the leftmost one. 3.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/06/2012 for the course COP 2250 taught by Professor Staff during the Fall '08 term at FIU.

Page1 / 48

Lesson_07.2 - Iterative Statements Introduction The while statement The do/while statement The for Statement Iterative Statement Objective To

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online