Você está na página 1de 21

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
2

Repetition/Looping

Three forms of repetition statements:


while statements
for statements
do-while statements

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;

product <= 1000


fals
e

true

break dan continue

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

Deadline: 21 October 2012


To: ismiarta@gmail.com
Subject: [PROGDAS SI] Si Rakus
Content:
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 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

while and for

Equivalence of for and while:

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

equivalent

for(exp1; exp2; exp3){


statement1;
statement2;
.
}

19

while and for

Equivalence of for and while:

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

equivalent

for(exp1; exp2; exp3){


statement1;
statement2;
.
}

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

Você também pode gostar