LoginSignup
0

More than 3 years have passed since last update.

Dart-Controle Flow statements

Posted at

Dart-Controle Flow statements

Dart言語における条件分岐やループ処理等を整理します。

条件分岐

if文

まんまJavaと一緒の書き方です。

void main(List<String> args) {
  bool flg = false;

  if(flg == true) {
    print("true");
  } else if(flg == false) {
    print("false");
  } else {
    print("else");
  }
}

switch文

これもjavaと一緒の書き方です。

void main(List<String> args) {
  String command = 'OPEN';
  switch(command) {
    case 'COLOSED' :
      print('CLOSED');
      break;
    case 'OPEN' :
      print('OPEN');
      break;
    default:
      print('DEFAULT');
  }
}

ループ

for文

dartにおけるfor文は三種類の書き方ができます。

void main(List<String> args) {
  List<int> list = [1,2,3];

  // 一般的な書き方
  for(int i = 0; i < list.length; i++) {
    print(list[i]);
  }

  // クロージャーを使用した書き方
  //{}は省略可能
  list.forEach((i) => {print(i)});

  // 拡張for文
  for(int i in list) {
    print(i);
  }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0