Tomar decisiones con if/else
Cree ramificaciones según las condiciones.
Tomar decisiones con if/else es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎯
Preguntas frecuentes
¿La lección «Tomar decisiones con if/else» es gratis?
Sí — el texto completo de «Tomar decisiones con if/else» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Tomar decisiones con if/else»?
Cree ramificaciones según las condiciones. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Mojo Academy?
No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Tomar decisiones con if/else»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?
Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Tomar decisiones con if/else
- Repetir con while
- Iterar con for y range
- break, continue y salida anticipada