Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1

Kode Mata Kuliah – Nama Mata Kuliah

Loop
Bersarang

Ramdhan Nugraha, S.Pd., M.T.

Teknik Elektro – Fakultas Teknik Elektro


2

Nested Loops
Loops can be placed inside other loops
The break and continue statements
apply to the innermost enclosing while
or for statement

2
Example: rect.c 3

Print an m by n rectangle
of
asterisks

input width and height

for each row


{
for each column in the
current row
{
print an asterisk
}
3 start next row
Example:
#include <stdio.h> 4

/* Print an m-by-n rectangle of


rect.c (cont) asterisks */

int main()
{
int i, j, m, n;
Print an m by n rectangle of
asterisks printf("\nEnter width: ");
scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);

input width and height

for each row


{
for each column in the current row
{
print an asterisk
}
start next row return 0;
} }
4
Example: rect.c #include <stdio.h> 5
(cont) /* Print an m-by-n rectangle of
asterisks */

int main()
{
int i, j, m, n;
Print an m by n rectangle of
asterisks printf("\nEnter width: ");
scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);
input width and height
for (i=0; i < n; i++)
{

for each row


{
for each column in the current row
{
print an asterisk }
}
start next row
} return 0;
}
5
Example: #include <stdio.h> 6

rect.c (cont) /* Print an m-by-n rectangle of


asterisks */

int main()
{
Print an m by n rectangle of int i, j, m, n;
asterisks
printf("\nEnter width: ");
scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);
input width and height
for (i=0; i < n; i++)
{
for each row for (j=0; j < m; j++)
{ {
for each column in the current row
{
print an asterisk }
}
start next row }
}
return 0;
}
6
#include <stdio.h> 7

Example: rect.c /* Print an m-by-n rectangle of


asterisks */
(cont)
int main()
{
Print an m by n rectangle of int i, j, m, n;
asterisks
printf("\nEnter width: ");
scanf("%d", &m);
input width and height printf("\nEnter height: ");
scanf("%d", &n);

for (i=0; i < n; i++)


{
for each row for (j=0; j < m; j++)
{ {
for each column in the current row printf("*");
{ }
print an asterisk
} }
start next row
} return 0;
7 }
#include <stdio.h> 8

Example: rect.c /* Print an m-by-n rectangle of

(cont)
asterisks */

int main()
{
int i, j, m, n;
Print an m by n rectangle of
asterisks printf("\nEnter width: ");
scanf("%d", &m);
printf("\nEnter height: ");
input width and height scanf("%d", &n);

for (i=0; i < n; i++)


{
for (j=0; j < m; j++)
for each row {
{ printf("*");
for each column in the current row
}
{
print an asterisk printf("\n");
} }
start next row
} return 0;
8 }
#include <stdio.h> 9

Example: rect.c /* Print an m-by-n rectangle of

(cont)
asterisks */

int main()
{
int i, j, m, n;

Print an m by n rectangle of printf("\nEnter width: ");


asterisks scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);

for (i=0; i < n; i++)


input width and height {
for (j=0; j < m; j++)
{
for each row
{ printf("*");
for each column in the current row }
{ printf("\n");
print an asterisk }
}
start next row return 0;
} }
9
Example: rect.c #include <stdio.h> 10

(cont)
/* Print an m-by-n rectangle of
asterisks */

int main()
Print an m by n rectangle of {
asterisks int i, j, m, n;

algorithm program
printf("\nEnter width: ");
scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);
input width and height
for (i=0; i < n; i++)
{
for each row
{ for (j=0; j < m; j++)
for each column in the current row {
{ printf("*");
print an asterisk }
} printf("\n");
start next row }
}
return 0;
}
10
#include <stdio.h>
Variation: rect2.c
11

/* Print an m-by-n rectangle of


asterisks */
Print an m by n rectangle of
asterisks int main()
{
int i, j, m, n;

printf("\nEnter width: ");


input width and height
scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);
for each row
{ i = 0;
for each column in the current row while (i < n)
{ {
print an asterisk for (j=0; j < m; j++)
} {
start next row
printf("*");
}
}
printf("\n");
i++;
}
return 0;
11 }
#include <stdio.h>
Variation: rect3.c
12
/* Print an m-by-n rectangle of
asterisks */
int main()
Print an m by n rectangle of {
asterisks int i, j, m, n;

printf("\nEnter width: ");


scanf("%d", &m);
input width and height
printf("\nEnter height: ");
scanf("%d", &n);
for each row
{ for (i=0; i < n; i++)
for each column in the current row {
{ j = 0;
while (1)
print an asterisk {
printf("*");
}
j++;
start next row if (j == m)
} break;
}
printf("\n");
}
return 0;
}
#include <stdio.h> 13

Variation: rect3.c
/* Print an m-by-n rectangle of
asterisks */

(cont)
int main()
{
int i, j, m, n;

printf("\nEnter width: ");


scanf("%d", &m);
printf("\nEnter height: ");
scanf("%d", &n);

for (i=0; i < n; i++)


{
j = 0;
The innermost while (1)
{
enclosing loop printf("*");
j++;
for this break is if (j == m)
the while-loop }
break;

printf("\n");
}
return 0;
13 }
SOURCE 14

Deitel &Deitel
Chapter 3, Section 3.7
Chapter 4,
Sections 4.1 to 4.6
Sections 4.8 to 4.11

14
15

TUGAS

Buat program membuat segitiga fibonachi, dengan contoh input :


input nilai n = 7; input nilai n = 5;
maka outputnya : maka outputnya :
1 1
1 2 1 2
3 5 8 3 5
13

Dengan ketentuan :
Menggunakan for
BONUS +20 “Menggunakan while atau do..while”

You might also like