Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

Loop

Ika Maulid Nur Ahmad, ST, MM

Kuliah Bahasa Pemrograman


Outline

 Review
 Loop Statements
 Infinite Loops
 Loop Breaking
 Nested Loops
Review
Control Flow Statement

1. Branch (if…else.., switch…case)


2. Loop / Repetition (for, while, do…while)
Loops

 Concept: do something again = repeat

 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”

int a=2, b=3, p = 1;


for(k=1;k<=b;k++)
{
p*=a;
}
Tracing a Loop

int i = 1; Round i Result


while(i<=3) 0 1
{ 1 1 Hi
System.out.print(“Hi”); 2 2 Hi
i++; 3 3 Hi
} 4 4
Tracing a Loop

int i = 1, sum=0; Round i sum


while(i<=3) 0 1 0
{ 1 1 1
sum+=i; 2 2 3
3 3 6
i++;
4 4
}
Tracing a Loop

int i = 4, fac=1; Round i fac


while(i>=1) 0
{ 1
fac*=i; 2
3
i--;
4
}
5
Tracing a Loop

int k; Round k Result


for(k=1;k<=3;k++) 0
{ 1 1 1
if(k%2==1) 2 2
System.out.print(k); 3 3 3
} 4 4
Tracing a Loop

int k; Round k Result


for(k=1;k<=3;k+=2) 0
{
1 1 1
System.out.print(k);
3 3 3
}
4 5
Tracing a Loop

int a=2, b=3, p = 1; R a b k p


for(k=1;k<=b;k++) 0
{ 1
p*=a; 2
3
}
4
Loops statement part#2
Pretest Loops

 “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

 Forget START or CONTINUE or STEP


 START, CONTINUE and STEP must be integer types
 Extra ; after while and for
 Forget ; after do…while
 Use , instead of ; in for
 Forget { }
Outline

 Review
 Loop Statements
 Infinite Loops
 Loop Breaking
 Nested Loops
Infinite Loops

 Infinite / forever / perpetual loops


 Loop that runs without stopping
 Causes
 Missing of START, CONTINUE or STEP
 Unordered START, CONTINUE and STEP
 Intention
 Etc.
 Generally, infinite loops should be prevented
Infinite Loops: Missing

int i = 1;
while(i<=10) Missing?

{
System.out.print(“Hi ”);
}
Infinite Loops: Missing

for(int i=1; i<=10 ; ) 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

 Some loops need a break before their STOP


 Infinite loops need a break
 Two commands:
 break
 Continue

 They are usually with selection statements (if)


break

 It breaks the current loop.


int i = -5; int i = -5;
while( i<=5) while( i<=5)
{ {
System.out.print(10.0/i); if(i==0)
i++; break;
} System.out.print(10.0/i);
i++;
Divided by zero!
}
break
int i = -5; int i = -5;
while( i<=5) while( i<=5)
{ {
if(i==0) if(i==0)
break; i=5;
System.out.print(10.0/i); else
i++; System.out.print(10.0/i);
} i++;
}
continue

 Start the next round of the current loop immediately.


 Skip the next statements in the loop.

for(int i=1;i<=10;i++) for(int i=1;i<=10;i++)


{ {
if(i==5)
System.out.print(i); continue;
}
System.out.print(i);
}
continue

for(int i=1;i<=10;i++) for(int i=1;i<=10;i++)


{ {
if(i==5) if(i!=5)
continue;
System.out.print(i);
System.out.print(i); }
}
“continue” is not frequently found. It
can be replaced by another simple code.
break vs. continue

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

 We can put a loop inside another loop.

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

 Loops within loops


 Loop is processed from inside to outside
 The total number of loops = multiplication of number of
each loop
 If there are two nested loops, think as row and column
Nested Loops

 Write codes to display


xxxxx
xxxxx int r;
xxxxx for(r=1;r<=10;r++)
xxxxx {
xxxxx
xxxxx
xxxxx System.out.println(“xxxxx”);
xxxxx }
xxxxx
xxxxx
Nested Loops

 Write codes to display


xxxxx int r, c;
xxxxx for(r=1;r<=10;r++)
xxxxx
{
xxxxx
xxxxx
for(c=1;c<=5;c++)
xxxxx {
xxxxx System.out.print(“x”);
xxxxx }
xxxxx System.out.println();
xxxxx
}
Contoh

 Menulis Hello World sebanyak 10x


 Menggunakan for statement :
#include <stdio.h>
int main()
{
int i;
for (i=0;i<10;i=i+1)
printf("Hello world!\n");
return 0;
}
 Menggunakan while statement :
#include <stdio.h>
int main()
{
int i;
i=1;
while (i<=10)
{
printf("Hello world!\n");
i=i+1;
}
return 0;
}
 Menggunakan Do While statement
#include <stdio.h>
int main()
{
int i;
i=1;
do
{
printf("Hello world!\n");
i=i+1;
}
while (i<=10);
return 0;
}
LATIHAN SOAL 1

Buat program untuk menampilkan berikut :


*
**
***
****
*****
#include <stdio.h>
int main()
{
int i, j;
for (i=1;i<=5;i++)
{
for (j=1;j<=i;j++)
printf("*");
printf("\n ");
}

}
Latihan soal 2

 Buat program menghitung jumlah deret s/d N bilangan


Jumlah = 1+2+3+…+N
#include <stdio.h>
int main()
{
int N;
int i;
int jumlah;
printf("Berapa N : "); scanf("%ld",&N);
jumlah = 0;
for (i=1;i<=N;i++)
{
jumlah = jumlah + i;
}
printf("Jumlah Deret = %ld \n", jumlah);
return 0;
}
Latihan soal 3

 Buat program untuk menghitung rata-rata dari sejumlah


data bilangan bulat
#include <stdio.h>
int main()
{
int batas, angka, jum, kounter;
float rata;
printf("Masukkan Jumlah integer didalam daftar : "); scanf("%d", &batas);
jum = 0;
kounter = 0;
printf("Masukkan %d buah integer. ", batas);
while (kounter < batas)
{
scanf("%d",&angka);
jum=jum+angka;
kounter++;
}
printf("Penjumlahan atas %d angka = %d \n", batas, jum);
if (kounter != 0)
{
rata = jum/kounter;
printf(" rata-rata = %f", rata);
}

else printf("Tidak Ada Masukkan");

}
TUGAS

 Write codes to display


12345 11111 1 1
1234 1111 11 12
123 111 111 123
12 11 1111 1234
1 1 11111 12345

You might also like