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?

C言語学習記録(7日目 10/9)

Posted at

list3-19から

知らなかったこと

switch文

入力した整数値を3で割った剰余を表示するプログラムを以下のように書ける。
単一の整数型の式の結果で動きを変えたい場合は、if文で書くよりも条件式を省けて分かりやすくなる。

switch (no % 3) {
 case 0: puts("3で割り切れます。");		break;
 case 1: puts("3で割った剰余は1です。"); break;
 case 2: puts("3で割った剰余は2です。"); break;
}

少し複雑な動き
defaultはどのcaseにも当てはまらないときの動き。
breakが無いときは、そのまま下のプログラムに沿って動き続け、breakで止まる。
caseの後に何もない場合も同様に下のプログラムを順に実行していく。

switch (sw) {                           // 出力
case 1  : puts("A"); puts("B");	break;  // AB
case 2  : puts("C");                    // CD
case 5  : puts("D"); break;             // D
case 6  :                               // E
case 7  : puts("E"); break;             // E
default : puts("F"); break;             // F
}

※caseと数字の間のスペースは省略不可。
※caseの数字の部分は変数を使うことは出来ず、定数に限る。

進捗

3章終了、4章のソリューション作りから

0
0
2

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?