Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.
-
Upload
julian-copsey -
Category
Documents
-
view
223 -
download
2
Transcript of Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB 1.
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
Three forms of repetition statements: while statements for statements do-while statements
3
Repetition/Looping
Syntax:while (boolean_exp) statement;
or
while(boolean_exp){
statement1;
statement2;
..
}
4
boolean_expboolean_exp
statementsstatements
true
false
while statements
Example:
while(product <= 1000)
product = 2*product;
5
product <= 1000
product=2*product;
false
true
while statements
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
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
Example:
8
int x; for(x=1; x<=10; x++) { if (x == 5)
continue; System.out.println(x); }
continue
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;
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
Submission
Deadline: 21 October 2012To: [email protected]: [PROGDAS SI…] Si RakusContent: Nama: NIM: Kelas: Table Source code
11
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
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
14
exp1
exp3
statements
exp2true
false
exp1
exp3
statements
exp2
for statements
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
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
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
Infinite Loopfor(;;){
…
}
Contoh:
18
int i = 0; for(;;){
i++; printf("%d ", i);
if (i>=10) break; // break out of the loop
}
for statements
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
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++; }
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