Skip to content

Instantly share code, notes, and snippets.

@Muhammad-Ikhwan-Fathulloh
Created November 9, 2024 04:51
Show Gist options
  • Save Muhammad-Ikhwan-Fathulloh/f9bdab9874e73eb5b658489736707d75 to your computer and use it in GitHub Desktop.
Save Muhammad-Ikhwan-Fathulloh/f9bdab9874e73eb5b658489736707d75 to your computer and use it in GitHub Desktop.
// While
console.log("Kasus 1")
let count = 0
// Kondisi (jika benar maka proses dijalankan)
// Kondisi (jika salah maka proses diberhentikan)
while (count <= 6) {
console.log("Sebelum "+count)
count++ //increment variabel count +1
console.log("Sesudah "+count)
console.log(count <= 6)
}
console.log("Kasus 2")
let statusProcess = true
// jika kondisi ini bernilai true terus,
// maka akan terjadi infinite loop
while (statusProcess) {
console.log("Status "+statusProcess)
statusProcess = false
console.log("Status "+statusProcess)
}
console.log("Kasus 3")
// logic menentukan kondisi while benar(true)/salah(false)
let scoreA = 10
let scoreB = 50
let score = scoreA + scoreB
let statusScore = true
// && = AND
// || = OR
// != = NOT
console.log(score <= 50 || statusScore)
while (score <= 50 || statusScore) { // kondisi
console.log("Score "+score)
statusScore = false
}
// Do While
console.log("Kasus 1")
let count = 6;
do { // Jalankan dulu prosesnya sekali
console.log("Angka " + count)
count++
} while (count <= 5) // cek kondisi benar/salah
console.log("Kasus 2")
let message = "Selamat Datang"
let number = 1
do {
console.log(message)
if (number <= 3) {
console.log("Angka "+number)
}
number++
} while (number <= 3)
// Nested Loop
console.log("Kasus 1")
for (var i = 1; i < 3; i++) {
for (var j = 1; j < 4; j++) {
console.log("Loop dalam: "+j+" Iterasi Luar "+i)
}
console.log("Loop Luar: "+i)
}
console.log("Kasus 2")
let numbers = ["Senin","Selasa","Rabu","Kamis","Jum'at", "Sabtu"]
let activities = ["Mandi", "Sekolah", "Tidur", "Makan"]
console.log(numbers.length)
console.log(activities.length)
for (let index = 0; index < numbers.length; index++) {
console.log("Hari: "+numbers[index])
for (var j = 0; j < 4; j++) {
console.log("Aktivitas: "+activities[j])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment