Top Banner
29

Iteration Statement Session 7

Dec 30, 2015

Download

Documents

garrison-burke

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Iteration Statement Session 7
Page 2: Iteration Statement Session 7

Iteration StatementSession 7

Course : T-0974 Algorithm & Object-Oriented Programming IYear : 2010

Page 3: Iteration Statement Session 7

Bina Nusantara

Learning Outcomes

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

Page 4: Iteration Statement Session 7

Bina Nusantara

Outline Materi

• While• Do-While• For• Nested

Page 5: Iteration Statement Session 7

Bina Nusantara

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)

Page 6: Iteration Statement Session 7

Bina Nusantara

While Statement

• Syntax:while (loop-continuation-condition){

statement(s);}

• Flowchart:

LoopContinuationCondition?

Statement(s)(loop body)

false

true

(count < 10)?

System.out.println(“Welcome to Java!”);count++;

false

true

count = 0;

Page 7: Iteration Statement Session 7

Bina Nusantara

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.

Page 8: Iteration Statement Session 7

Bina Nusantara

• 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]

1

2

4

While Statement

Page 9: Iteration Statement Session 7

Bina Nusantara

While Statement

Page 10: Iteration Statement Session 7

Bina Nusantara

Do-While Statement

• Syntax:do{

statement(s);} while (loop-continuation-condition);

• Flowchart:

LoopContinuationCondition?

Statement(s)(loop body)

false

true

count = 0;

(count < 10)?

System.out.println(“Welcome to Java!”);count++;

false

true

Page 11: Iteration Statement Session 7

Bina Nusantara

• 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

Page 12: Iteration Statement Session 7

Bina Nusantara

• 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.

1

2

43

Do-While Statement

Page 13: Iteration Statement Session 7

Bina Nusantara

Do-While Statement

Page 14: Iteration Statement Session 7

Bina Nusantara

Do-While Statement

Page 15: Iteration Statement Session 7

Bina Nusantara

• 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

Do-While Statement

Page 16: Iteration Statement Session 7

Bina Nusantara

For Statements

• Syntax :for( initial-action ; loop-continuation-condition ; action-after-each-iteration ){

statement(s)(loop body)

}

Page 17: Iteration Statement Session 7

Bina Nusantara

For Statement

Initial-Action

LoopContinuationCondition?

Statement(s)(loop body)

Action-After-Each-Iteration

i = 0

( i < 100 ) ?

System.out.println(“Welcome to Java”);

i++

false

true

• Flowchart:

false

true

Page 18: Iteration Statement Session 7

Bina Nusantara

• 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.

For Statement

Page 19: Iteration Statement Session 7

Bina Nusantara

• 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]

1 2 4

Do-While Statement

Page 20: Iteration Statement Session 7

Bina Nusantara

Perulangan for

Do-While Statement

Page 21: Iteration Statement Session 7

Bina Nusantara

• 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++ ;

}

Do-While Statement

Page 22: Iteration Statement Session 7

Bina Nusantara

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;}

Page 23: Iteration Statement Session 7

Bina Nusantara

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?

Page 24: Iteration Statement Session 7

Bina Nusantara

• 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.

Did You Know?

Page 25: Iteration Statement Session 7

Bina Nusantara

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.

Page 26: Iteration Statement Session 7

Bina Nusantara

Advanced Learning

Page 27: Iteration Statement Session 7

Exercise• Buatlah program iterasi

Bina Nusantara University 27

Page 28: Iteration Statement Session 7

answer

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();

Bina Nusantara University 28

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();

}

}}

Page 29: Iteration Statement Session 7

Bina Nusantara

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