0Pricing
Mojo Academy · Aula

Tomando decisões com if/else

Crie ramificações com base em condições.

Tomando decisões com if/else é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎯

Perguntas Frequentes

A aula “Tomando decisões com if/else” é grátis?

Sim — o texto completo de “Tomando decisões com if/else” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.

O que vou aprender em “Tomando decisões com if/else”?

Crie ramificações com base em condições. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Mojo Academy?

Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Tomando decisões com if/else”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Mojo Academy?

Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Tomando decisões com if/else
  2. Repetindo com while
  3. Iterando com for e range
  4. break, continue e saída antecipada
← Voltar para Mojo Academy