Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Dartにおけるelse-if文 | 条件分岐文
Dart入門

Dartにおけるelse-if文

メニューを表示するにはスワイプしてください

else…if 文は、複数の条件を判定する際に有用。以下はその構文。

  • if 構文には任意の数の else if ブロックを含めることが可能。
  • else のコードブロックは必ずしも使用する必要はない。
if (first_expression)
{

} 
else if (second_expression)
{
 
} 
else
{ 

} 

else if 文は if と同様に動作し、() 内に条件式、{} 内にコードブロックを持つ。いずれかの条件が真となり、そのブロックが実行されると、それ以降の条件はスキップされる。

main.dart

main.dart

12345678910111213141516
void main() { int num = 2; if(num > 0) { print("is positive"); } else if(num < 0) { print("is negative"); } else { print("is zero"); } }

このプログラムでは、変数 num に値 2 を定義している。num0 より大きい場合は "is positive" を出力し、num0 より小さい場合は "is negative" を出力する。それ以外、num0 の場合は "is zero" を出力する。

課題

コードを修正して、プログラムが正しく動作するようにしてください。

main.dart

main.dart

12345678910111213141516
void 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

main.dart

12345678910111213141516
void 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に質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  3
some-alt