Download presentation
1
Control Flow Statements: Repetition/Looping
Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB
2
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
3
Repetition/Looping Three forms of repetition statements:
while statements for statements do-while statements
4
while statements Syntax: while (boolean_exp) statement; or
.. } statements true boolean_exp false
5
while statements Example: while(product <= 1000)
product = 2*product; product=2*product; product <= 1000 true false
6
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.
7
break Example: int x = 1; while(x<=10){ System.out.println(x); x++;
if (x>5) break; } Exit from the loop
8
continue Example: int x; for(x=1; x<=10; x++) { if (x == 5)
System.out.println(x); }
9
Exercise
10
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
11
Submission Deadline: 21 October 2012 To: Subject: [PROGDAS SI…] Si Rakus Content: Nama: NIM: Kelas: Table Source code
12
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) { … }
13
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.
14
for statements true false exp1 exp3 statements exp2 exp1 exp3
15
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
16
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);
17
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); }
18
for statements Infinite Loop for(;;){ … } Contoh: int i = 0; for(;;){
printf("%d ", i); if (i>=10) break; // break out of the loop }
19
while and for Equivalence of for and while:
exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent
20
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
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.