-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05.1-perulangan-for.php
More file actions
65 lines (46 loc) · 1.13 KB
/
05.1-perulangan-for.php
File metadata and controls
65 lines (46 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
for ($i = 0; $i < 100; $i++) {
echo "Saya berjanji tidak akan mengulangi <br>";
}
echo "<hr>";
# inisialisasi variabel tidak harus dari angka 0
for ($i = 1; $i <= 10; $i++) {
echo "Perulangan ke-{$i} <br>";
}
echo "<hr>";
for ($x = 1; $x < 11; $x++) {
echo "Nilai x = {$x} <br>";
}
echo "<hr>";
for ($i = 2; $i < 100; $i += 2) {
echo "Nilai i = {$i} <br>";
}
echo "<hr>";
for ($i = 10; $i > 0; $i--) {
echo "Nilai i = {$i} <br>";
}
echo "<hr>";
# kode program ini akan menampilkan * dalam bentuk persegi
# dengan ukuran 5x5
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < 5; $j++) {
echo "* ";
}
echo "<br>";
}
echo "<hr>";
$listMahasiswa = ['Nurul Huda', 'Wahid Abdullah', 'Elmo Bachtiar'];
for ($i = 0; $i < count($listMahasiswa); $i++) {
echo "Nama: {$listMahasiswa[$i]} <br>";
}
echo "<hr>";
# inisialisasi variabel tidak harus dari angka 0
for ($i = 1; $i <= 50; $i++) {
if ($i % 10 === 0) {
continue; # skip perulangan jika nilai $i habis dibagi 10
}
echo "Perulangan ke-{$i} <br>";
if ($i > 40) {
break; # hentikan perulangan jika $i lebih dari 20
}
}