1 / 29

Iteration Statement Session 7

Iteration Statement Session 7. Course : T-0974 Algorithm & Object-Oriented Programming I Year : 2010. Learning Outcomes. After taking this course, students should be expected to apply and demonstrate using Looping and Looping Control Structure. Outline Materi. While Do-While For

Download Presentation

Iteration Statement Session 7

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 StatementSession 7 Course : T-0974 Algorithm & Object-Oriented Programming I Year : 2010

  2. Learning Outcomes After taking this course, students should be expected to apply and demonstrate using Looping and Looping Control Structure.

  3. Outline Materi • While • Do-While • For • Nested

  4. Interation Statement • Control Structure that controlling how many statements/block is executed. • As a part of programming fundamental. • Coding Efficiency. • Three types of Iterations • while • do – while • For • Iteration can be implemented inside iteration (nested)

  5. While Statement • Syntax: while (loop-continuation-condition) { statement(s); } • Flowchart: count = 0; Loop Continuation Condition? (count < 10)? false false true true Statement(s) (loop body) System.out.println(“Welcome to Java!”); count++;

  6. While Statement • Loop-continuation-condition • As boolean expression. • Loop is executed when its condition is true. • Its argument is inside parentheses (…) • Don’t put a semi-colon after while(…) • Curly block is needed when it has more than one statement.

  7. While Statement 1 • Example : int count=0; while(count < 10) { System.out.println("Welcome to Java!"); count++; } • Steps : • [1] Count variable is initialized. • [2] Check if count < 10; • 3] If true, execute the statement inside block If false, break from loop • [4] Increase count variable by 1. • [5] return to point [2] 2 4

  8. While Statement

  9. Do-While Statement • Syntax: do { statement(s); } while (loop-continuation-condition); • Flowchart: count = 0; Statement(s) (loop body) System.out.println(“Welcome to Java!”); count++; true Loop Continuation Condition? (count < 10)? true false false

  10. Do-While Statement • Loop-continuation-condition • As boolean expression • Loop is executed when its condition is true. • Its argument is inside parentheses (…) • Starts by do and ends by while(…) and Semicolon(;) • Curly block {…} is needed when it has more than one stetement.

  11. Do-While Statement 1 • Example : int count=0; do { System.out.println("Welcome to Java!"); count++; } while(count < 10) • Steps : • [1] Count variable is initialized by 0 • [2] Statements are executed inside block. • [3] Count is incremented. • [4] Check if count < 10 • [5] If true, then return to point [2] If false,break from loop. 2 3 4

  12. Do-While Statement

  13. Do-While Statement

  14. Do-While Statement • while: • Condition is checked in the beginning of loop (pre-test loop). • do-while: • Condition is checked in the last of loop (post-test loop) • Contoh: while do-while

  15. For Statements • Syntax : for( initial-action ; loop-continuation-condition ; action-after-each-iteration ) { statement(s) (loop body) }

  16. For Statement • Flowchart: Initial-Action i = 0 Loop Continuation Condition? ( i < 100 ) ? false false true true Statement(s) (loop body) System.out.println(“Welcome to Java”); Action-After-Each-Iteration i++

  17. For Statement • Initial-action • Variable is initialized • Loop-continuation-condition • As boolean expression • True means loop is executed. • Its locations between initial-action and action-after-each-iteration divides by semicolon(;) • Action-after-each-iteration • Execute after looping • Usually in increment or decrement form. • Control Variable. • Starts with for(…;…;…) and ends without semicolon(;) except in special condition. • Curly block is needed when it has more than one statement.

  18. Do-While Statement 1 2 4 • Example : for ( int count=0 ; count < 10 ; count++ ) { System.out.println("Welcome to Java!"); } • Steps : • [1] count variable is initialized by 0 • [2] Check if count < 10. • [3] If true, then execute statements inside block. If false, break from loop. • [4] Count is incremented. • [5] Return to point [2]

  19. Do-While Statement Perulangan for

  20. Do-While Statement • For paramater’s can be left blank. • Example : for ( int count=0 ; count < 10 ; count++ ) { System.out.println("Welcome to Java!"); } Become to int count = 0; for ( ; count < 10 ; ) { System.out.println("Welcome to Java!"); count++ ; }

  21. Did You Know? for ( int count=0 ; count < 10 ; count++ ) { System.out.println("Welcome to Java!"); } Become to int count = 0; for ( ; ; ) { if(count < 10) { System.out.println("Welcome to Java!"); count++ ; } else break; }

  22. Did You Know? for ( int count=0 ; count < 10 ; count++ ) { System.out.println("Welcome to Java!"); } Become to for ( int count=0 ; count<10 ; System.out.println("Welcome to Java!"), count++); • Break is explained later in Jump Operation Lecture.

  23. Did You Know? • Looping forever = infinite loop • Due to logic error • Example : int count = 0; do { System.out.println(“Welcome to Java!”); } while(count < 10); • Application should be terminated forcefully.

  24. Advanced Learning • Delay is used for slowing or holding up the process. • Using loop with a large of number. • Example : for ( int i = 0 ; i < 2000000000 ; i++ ); • Semicolon(;) after the “for” means it is not executing anything after it. • How long the delay by using this method is depend on the spesification of the computer running it.

  25. Advanced Learning

  26. Exercise • Buatlah program iterasi

  27. answer for (int i=1;i<=angka;i++) { for(int j=1;j<=i;j++) { System.out.print("*"); } System.out.println(); } for (int i=angka;i>=1;i--) { for(int j=i;j>0;j--) { System.out.print("#"); } System.out.println(); } } } import java.util.Scanner; public class Iterasi { public static void main(String[] args) { int angka; Scanner input = new Scanner(System.in); System.out.println("Masukkan angka :"); angka=input.nextInt();

  28. References • Introduction to Java Programming. 7ed. Liang. 2009. p132-150 • Programming with Java. Julia. 2002. p240-248 • Java A Beginner’s Guide. 3ed. Herbert Schildt. 2005. p86-96 • Dasar Pemrograman Java 2. Abdul Kadir. 2004. Chapter 7 • The for Statement http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html • For Loop in Java http://www.roseindia.net/java/beginners/ForLoop.shtml

More Related