0Pricing
Mojo Academy · レッスン

if/elseによる条件分岐

条件に応じて処理を分岐します

「if/elseによる条件分岐」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎯

よくある質問

「if/elseによる条件分岐」レッスンは無料ですか?

はい。「if/elseによる条件分岐」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「if/elseによる条件分岐」で何を学びますか?

条件に応じて処理を分岐します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「if/elseによる条件分岐」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. if/elseによる条件分岐
  2. whileによるループ
  3. forとrangeによる反復
  4. break、continue、早期終了
← Mojo Academyに戻る