LoginSignup
11
10

More than 3 years have passed since last update.

Dartの制御文(繰り返し)

Posted at

Dartの制御文(繰り返し)

繰り返し制御文は他のプログラミング言語と大差ない様子。

for文

List<int> list = [1, 2, 3];
// for
for (int i = 0; i < list.length; i++){
    print(list[i]);
}
// 実行結果
1
2
3

forEach文

List<int> list = [1, 2, 3];
Map map = { 1 : 'first',
            2 : 'second',
            3 : 'third',
}; 
// forEach
list.forEach((int value){print(value);});
map.forEach((key,value){print('$key : $value');});

// 実行結果
1
2
3
1 : first
2 : second
3 : third

for-in文

List<int> list = [1, 2, 3];
// for-in
for(var value in list){
    print(value);
}
// 実行結果
1
2
3

while文

List<int> list = [1, 2, 3];
var index = 0;
while(true){
    if (index >= list.length){
        break;
    }
    print(list[index]);
    index++;
}
// 実行結果
1
2
3

do-while文

List<int> list = [1, 2, 3];
var index = 0;
do {
    print(list[index]);
    index++;
} while(index < list.length);
// 実行結果
1
2
3

[参考サイト]

Flutter入門のためのDart入門

11
10
1

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
11
10