Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Voorwaardelijke Uitspraken | Introductie tot Programmastroom
C++ Introductie
course content

Cursusinhoud

C++ Introductie

C++ Introductie

1. Aan de Slag
2. Inleiding tot Operatoren
3. Variabelen en Gegevenstypen
4. Introductie tot Programmastroom
5. Introductie tot Functies

book
Voorwaardelijke Uitspraken

De if constructie in programmeren stelt je programma in staat om beslissingen te nemen en verschillende scenario's te behandelen.

Het heeft twee belangrijke componenten: een voorwaarde die evalueert naar waar of onwaar, en de acties of gevolgen die volgen op basis van de uitkomst van die voorwaarde.

cpp

main

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } if (balance < 13) { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

De else constructie in programmeren wordt gebruikt in combinatie met een if-verklaring om een alternatieve reeks acties te definiëren die moeten worden uitgevoerd wanneer de voorwaarde in de if-verklaring onwaar is.

cpp

main

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

Je kunt extra if...else-verklaringen genest binnen een andere if...else-blok hebben. Dit staat bekend als geneste if...else. Dit maakt complexere besluitvorming mogelijk, waarbij meerdere voorwaarden opeenvolgend kunnen worden gecontroleerd en verschillende acties kunnen worden ondernomen op basis van deze voorwaarden.

cpp

main

cpp

format_example

copy
12345678910111213141516171819202122
#include<iostream> int main() { int balance = 25; if (balance >= 13) // First condition: check if balance is greater than or equal to 13 { if (balance >= 20) // Nested condition: check if balance is greater than or equal to 20 { std::cout << "Balance is greater than or equal to 20, transaction is APPROVED" << std::endl; } else { std::cout << "Balance is between 13 and 19, transaction is OKAY" << std::endl; } } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

Opmerking

Als er slechts één instructie is om uit te voeren binnen een if of else blok, kun je de accolades weglaten. Dit kan de code beknopter maken, maar het vermindert ook de duidelijkheid, vooral bij complexere voorwaarden.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

C++ Introductie

C++ Introductie

1. Aan de Slag
2. Inleiding tot Operatoren
3. Variabelen en Gegevenstypen
4. Introductie tot Programmastroom
5. Introductie tot Functies

book
Voorwaardelijke Uitspraken

De if constructie in programmeren stelt je programma in staat om beslissingen te nemen en verschillende scenario's te behandelen.

Het heeft twee belangrijke componenten: een voorwaarde die evalueert naar waar of onwaar, en de acties of gevolgen die volgen op basis van de uitkomst van die voorwaarde.

cpp

main

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } if (balance < 13) { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

De else constructie in programmeren wordt gebruikt in combinatie met een if-verklaring om een alternatieve reeks acties te definiëren die moeten worden uitgevoerd wanneer de voorwaarde in de if-verklaring onwaar is.

cpp

main

copy
1234567891011121314151617181920
#include<iostream> // if (condition) // { // Actions to take if the condition is true // } int main() { int balance = 25; if (balance >= 13) { std::cout << "Balance is greater than 13, transaction is OKAY" << std::endl; } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

Je kunt extra if...else-verklaringen genest binnen een andere if...else-blok hebben. Dit staat bekend als geneste if...else. Dit maakt complexere besluitvorming mogelijk, waarbij meerdere voorwaarden opeenvolgend kunnen worden gecontroleerd en verschillende acties kunnen worden ondernomen op basis van deze voorwaarden.

cpp

main

cpp

format_example

copy
12345678910111213141516171819202122
#include<iostream> int main() { int balance = 25; if (balance >= 13) // First condition: check if balance is greater than or equal to 13 { if (balance >= 20) // Nested condition: check if balance is greater than or equal to 20 { std::cout << "Balance is greater than or equal to 20, transaction is APPROVED" << std::endl; } else { std::cout << "Balance is between 13 and 19, transaction is OKAY" << std::endl; } } else { std::cout << "Balance is less than 13, transaction is NOT OKAY" << std::endl; } }

Opmerking

Als er slechts één instructie is om uit te voeren binnen een if of else blok, kun je de accolades weglaten. Dit kan de code beknopter maken, maar het vermindert ook de duidelijkheid, vooral bij complexere voorwaarden.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt