# Control Flow Statements: Repetition/Looping

## Presentation on theme: "Control Flow Statements: Repetition/Looping"— Presentation transcript:

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

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

Repetition/Looping Three forms of repetition statements:
while statements for statements do-while statements

while statements Syntax: while (boolean_exp) statement; or
.. } statements true boolean_exp false

while statements Example: while(product <= 1000)
product = 2*product; product=2*product; product <= 1000 true false

break dan continue break continue
Exit from decision-making (switch) or repetition (for, while dan do-while) continue Skips the current iteration of a for, while and do-while.

break Example: int x = 1; while(x<=10){ System.out.println(x); x++;
if (x>5) break; } Exit from the loop

continue Example: int x; for(x=1; x<=10; x++) { if (x == 5)
System.out.println(x); }

Exercise

Exercise Input: Asumsi Maka output-nya:
Berapakah kue untuk si Rakus? … Asumsi Misalkan jawaban pertanyaan di atas adalah 5 Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

Submission Deadline: 21 October 2012 To: Subject: [PROGDAS SI…] Si Rakus Content: Nama: NIM: Kelas: Table Source code

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) { }

for statements for (exp1; exp2; exp3) statement; exp1: initialization-expression exp2: 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.

for statements true false exp1 exp3 statements exp2 exp1 exp3

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

for statements Examples: 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 Examples:
exp1 and exp3 may consist of more than one expressions, separated by comma(s) Examples: int i,j; for(i=1, j=30; i<j; i++, j--){ System.out.println(i + " -- " + j); }

for statements Infinite Loop for(;;){ … } Contoh: int i = 0; for(;;){
printf("%d ", i); if (i>=10) break; // break out of the loop }

while and for Equivalence of for and while:
exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent

while and for Equivalence of for and while: Contoh:
exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent int x=1; while(x<=10){ System.out.println(x); x++; } int x; for(x=1;x<=10;x++) System.out.println(x); equivalent

Exercise Input: Asumsi Maka output-nya:
Berapakah kue untuk si Rakus? … Asumsi Misalkan jawaban pertanyaan di atas adalah 5 Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya