Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele If-Else-operaattori | Ohjauslauseet
C:n Perusteet

bookIf-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

main.c

copy
123456
if (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

Main.c

copy
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

Main.c

copy
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.

question mark

Mikä on if-else-lauseen tarkoitus ohjelmoinnissa?

Select the correct answer

question mark

Mikä on if-else-lauseen perusrakenne C-kielessä, mukaan lukien syntaksi koodilohkoille.

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain how the if-else statement works in more detail?

What are some common mistakes to avoid when using if-else statements?

Can you give more examples of using if-else statements in different scenarios?

Awesome!

Completion rate improved to 2.63

bookIf-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

main.c

copy
123456
if (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

Main.c

copy
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

Main.c

copy
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.

question mark

Mikä on if-else-lauseen tarkoitus ohjelmoinnissa?

Select the correct answer

question mark

Mikä on if-else-lauseen perusrakenne C-kielessä, mukaan lukien syntaksi koodilohkoille.

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1
some-alt