Faire des choix avec if/else
Créez des branches selon les conditions.
Faire des choix avec if/else est une leçon Mojo Academy gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Mojo Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Mojo Academy comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
Code That Decides
Programs often need to choose a path. The if statement runs a block only when its condition is true, so your code can react to data. 🔀
Your First if
Write if, a condition, a colon, then an indented block. If the condition holds, Mojo runs the block; otherwise it skips it entirely.
if score > 90:
print("Top marks!")Conditions Are Booleans
The bit after if must boil down to True or False. Comparisons like greater-than and equals naturally produce a boolean for you.
var ready = count == 0
if ready:
print("Empty")Add an else Branch
Pair else with if to cover the other case. Exactly one of the two blocks runs, so nothing is ever left unhandled.
if temp > 30:
print("Hot")
else:
print("Mild")Indentation Defines the Block
Like Python, Mojo uses indentation to mark what belongs to a branch. Consistent spaces, usually four, keep the structure clear and valid.
Comparison Operators
Build conditions with operators like ==, !=, <, and >=. Each compares two values and hands back the boolean that if needs.
if age >= 18:
print("Adult")Chain More Cases with elif
Need more than two paths? Insert one or more elif branches between if and else to test extra conditions in order.
if g >= 90:
print("A")
elif g >= 80:
print("B")
else:
print("C")Only the First Match Wins
Mojo checks branches top to bottom and stops at the first condition that is true. The rest are skipped, even if they would also match.
Combine Conditions with and / or
Join checks using and and or. With and both must be true; with or just one is enough to enter the block.
if hungry and has_food:
print("Eat now")Flip a Condition with not
Put not in front of a condition to invert it. It turns True into False, handy for checking the absence of something.
if not logged_in:
print("Please sign in")Nesting Branches
You can place an if inside another branch to refine a decision. Keep nesting shallow so the logic stays easy to read.
if signed_in:
if is_admin:
print("Dashboard")Quick Check
You want to handle three or more separate cases in one decision. Which keyword do you reach for?
Recap
You branch with if, add else for the other case, and chain elif for more. Conditions are booleans you can combine with and, or, and not. 🎯
Questions Fréquemment Posées
La leçon « Faire des choix avec if/else » est-elle gratuite ?
Oui — le texte complet de « Faire des choix avec if/else » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Mojo Academy, passe à CoddyKit PRO. Le cours Mojo Academy comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Faire des choix avec if/else » ?
Créez des branches selon les conditions. Tu pratiques Mojo Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Mojo Academy ?
Aucune expérience préalable n'est requise. Mojo Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.
Combien de temps prend la leçon « Faire des choix avec if/else » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Mojo Academy ?
Oui. Chaque leçon Mojo Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Faire des choix avec if/else
- Boucler avec while
- Itérer avec for et range
- break, continue et sortie anticipée