Chapter 04 — Conditions: if, else if, else, switch
Part 1A — Learning Dart · Chapter 04 of 62 · 3 h
Chapter 04 — Conditions: if, else if, else, switch
Level: beginner Estimated time: 3 h Prerequisites: Chapter 03 — Operators and Expressions What you will be able to do by the end: make a program react to data using
if,else if,else, andswitch, including Dart's modernswitchexpression.
04.0 — Chapter Objectives
By the end of this chapter, you will be able to:
- understand what a condition is;
- use
if; - use
else; - use
else if; - combine multiple conditions;
- create nested conditions;
- use logical operators in a condition;
- use
switch; - understand when to use
iforswitch; - build decision systems;
- build simple game logic;
- display a
Game Over; - handle different levels;
- award rewards;
- control access to certain features.
04.1 — Why Do We Need Conditions?
So far, our programs have executed instructions in order.
Example:
void main() {
print('Début');
print('Joueur');
print('Fin');
}
The program always runs:
Début
Joueur
Fin
But in a real application, we need to make decisions.
For example:
IF the player still has lives
continue the game
ELSE
display Game Over
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion