0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MQL5-swich と case文の使い方 8

Posted at

はじめに

この記事は以下リンクのYoutubeビデオを翻訳したものになります。よければそちらもご覧ください。

前回記事 は以下です。

ifを並べるのは...

プログラミングしていると、しばしば、「もし、○○だったらAをする、ちがってたら何もしない」という2択ではなく、「◎だったらA、△だったらB、×だったらC、◇だったらD・・・」のように4択や5択になることもある。一個一個すべてifで確認して条件分岐していってもいいのだがswich文など便利なものもある。

実例


int choice = 1;


void OnTick()
  {
   string entry = "";

   switch(choice)
     {
      case 1:
         entry = "RSIがほしい";
         break;

      case 2:
         entry = "ボリンジャーバンドがほしい";
         break;

      case 3:
         entry = "単純移動平均線がほしい";
         break;

      case 4:
         entry = "MACDがほしい";
         break;

      case 5:
         entry = "ZigZagがほしい";
         break;

      default:
         entry = "何もいらない";
     }
     Comment(entry);
  }

さて、これをこのままコンパイルして動かすと”RSIがほしい”というふうにCommentででてくるはずである。
一行目の

int choice = 1;

ここで後に続くswich文のどの場合に移行するのかを決めている。ここでは1の場合ということである。

そして問題のswich文本体は以下のとおりである。

   switch(choice)
     {
      case 1:
         entry = "RSIがほしい";
         break;

      case 2:
         entry = "ボリンジャーバンドがほしい";
         break;

...以下省略

switch()のかっこの中にint型の数値を与えて、なかのcaseの番号に当てはまっている処理に回るのである、
今回はwitchに入力されている数値は1なので、 case 1: の部分が動くのである。一度これを動かしてみたら、一行目のchoice変数の中身を変更してみてもう一度やってみよう。

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?