290 likes | 392 Views
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
E N D
Iteration StatementSession 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 • Nested
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)
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++;
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.
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
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
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.
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
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
For Statements • Syntax : for( initial-action ; loop-continuation-condition ; action-after-each-iteration ) { statement(s) (loop body) }
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++
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.
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]
Do-While Statement Perulangan for
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++ ; }
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; }
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.
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.
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.
Exercise • Buatlah program iterasi
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();
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