Dartにおけるelse-if文
メニューを表示するにはスワイプしてください
else…if 文は、複数の条件を判定する際に有用。以下はその構文。
if構文には任意の数のelse ifブロックを含めることが可能。elseのコードブロックは必ずしも使用する必要はない。
if (first_expression)
{
}
else if (second_expression)
{
}
else
{
}
else if 文は if と同様に動作し、() 内に条件式、{} 内にコードブロックを持つ。いずれかの条件が真となり、そのブロックが実行されると、それ以降の条件はスキップされる。
main.dart
12345678910111213141516void main() { int num = 2; if(num > 0) { print("is positive"); } else if(num < 0) { print("is negative"); } else { print("is zero"); } }
このプログラムでは、変数 num に値 2 を定義している。num が 0 より大きい場合は "is positive" を出力し、num が 0 より小さい場合は "is negative" を出力する。それ以外、num が 0 の場合は "is zero" を出力する。
課題
コードを修正して、プログラムが正しく動作するようにしてください。
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } ___ (___) { print('Type: double'); } else { print('Type: other type'); } }
else if (score is double) - 正しい条件
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } else if (score is double) { print('Type: double'); } else { print('Type: other type'); } }
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 3
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 3