8
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

# Dartの制御文(条件分岐)

Dartの制御文(条件分岐)

条件分岐のif文とswitch文について。

switch文

switch文で同一条件の処理をまとめる場合にcaseを連続して書くやり方は使えない。
case COL_YELLOW:case COL_NON:の部分のように実行したいラベル(?)を指定したcontinueを書くとそこへ飛ぶことができるらしい。

const COL_RED    = 0;
const COL_BLUE   = 1;
const COL_YELLOW = 2;
const COL_NON    = 3;
const COL_NON2   = 4;
var signal = COL_RED;

for(int i = 0; i < 3; i++)
{
    switch(signal){
        case COL_RED:
            print('赤は止まれ');
            signal++;
            break;
        case COL_BLUE:
            print('青は進め');
            signal++;
            break;
        case COL_YELLOW:
            print('黄は・・・?');
            continue COL_NON;
        COL_NON2:
        case COL_NON2:
            print('何でしょうねぇ・・。');
            break;
        COL_NON:
        case COL_NON:
            print('何ですか?');
            continue COL_NON2;
        default:
            print('Default');
            break;
    }
}
// 実行結果
赤は止まれ
青は進め
黄は・・・?
何ですか?
何でしょうねぇ・・。

if文

var lists = [ 1, 2, 3, 4, 5 ];

lists.forEach((factor) {
    if ( factor % 2 == 0 ){
        print('$factor : Odd');
    }
    else{
        print('$factor : Even');
    }
});
// 実行結果
1 : Even
2 : Odd
3 : Even
4 : Odd
5 : Even

[参考サイト]

Flutter入門のためのDart入門

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
8
Help us understand the problem. What are the problem?