Else If -Rakenteen Käyttäminen Useisiin Ehtoihin
Nyt tarkastellaan tilannetta, jossa useita ehtoja tulee mukaan kuvaan:
1234567891011121314151617let a = 11; if (a > 15) { console.log('greater than 15'); } if (a > 10) { console.log('greater than 10'); } if (a > 5) { console.log('greater than 5'); } if (a > 0) { console.log('greater than 0'); }
Tässä esimerkissä muuttuja a on suurempi kuin 10, mutta myös muut viestit kuten "greater than 5" ja "greater than 0" tulostuvat. Tämä ei ole toivottu käyttäytyminen, kun haluat suorittaa vain yhden ehdon.
else-lause ei toimisi hyvin tässä useiden ehtojen vuoksi.
else if -rakenteen esittely
else if -rakenne tarjoaa ratkaisun tietyn koodilohkon valitsemiseen ehtosarjassa:
if (condition) {
// First `if` code block
} else if (condition) {
// First `else-if` code block
} else if (condition) {
// Second `else-if` code block
}
Kuten näet, else-if-lause on yksinkertainen, ja sitä seuraa if-lause:
if (condition) {
// Code block
} else if (condition) {
// Repeat the `if` syntax
}
Soveltakaamme tätä esimerkkiimme:
1234567891011let a = 11; if (a > 15) { console.log("greater than 15"); } else if (a > 10) { console.log("greater than 10"); } else if (a > 5) { console.log("greater than 5"); } else if (a > 0) { console.log("greater than 0"); }
Nyt olemme luoneet ehtojen ketjun. Kun vähintään yksi if-ehto tulee true, ketju keskeytyy.
Huomautus
Tämä rakenne on hyödyllinen, kun tarvitset vain yhden ehdon täyttyvän.
Lisää else
Voit myös lisätä else-lauseen ehtoketjun jälkeen.
Muokataan esimerkkiämme:
12345678910111213let a = -61; if (a > 15) { console.log("greater than 15"); } else if (a > 10) { console.log("greater than 10"); } else if (a > 5) { console.log("greater than 5"); } else if (a > 0) { console.log("greater than 0"); } else { console.log("No condition is satisfied"); }
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you explain how the else if chain works in this example?
What would happen if the value of 'a' was changed to a different number?
Can you show a real-world scenario where else if would be useful?
Awesome!
Completion rate improved to 2.33
Else If -Rakenteen Käyttäminen Useisiin Ehtoihin
Pyyhkäise näyttääksesi valikon
Nyt tarkastellaan tilannetta, jossa useita ehtoja tulee mukaan kuvaan:
1234567891011121314151617let a = 11; if (a > 15) { console.log('greater than 15'); } if (a > 10) { console.log('greater than 10'); } if (a > 5) { console.log('greater than 5'); } if (a > 0) { console.log('greater than 0'); }
Tässä esimerkissä muuttuja a on suurempi kuin 10, mutta myös muut viestit kuten "greater than 5" ja "greater than 0" tulostuvat. Tämä ei ole toivottu käyttäytyminen, kun haluat suorittaa vain yhden ehdon.
else-lause ei toimisi hyvin tässä useiden ehtojen vuoksi.
else if -rakenteen esittely
else if -rakenne tarjoaa ratkaisun tietyn koodilohkon valitsemiseen ehtosarjassa:
if (condition) {
// First `if` code block
} else if (condition) {
// First `else-if` code block
} else if (condition) {
// Second `else-if` code block
}
Kuten näet, else-if-lause on yksinkertainen, ja sitä seuraa if-lause:
if (condition) {
// Code block
} else if (condition) {
// Repeat the `if` syntax
}
Soveltakaamme tätä esimerkkiimme:
1234567891011let a = 11; if (a > 15) { console.log("greater than 15"); } else if (a > 10) { console.log("greater than 10"); } else if (a > 5) { console.log("greater than 5"); } else if (a > 0) { console.log("greater than 0"); }
Nyt olemme luoneet ehtojen ketjun. Kun vähintään yksi if-ehto tulee true, ketju keskeytyy.
Huomautus
Tämä rakenne on hyödyllinen, kun tarvitset vain yhden ehdon täyttyvän.
Lisää else
Voit myös lisätä else-lauseen ehtoketjun jälkeen.
Muokataan esimerkkiämme:
12345678910111213let a = -61; if (a > 15) { console.log("greater than 15"); } else if (a > 10) { console.log("greater than 10"); } else if (a > 5) { console.log("greater than 5"); } else if (a > 0) { console.log("greater than 0"); } else { console.log("No condition is satisfied"); }
Kiitos palautteestasi!