10.5 Nested loop 6.3 & 7.3 NESTED LOOP 255
10.5 NESTED LOOP 255
? - Pendahuluan #include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { - } J 1 2 3 4 5 6 - Loop Berapa kali loop dikerjakan ? 255
Pendahuluan int J; for ( J=1; J<=5; J++) { - } - J<=5 J<=5 false J<=5 true - true - - false J++ J++ Atau J=1 J++1 - J<=5 true false
#include<stdio.h> main() { int J; J = 1; while ( J<=5 ) { - Pendahuluan J<=5 J=1 J=J+1 false true - #include<stdio.h> main() { int J; J = 1; while ( J<=5 ) { - J++; } Atau J<=5 false true J=1 J=J+1 - - Loop
Pendahuluan #include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { - } #include<stdio.h> main() { int J; J = 1; while ( J<=5 ) { - J++; }
#include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { Pendahuluan #include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { printf(“Jakarta”); } #include<stdio.h> main() { int J; J=1; while ( J<=5 ) { printf(“Jakarta”); J++; } Tercetak: JakartaJakartaJakartaJakartaJakarta
Pendahuluan #include<stdio.h> main() { int J; J = 1; while ( J<=5 ) { printf(“\nJakarta”); J++; } #include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { printf(“\nJakarta”); } Tercetak: Jakarta Tercetak: Jakarta
Pendahuluan #include<stdio.h> main() { int J; J = 1; while ( J<=5 ) { printf(“\n%i”, J ); J++; } #include<stdio.h> main() { int J; for ( J=1; J<=5; J++) { printf(“\n%i”, J ); } Tercetak : 1 2 3 4 5 Tercetak : 1 2 3 4 5
6.3 Nested Loop dengan for( ) 6.3.1 Pemahaman Nested Loop #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } (A) for ( J=1; J<=5; J++) { } - for ( J=1; J<=5; J++) { } (B) -
printf(“\nJakarta”) printf(“\nJakarta”) for ( J=1; J<=5; J++) { } #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } printf(“\nJakarta”) for ( J=1; J<=5; J++) { } Tercetak : Jakarta printf(“\nJakarta”) Tercetak : Jakarta - 15 kali 258
? ? #include<stdio.h> main() { int J; for(J=1; J<=5; J++) { printf(“\n%i”, J); } ? 6.15 Tercetak : ? 258
? #include<stdio.h> main() {int J; for(J=1; J<=5; J++) { 6.15 ? #include<stdio.h> main() {int J; for(J=1; J<=5; J++) { printf(“\n%i”, J); } Tercetak : 1 2 3 4 5
? ? #include<stdio.h> void main() {int I, J; for(I=1; I<=3; I++) {for(J=1; J<=5; J++) { printf(“\n%i”,J); } 6.15 disebut : Loop 3 x 5 ? Tercetak :
#include<stdio.h> void main() {int I, J; for(I=1; I<=3; I++) {for(J=1; J<=5; J++) { printf(“\n%i”,J); } I J 1 2 3 4 5 Tercetak : 1 2 3 4 5
6.15 I J I J 1 2 3 1 2 3 4 5 1 2 3 1 2 3 4 5 258
#include<stdio.h> void main() {int I, J; I=1; while( I<=3 ) while ( J<=5 ) { printf(“\n%i”, J); J++; } I++; Tercetak : I J 1 2 3 4 5 1 2 3 4 5 258
#include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { Perhatikan kembali Nested Loop sebelumnya sebagai berikut ini : I=1 #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } I<=3 false true J=1 for ( J=1; J<=5; J++) { } J<=5 false Outer loop true Outer loop - Inner loop Inner loop J++ I++ 258 keluar
I=1 I<=3 J=1 J<=5 J++ I++ #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } I=1 I<=3 true J=1 for ( J=1; J<=5; J++) { } false J<=5 true - Outer loop Inner loop false J++ I++ 258 keluar
printf(“\nJakarta”); 6.3.2 Contoh Penggunaan Nested Loop #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } Tercetak : Jakarta for ( J=1; J<=5; J++) { } printf(“\nJakarta”); 258
#include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } Tercetak : 1 2 3 4 5 for ( J=1; J<=5; J++) { } printf(“\n%i”, J); 258
printf(“%3i”, J ); printf(“%3i”, J ); #include<stdio.h> main() { int I, J; I=1; while( I<=3) { I++; } #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } J=1; while ( J<=5) { J++; } for ( J=1; J<=5; J++) { } printf(“%3i”, J ); printf(“%3i”, J ); Tercetak : 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 258
? ? #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { } for ( J=1; J<=5; J++) printf(“%3i”, J ); #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { printf(“\n”); } for ( J=1; J<=5; J++) { } printf(“%3i”, J ); Tercetak : 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ? ? Tercetak : Tercetak : 258
#include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { printf(“\n”); } 1 2 3 1 2 3 4 5 for ( J=1; J<=5; J++) { } printf(“%3i”, J ); Tercetak : 1 2 3 4 5 Loop 3 x 5 Hal: 257 258
? #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) for(J = 1; J<=5; J++) { printf(“%3i”, I ); } printf(“\n”); } nyetak I ? ? Tercetak : 259
#include<stdio.h> void main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, I ); } printf(“\n”); } 1 2 3 1 2 3 4 5 Tercetak : 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 Hal: 258 259
? ? #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, I+J ); } printf(“\n”); } ? Tercetak : 259
I J I+J #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, I+J ); } printf(“\n”); } 1 2 3 1 2 3 4 5 2 3 4 5 6 7 8 Tercetak : 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
Susun program untuk mencetak nilai- nilai ( 1- 15) sehingga tercetak sebagai berikut : a. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 b. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hal. 266 Soal-04. g d. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hal. 266 Soal-04.k 266
a. { int I; for ( I=1; I<=15; I++) { printf(“%3i”, I ); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { int I; for ( I=1; I<=15; I++) { printf(“%3i”, I ); } } I N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 atau : { int I,N; N=1; for ( I=1; I<=15; I++) { printf(“%3i”, N ); N++; }
b. { int I; for ( I=1; I<=15; I++) { printf(“\n%i”, I ); } } 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { int I; for ( I=1; I<=15; I++) { printf(“\n%i”, I ); } } I N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 atau : { int I,N; N=1; for ( I=1; I<=15; I++) { printf(“\n%i”, N ); N++; }
? a. Dengan menggunakan Nested Loop I J Tercetak 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) printf(“%3i”, ………………. ); } ?
a. #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) Menggunakan Nested Loop I J (I-1)*5+J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) printf(“%3i”, ( I-1 )*5 + J ); }
b. Menggunakan Nested Loop I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) printf(“\n%i”, ( I-1 )*5 + J ); }
c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I J (I-1)*5+J 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Dengan Nested Loop #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, (I-1)*5+J ); } printf(“\n”); }
c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I J N 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Dengan Nested Loop #include<stdio.h> main() { int I, J, N; N = 1; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, N ); N++; } printf(“\n”); Hal : 259
c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int I, J, N; N = 1; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, N ); N++; } printf(“\n”); int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”,(I-1)*5+J ); } printf(“\n”); } Hal : 259
#include<stdio.h> main() { int I, X; X = 0; for ( I=1; I<=15; I++) { printf(“%3i”, I ); X++; if (X=5) { printf(“\n”); } I X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 Tercetak : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio.h> main() { int I; for ( I=1; I<=15; I++) { printf(“%3i”, I ); if (I%5 == 0) printf(“\n”); } I I%5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 Tercetak : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int I, J, N; N = 1; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”, N ); N++; } printf(“\n”); int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=5; J++) { printf(“%3i”,(I-1)*5+J ); } printf(“\n”); } d. SOAL : Susun program untuk mencetak : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
d. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I 1 2 3 4 5 J 1 2 3 (I-1)*3 + J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int I, J; for ( I=1; I<=5; I++) { for(J = 1; J<=3; J++) { printf(“%3i”,(I-1)*3+J ); } printf(“\n”); } Loop 5 x 3
d. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I 1 2 3 4 5 J 1 2 3 N 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int I, J, N; N = 1; for ( I=1; I<=5; I++) { for(J = 1; J<=3; J++) { printf(“%3i”,N ); N++; } printf(“\n”);
d. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int I, J, N; N = 1; for ( I=1; I<=5; I++) { for(J = 1; J<=3; J++) { printf(“%3i”, N ); N++; } printf(“\n”); int I, J; for ( I=1; I<=5; I++) { for(J = 1; J<=3; J++) { printf(“%3i”,(I-1)*3+J ); } printf(“\n”); }
? ? #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J=1; J<=5; J++) printf(“\n%i”, J); } ? ? Tercetak : disebut : Loop 3 x 5
#include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J=1; J<=5; J++) printf(“\n%i”, J); } Tercetak : I J 1 2 3 4 5 1 2 3 4 5
I J I J 1 2 3 1 2 3 4 5 1 2 3 1 2 3 4 5
? ? #include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J=I; J<=5; J++) printf(“\n%i”, J); } ? ? Tercetak : disebut : Loop 3 x 5
#include<stdio.h> main() { int I, J; for ( I=1; I<=3; I++) { for(J=I; J<=5; J++) printf(“\n%i”, J); } Tercetak : I J 1 2 3 4 5 1 2 3 1 2 3 4 5
a. b. SOAL : Apa yang tercetak oleh program berikut ini : int I, J; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { printf(“%3i”, J ); } printf(“\n”); } int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=I; J++) { printf(“%3i”, J ); } printf(“\n”); } Tercetak : ? Tercetak : ?
a. 1 2 3 4 5 2 3 4 5 3 4 5 int I, J; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { printf(“%3i”, J ); } printf(“\n”); } I 1 2 3 J 1 2 3 4 5 I 1 2 3 J 1 2 3 4 5 Tercetak : 1 2 3 4 5 2 3 4 5 3 4 5 Hal : 258
b. 1 1 2 1 2 3 int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=I; J++) { printf(“%3i”, J ); } printf(“\n”); } I 1 2 3 J 1 2 3 Tercetak : 1 1 2 1 2 3
c. d. SOAL : Apa yang tercetak oleh program berikut ini : int I, J; for ( I=1; I<=3; I++) { for(J = 1; J<=6-I; J++) { printf(“%3i”, J ); } printf(“\n”); } int I, J; for ( I=1; I<=3; I++) { for(J = I; J<=6-I; J++) { printf(“%3i”, J ); } printf(“\n”); } Tercetak : ? Tercetak : ?
c. 1 2 3 4 5 1 2 3 4 1 2 3 int I, J; I J for ( I=1; I<=3; I++) 1 1 { for(J = 1; J<=6-I; J++) { printf(“%3i”, J ); } printf(“\n”); } I 1 2 3 J 1 2 3 4 5 Tercetak : 1 2 3 4 5 1 2 3 4 1 2 3
d. 1 2 3 4 5 2 3 4 3 I J int I, J; 1 1 for ( I=1; I<=3; I++) 2 { 3 for(J = I; J<=6-I; J++) { printf(“%3i”, J ); } printf(“\n”); } Tercetak : 1 2 3 4 5 2 3 4 3
? ? #include<stdio.h> main() { int I, J; char C = ‘A’; for ( I=1; I<=3; I++) { for(J=1; J<=5; J++) printf(“%c”, C); C++; } ? ? Tercetak : disebut : Loop 3 x 5
{ int I, J; char C = ‘A’; for ( I=1; I<=3; I++) { for(J=1; J<=5; J++) printf(“ %c”, C); C++; } I J 1 2 3 4 5 Tercetak : A B C D E F G H I J K L M N O
I J I J 1 2 3 1 2 3 4 5 1 2 3 1 2 3 4 5
1. int A = 25; printf(“%i”, A+2 ); 27 3. int A = 25; printf(“%i”, A++ ); 25 Nilai A tidak berubah 25 * Cetak nilai A * A ditambah 1 26 A A 2. int A = 25; printf(“%i”, A=A+2 ); 27 4. int A = 25; printf(“%i”, ++A ); 26 27 * A ditambah 2 * Cetak nilai A 26 * A ditambah 1 * Cetak nilai A A A
SOAL : Susun program untuk mencetak nilai 1-15, sehingga tercetak sebagai berikut : a. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 b. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 c. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 d. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
a. b. SOAL : Apa yang tercetak bila program berikut ini dijalankan : #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; printf(“%3i”, T ); } printf(“\n”); #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T ); printf(“\n”);
a. SOAL : Apa yang tercetak bila program berikut ini dijalankan : #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T );
a. b. SOAL : Apa yang tercetak bila program berikut ini dijalankan : #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; printf(“%3i”, T ); } printf(“\n”); #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T ); printf(“\n”);
c. #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T );
a. #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; printf(“%4i”, T ); } printf(“\n”); I 1 2 3 J 1 2 3 4 5 T=0 1 3 6 10 15 17 20 24 29 32 36 41 Tercetak : 1 3 6 10 15 17 20 24 29 32 36 41
b. #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T ); printf(“\n”); I 1 2 3 J 1 2 3 4 5 T=0 1 3 6 10 15 17 20 24 29 32 36 41 Tercetak : 15 29 41
I 1 2 3 J 1 2 3 4 5 T=0 1 3 6 10 15 17 20 24 29 32 36 41 c. #include<stdio.h> main() { int I, J, T; T = 0; for ( I=1; I<=3; I++) { for(J = I; J<=5; J++) { T = T + J; } printf(“%3i”, T ); Tercetak : 41