Iteration Statement Session 7

Post on 30-Dec-2015

21 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

Transcript

Iteration StatementSession 7

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

Bina Nusantara

Learning Outcomes

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

Bina Nusantara

Outline Materi

• While• Do-While• For• Nested

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)

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;

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.

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

Bina Nusantara

While Statement

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

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

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

Bina Nusantara

Do-While Statement

Bina Nusantara

Do-While Statement

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

Bina Nusantara

For Statements

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

statement(s)(loop body)

}

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

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

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

Bina Nusantara

Perulangan for

Do-While Statement

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

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

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?

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?

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.

Bina Nusantara

Advanced Learning

Exercise• Buatlah program iterasi

Bina Nusantara University 27

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

}

}}

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

top related