Escolar Documentos
Profissional Documentos
Cultura Documentos
PTIIK - UB
Statements
Repetition/Looping
while statements
Syntax:
while (boolean_exp) statement;
or
while(boolean_exp){
statement1;
statement2;
..
}
statements
boolean_ex
p
false
true
while statements
Example:
while(product <= 1000)
product = 2*product;
product=2*product;
true
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.
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)
continue;
System.out.println(x);
}
Exercise
Exercise
Input:
Berapakah kue untuk si Rakus?
Asumsi
Misalkan jawaban pertanyaan di atas adalah 5
Maka output-nya:
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
piringnya
5
4
3
2
1
kue,
kue,
kue,
kue,
kue,
memakan
memakan
memakan
memakan
memakan
1,
1,
1,
1,
1,
menyisakan
menyisakan
menyisakan
menyisakan
menyisakan
4
3
2
1
10
Submission
for statements
12
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 loopexpression is found to be false.
The presence of exp1, exp2, and exp3 are optional.
13
for statements
exp1
exp3
statements
exp2
true
false
14
for statements
Example:
for (x=1; x <= 10; x++)
System.out.println(x = + x);
x=1
x++
System.out.println(x = + x);
x<=1
00
true
false
15
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);
16
for statements
and exp3 may consist of more than one
expressions, separated by comma(s)
exp1
Examples:
int i,j;
for(i=1, j=30; i<j; i++, j--){
System.out.println(i + " -- " + j);
}
17
for statements
Infinite Loop
for(;;){
}
Contoh:
int i =
0;
for(;;){
i++;
printf("%d ", i);
if (i>=10)
break; // break out of the loop
}
18
exp1;
while(exp2){
statement1;
statement2;
.
exp3
}
equivalent
19
exp1;
while(exp2){
statement1;
statement2;
.
exp3
}
equivalent
Contoh:
int x=1;
while(x<=10){
System.out.println(x);
x++;
}
equivalent
int x;
for(x=1;x<=10;x++)
System.out.println(x);
20
Exercise
Input:
Berapakah kue untuk si Rakus?
Asumsi
Misalkan jawaban pertanyaan di atas adalah 5
Maka output-nya:
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
Si Rakus melihat
piringnya
5
4
3
2
1
kue,
kue,
kue,
kue,
kue,
memakan
memakan
memakan
memakan
memakan
1,
1,
1,
1,
1,
menyisakan
menyisakan
menyisakan
menyisakan
menyisakan
4
3
2
1
21