Codegym University コースの一部としてのメンターによる講義の抜粋。フルコースにお申し込みください。


「こんにちは、アミーゴ。今日はif/else ステートメントについて話します。」

「外部環境の変化に対応できなければ、プログラムはほとんど役に立ちません。プログラムは、状況に適応して、ある場合にはあるアクションを実行し、別の場合には別のアクションを実行する方法を知る必要があります。Javaでは、これは、 「if/else ステートメント」 – 条件が満たされた場合にさまざまなコード ブロックを実行できるようにする特別な構造。

「これは、「条件」、「コマンド 1」、および「コマンド 2 」の 3 つの部分で構成されます。条件が true の場合は「コマンド 1」が実行され、そうでない場合は「コマンド 2」が実行されます。これらのコマンドが両方とも実行されることはありません。ステートメントは多かれ少なかれ次のようになります。」

if/else ステートメントのコード
if (condition)
    command_1;
else
    command_2;

「なんて面白いのでしょう! この一言でプログラミングがもっと面白くなると思いますよ!」

「はい。ここにいくつかの例を示します。」

コード 説明
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
a が b より小さい場合、最初のコマンドが実行されます。それ以外の場合は、2 番目のコマンドが実行されますコマンドが両方とも実行されることはありません。
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
1 つのコマンドをコード ブロックに置き換えることができます。残りは同じです。
3
if (a < b)
{
    a = 0;
}
else
{
}
elseブロックが空の場合は省略できます。
これら 3 つの例は完全に同等です。
コマンドを 1 つだけ実行する必要がある場合は、中括弧を省略できます。複数のコマンドがある場合は、括弧を保持する必要があります。
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

「ディエゴが私にいくつかの仕事を与えるよう頼んだところです。」

2
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
タスク
Java Syntax,  レベル 4レッスン 4
ロック未解除
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Codegym University コースの一部としてのメンターによる講義の抜粋。フルコースにお申し込みください。