Professional Documents
Culture Documents
Day 6 Perulangan - Looping
Day 6 Perulangan - Looping
Review
Loop Statements
Infinite Loops
Loop Breaking
Nested Loops
Review
Control Flow Statement
Components of loops
START
STOP/CONTINUE
STEP
Loop Statements
while
for
do…while
goto
while
i=1
int i = 1;
while(i<=10)
Yes No
i<=10 ? {
System.out.print(“Hi ”);
PRINT “Hi” i++;
}
i++
FOR
i=1
for(int i=1; i<=10 ; i++)
Yes No {
i<=10?
System.out.print(“Hi”);
}
PRINT “Hi”
i++
while for
int i = 1;
for(int i=1; i<=10 ; i++)
while(i<=10)
{
{
System.out.print(“Hi”);
System.out.print(“Hi”);
}
i++;
}
Change to “for”
int i = 4, fac=1;
while(i>=1)
{
fac*=i;
i--;
}
Change to “while”
“while , for”
Check BEFORE repeat START
Yes No
CONTINUE ?
STATEMENTS
STEP
Posttest Loops
“do…while”
START
Check AFTER repeat
STATEMENTS
STEP
Yes
CONTINUE ?
No
Pretest and Posttest Loops
START START
Yes No STATEMENTS
CONTINUE ?
STEP
STATEMENTS
Yes No
CONTINUE ?
STEP
do…while
START START;
do
STATEMENTS {
STATEMENTS;
STEP
STEP;
Yes No
CONTINUE ? }
while(CONTINUE);
do…while
i=1 int i = 1;
do
PRINT i {
System.out.print(i);
i++
i++;
Yes No
i<=10 ? } while(i<=10);
Pretest and Posttest Loops
int i = 1; int i = 1;
while(i<=10) do
{ {
System.out.print(“Hi ”); System.out.print(“Hi ”);
i++; i++;
} } while(i<=10);
Common Loops’ Errors
Review
Loop Statements
Infinite Loops
Loop Breaking
Nested Loops
Infinite Loops
int i = 1;
while(i<=10) Missing?
{
System.out.print(“Hi ”);
}
Infinite Loops: Missing
{
System.out.print(“Hi ”);
}
Infinite Loops: Unordered
int i = 1; int i = 1;
while( i<=10) while( i>0)
{ {
System.out.print(“Hi ”); System.out.print(“Hi ”);
i--; i++;
} }
Infinite Loops: Intention
for(; ;) while(1)
{ {
System.out.print(“Hi ”); System.out.print(“Hi ”);
}
}
do
{
System.out.print(“Hi ”);
} while(1);
Outline
Review
Loop Statements
Infinite Loops
Loop Breaking
Nested Loops
Loop Breaking
int i; int i;
for(i=-5;i<=5;i++) for(i=-5;i<=5;i++)
{ {
if(i==0) if(i==0)
break; continue;
System.out.print(10.0/i); System.out.print(10.0/i);
} }
stop when i = 0 skip when i = 0
continue + while or do…
while
int i=1;
for(int i=1;i<=10;i++)
while(i<=10)
{
{
if(i==5)
if(i==5)
continue;
{
System.out.print(i);
i++;
}
continue;
Be careful that the “continue” in }
“while” or “do…while” loop must be System.out.print(i);
after the STEP. Otherwise, the loop
will be infinite. i++;
}
for(int i=1;i<=10;i++) int i=1;
{ do
statement; {
continue; statement;
statement; i++;
} continue;
statement;
int i=1; } while(i<=10);
while(i<=10)
{
statement; “continue” makes the program jump
to the CONTINUE condition
i++; immediately and skip all statements
after it.
continue;
statement;
}
Outline
Review
Loop Statements
Infinite Loops
Loop Breaking
Nested Loops
Nested Loops
int a= 1, b = 5;
Result?
while(a<=3)
{
while(b>=1)
{
System.out.println(a+ “ “+b);
b--;
}
a++;
}
int a = 1, b = 5;
while(a<=3) {
while(b>=1) {
System.out.println(a+“ ”+b);
b--;
}
a++;
} Same result?
???
int a, b;
for(a=1;a<=3;a++) {
for(b=5;b>=1;b--) {
System.out.println(a+“ ”+b);
}
}
START
No Yes
CONTINUE ?
START
No
CONTINUE ?
Yes
STATEMENTS
STEP
STEP
Nested Loop
}
Latihan soal 2
}
TUGAS