+ All Categories
Home > Documents > Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Date post: 31-Mar-2015
Category:
Upload: julian-copsey
View: 221 times
Download: 2 times
Share this document with a friend
Popular Tags:
21
Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1
Transcript
Page 1: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Pemrograman Dasar

Control Flow Statements:Repetition/Looping

PTIIK - UB

1

Page 2: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Statements Control flow statements regulate the order in which statements get

executed. Kinds of control flow statements:

Decision making: if-then statement if-then-else statement, switch-case statement

Repetition/looping: while statement for statement do-while statement

Branching statement: break statement continue statement return statement

2

Page 3: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Three forms of repetition statements: while statements for statements do-while statements

3

Repetition/Looping

Page 4: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Syntax:while (boolean_exp) statement;

or

while(boolean_exp){

statement1;

statement2;

..

}

4

boolean_expboolean_exp

statementsstatements

true

false

while statements

Page 5: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Example:

while(product <= 1000)

product = 2*product;

5

product <= 1000

product=2*product;

false

true

while statements

Page 6: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

break Exit from decision-making (switch) or repetition (for, while dan do-while)

continue Skips the current iteration of a for, while and do-while.

6

break dan continue

Page 7: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Example:

int x = 1; while(x<=10){ System.out.println(x); x++; if (x>5)

break; }

7

Exit from the loopExit from the loop

break

Page 8: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Example:

8

int x; for(x=1; x<=10; x++) { if (x == 5)

continue; System.out.println(x); }

continue

Page 9: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Exercise

9

int X2, Nilai, Rata-rata; float K, RND, Total;

String Nama, KodeMK, NamaKota, Fakultas;

No Pernyataan (statements ) B/S Alasan (jika salah)1 X2 = 3;

2 Nama = ”Amanda”;

3 3K = 8.71;

4 Nilai%3 = 3.16;

5 Rata-rata = 14;

6 KodeMK = CS512;

7 NamaKota = ”Malang”;

8 Fakultas = MIPA;

9 RND = 5.12 * N;

10 %Total = 71.8;

Page 10: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Exercise

Input:Berapakah kue untuk si Rakus? …

AsumsiMisalkan jawaban pertanyaan di atas adalah 5

Maka output-nya:Si Rakus melihat 5 kue, memakan 1, menyisakan 4Si Rakus melihat 4 kue, memakan 1, menyisakan 3Si Rakus melihat 3 kue, memakan 1, menyisakan 2Si Rakus melihat 2 kue, memakan 1, menyisakan 1Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

10

Page 11: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Submission

Deadline: 21 October 2012To: [email protected]: [PROGDAS SI…] Si RakusContent: Nama: NIM: Kelas: Table Source code

11

Page 12: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

for statements

You use the basic for statement to loop over a range of values from beginning to end.

for (initialization-expression; loop-expression; update-expression)

statement;

//or

for (initialization-expression; loop-expression; update-expression) {

statement;

}

12

Page 13: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

for (exp1; exp2; exp3)

statement;

exp1: initialization-expressionexp2: loop-expression exp3: update-expression

The initialization-expression allows you to declare and/or initialize loop variables, and is executed only once.

Then the loop-expression of boolean or Boolean type is evaluated and if it is true the statement in the body of the loop is executed.

After executing the loop body, the update-expression is evaluated, usually to update the values of the loop variables, and then the loop-expression is reevaluated. This cycle repeats until the loop-expression is found to be false.

The presence of exp1, exp2, and exp3 are optional.13

for statements

Page 14: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

14

exp1

exp3

statements

exp2true

false

exp1

exp3

statements

exp2

for statements

Page 15: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

15

Example: for (x=1; x <= 10; x++) System.out.println(“x =” + x);

x = 1

x++

System.out.println(“x =” + x);

x<=100true

false

for statements

Page 16: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Examples:

16

int x;for(x=1; x<=10; x++) System.out.println(x);

int x; for(x=10; x>=1; x--) System.out.println(x);

for statements

Page 17: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

exp1 and exp3 may consist of more than one expressions, separated by comma(s)

Examples:

17

int i,j; for(i=1, j=30; i<j; i++, j--){ System.out.println(i + " -- " + j); }

for statements

Page 18: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Infinite Loopfor(;;){

}

Contoh:

18

int i = 0; for(;;){

i++; printf("%d ", i);

if (i>=10) break; // break out of the loop

}

for statements

Page 19: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Equivalence of for and while:

19

exp1;while(exp2){ statement1; statement2; …. exp3}

exp1;while(exp2){ statement1; statement2; …. exp3}

for(exp1; exp2; exp3){ statement1; statement2; ….}

for(exp1; exp2; exp3){ statement1; statement2; ….}

equivalentequivalent

while and for

Page 20: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Equivalence of for and while:

Contoh:

20

int x; for(x=1;x<=10;x++) System.out.println(x);

exp1;while(exp2){ statement1; statement2; …. exp3}

exp1;while(exp2){ statement1; statement2; …. exp3}

for(exp1; exp2; exp3){ statement1; statement2; ….}

for(exp1; exp2; exp3){ statement1; statement2; ….}

equivalentequivalent

equivalent

while and for

int x=1; while(x<=10){ System.out.println(x); x++; }

Page 21: Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.

Exercise

Input:Berapakah kue untuk si Rakus? …

AsumsiMisalkan jawaban pertanyaan di atas adalah 5

Maka output-nya:Si Rakus melihat 5 kue, memakan 1, menyisakan 4Si Rakus melihat 4 kue, memakan 1, menyisakan 3Si Rakus melihat 3 kue, memakan 1, menyisakan 2Si Rakus melihat 2 kue, memakan 1, menyisakan 1Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

21


Recommended