Flutter(四)Dart语言基础-控制语句

2020-07-17  本文已影响0人  U东东枪

本文主要学习Dart 中控制语句

if 语句

  double score = 90.5;
  if (score >= 90) {
    if (score == 100) {
      print("完美");
    } else {
      print("优秀");
    }
  } else if (score > 60) {
    print("良好");
  } else if (score == 60) {
    print("及格");
  } else {
    print("不及格");
  }
  //优秀

for 语句

  var list = ["a", "b", "c", "d", "e"];
  for (var i = 0; i < list.length; i++) {
    print(list[i]);
    // a
    // b
    // c
    // d
    // e
  }
  print("-------------");
  for (var item in list) {
    print(item);
    // a
    // b
    // c
    // d
    // e
  }

while语句

  int a = 1;
  while (a < 5) {
    print(a++);
  }
  // 1
  // 2
  // 3
  // 4
  //-----------------
  int b = 1;
  do {
    print(++b);
  } while (b < 5);
  // 2
  // 3
  // 4
  // 5

breakcontinue

  var list = [1, 2, 3];
  for (var i = 0; i < list.length; i++) {
    if (list[i] == 2) {
      continue; //跳出本次循环,执行下一个循环
    }
    print(list[i]);
    //1
    //3
  }
  print("-------------");
  var list1 = [4, 5, 6];
  for (var item in list) {
    for (var item1 in list1) {
      if (item1 == 5) {
        break; // 结束循环
      }
      print(item1);
      //4
      //4
      //4
    }
  }
  //item1 = 5时,结束循环

switch...case 语句

  String language = "Swift";
  switch (language) {
    case "Dart":
      print("I love Dart");
      break;
    case "Swift":
      print("I love Swift");
      break;
    case "Java":
      print("I love Java");
      break;
    default:
      print("None");
  }
  //I love Swift

  switch (language) {
    Test:
    case "Dart":
      print("I love Dart");
      break;
    case "Swift":
      print("I love Swift");
      continue Test;
    case "Java":
      print("I love Java");
      break;
    default:
      print("None");
  }
  //I love Swift
  //I love Dart
上一篇下一篇

猜你喜欢

热点阅读