Professional Documents
Culture Documents
Bab 5 Loop Bersarang - Compressed
Bab 5 Loop Bersarang - Compressed
Loop
Bersarang
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
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);
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++)
{
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
(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);
(cont)
asterisks */
int main()
{
int i, j, m, n;
(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
Variation: rect3.c
/* Print an m-by-n rectangle of
asterisks */
(cont)
int main()
{
int i, j, m, n;
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
Dengan ketentuan :
Menggunakan for
BONUS +20 “Menggunakan while atau do..while”