If-Else-operaattori
if-else-lause on olennainen osa ohjelmointia. Sen avulla ohjelman kulkua voidaan ohjata tiettyjen ehtojen perusteella. if-else-rakenne on varsin yksinkertainen:
main.c
123456if (condition) { // Execute this block of code if condition is `true` } else { // Execute this block of code if condition is `false` }
Esimerkiksi, jos temperature-muuttuja lukee arvon anturista ja halutaan ohjelma, joka ilmoittaa, kun lämpötila ylittää asetetun rajan.
Main.c
1234567891011121314#include <stdio.h> int main() { int temperature = 200; // In celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }
Voit käyttää useita if-lauseita ohjelmassa, erityisesti kun sinun täytyy arvioida useita ehtoja. if-else-lausetta voidaan laajentaa edelleen käyttämällä else-if-rakennetta:
Main.c
123456789101112131415161718192021#include <stdio.h> int main() { int tempereture = 50; // In celsius if (tempereture > 50) { printf("Temperature is high: %d degrees Celsius\n", tempereture); // First instruction } else if (tempereture > 100) { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// Second instruction } else if (tempereture > 150) { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// Third instruction } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // Fourth instruction } return 0; }
1. Mikä on if-else-lauseen tarkoitus ohjelmoinnissa?
2. Mikä on if-else-lauseen perusrakenne C-kielessä, mukaan lukien syntaksi koodilohkoille.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 2.63
If-Else-operaattori
Pyyhkäise näyttääksesi valikon
if-else-lause on olennainen osa ohjelmointia. Sen avulla ohjelman kulkua voidaan ohjata tiettyjen ehtojen perusteella. if-else-rakenne on varsin yksinkertainen:
main.c
123456if (condition) { // Execute this block of code if condition is `true` } else { // Execute this block of code if condition is `false` }
Esimerkiksi, jos temperature-muuttuja lukee arvon anturista ja halutaan ohjelma, joka ilmoittaa, kun lämpötila ylittää asetetun rajan.
Main.c
1234567891011121314#include <stdio.h> int main() { int temperature = 200; // In celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }
Voit käyttää useita if-lauseita ohjelmassa, erityisesti kun sinun täytyy arvioida useita ehtoja. if-else-lausetta voidaan laajentaa edelleen käyttämällä else-if-rakennetta:
Main.c
123456789101112131415161718192021#include <stdio.h> int main() { int tempereture = 50; // In celsius if (tempereture > 50) { printf("Temperature is high: %d degrees Celsius\n", tempereture); // First instruction } else if (tempereture > 100) { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// Second instruction } else if (tempereture > 150) { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// Third instruction } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // Fourth instruction } return 0; }
1. Mikä on if-else-lauseen tarkoitus ohjelmoinnissa?
2. Mikä on if-else-lauseen perusrakenne C-kielessä, mukaan lukien syntaksi koodilohkoille.
Kiitos palautteestasi!