1 / 11

Iteration

Iteration. Overview while statement for statement do while statement. while statement. It is very common in programming to have a statement that needs to be executed more than once.

huntk
Download Presentation

Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iteration Overview • while statement • forstatement • dowhile statement

  2. while statement • It is very common in programming to have a statement that needs to be executed more than once. • To avoid repeating the same statement in a program, java provide three repetition (loop) statements that may be used. These are while, for and do-while. while Statement: • The Syntax of the while statement is as follows: while (condition) statement; • Interpretation • First evaluate the condition. If it is true, the statement is executed; then the condition is evaluated again • The statement is executed over and over until the condition becomes false • If the condition is false initially, the statement is not executed even once • Therefore, we say that awhilestatement executes its body zero or more times

  3. whilestatement flow diagram false condition true statement • Flow diagram for while statement

  4. whilestatement Example • The following example prints a table of squares for integers from 1 to 10. class WhileExample { public static void main(String[] args) { int i; i = 0; while (i < 10) { i++; System.out.println(i+ "\t" + Math.pow(i, 2)); } } }

  5. Important points about whilestatement • The condition of the while statement usually involves at least one variable called loop control variable. • The loop control variable should have a value ( initialized ) before entering the loop. • The loop control variable should be updated inside the body of the loop such that the condition will eventually evaluates to false, otherwise loop will be an infinite loop. public class Forever { public static void main(String[] args) { int i = 0; while (i < 10) System.out.println(”Never stop"); } }

  6. for statement • Because most loops involves three steps, namely, initialization, checking the condition and updating the control variable, Java provides the for statement which summarizes them on one line. • The Syntax is: for (initialization; condition; updating) statements • Interpretation • First initialization statement is executed • Then the condition is evaluated • If the condition is true, • The statements inside the loop are executed • Then the updating statement is executed • Then the condition is checked again, etc. • If the condition is false, • The control will proceed to the next statement(s) after the for loop

  7. For statement flow diagram • Flow diagram for ( ) { true } false Initialization Statement Updation Statement condition Statements inside the for loop

  8. for statement Example • The following example prints a table of squares for integers from 1 to 10 using for loop. class ForExample { public static void main(String[] args) { for (int i = 1; i <= 10; i++) System.out.println(i + "\t" + Math.pow(i,2)); } }

  9. do while statement syntax • Some times it is more appropriate to perform the test of a loop at the end. For this Java provides the do-while statement. • The Syntax is: do { statements } while ( condition ) ; • Interpretation • First the statements inside the do loop are executed regardless of the condition • Then the condition is evaluated • If it is true • The statements inside the do loop are executed again • And the condition is checked again, etc. • If condition is false • The statement that follows the do while statement is executed

  10. do while statement flow diagram • Flow diagram true false Statements inside the do loop Condition

  11. do while statement Example import java.io.*; class DoExample { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); int num; do { System.out.println("Enter a Number or 0 to stop execusion"); String input= stdin.readLine(); num = Integer.parseInt(input); System.out.println("Squared number is:"+(num*num)); } while (num != 0); } }

More Related