C#を使うようにはなったものの、具体的な機能については未習熟なので(というかまだ使うべき状況が出てきていないので)
を読んで、まずは最低ラインを学んでいく。
3. 演算と変数
3.1 演算
意味 |
|
| + | 足し算 |
| - | 引き算 |
| 掛け算 | |
| / | 割り算 |
| % | 剰余 |
3.2 変数
変数はデータを格納するための箱である。変数を扱うには、①変数の宣言、②値の代入を行なう。変数を宣言した後に最初に行う代入を初期化という。
int a,b; //変数a,bを宣言 int a = 1,b = 2; //変数a,bを初期化 int a,b=1 //変数a,bを宣言し、bのみ初期化
演算は「掛け算=割り算>足し算=掛け算」の順番で行う。同一の優先順位の演算が並んでいる場合は左にある順に行う。ただし()で囲んでいる場合は囲まれた演算を最優先で行う。
3.2.1 データの型
変数を宣言する際はデータの型を定義する。
3.2.2 変数の命名規則
- 使用できる文字は半角の英文字・数字・アンダーバーのみ。
- 変数名の最初の文字を数字にすることは出来ない。
- 英文字の大文字と小文字は別の文字として扱われる。
- 規定されているC#のキーワードは使用不可:
図表2 C#で利用できない予約語
| abstract | as | base | bool | break | byte | case |
| catch | char | checked | class | const | continue | decimal |
| default | delegate | do | double | else | enum | event |
| explicit | extern | false | finally | fixed | float | for |
| foreach | goto | if | implicit | in | int | interface |
| internal | is | lock | long | namespace | new | null |
| object | operator | out | override | params | private | protected |
| public | readonly | ref | return | sbyte | sealed | short |
| sizeof | stackalloc | static | string | struct | switch | this |
| throw | true | try | typeof | unit | ulong | unchecked |
| unsafe | ushort | using | using static | virtual | void | volatile |
| while |
3.3 変数の基礎知識
代入演算子はである。
3.3.1 主な代入演算子
3.3.2 キャスト
変換した型を先頭に付けることで行う明示的な型の変換をキャストという。整数から小数への変換はキャストを省略してもエラーにならない。これは暗黙的な型変換が行われているからである。
double a; a = (int)12; //キャストありで代入
3.3.3 文字列
文字列を用いる際は、string型を用いる。文字列は演算子で結合できる。
3.3.4 const
値を変えたくない変数には宣言時にconstを予め付ける。
3.4 例題1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem201
{
class Program
{
static void Main(string[] args)
{
// a, b の入力
Console.Write("a=");
int a = int.Parse(Console.ReadLine());
Console.Write("b=");
int b = int.Parse(Console.ReadLine());
// 四則演算
Console.WriteLine("a + b = {0}", a + b);
Console.WriteLine("a - b = {0}", a - b);
Console.WriteLine("a * b = {0}", a * b);
Console.WriteLine("a / b = {0}", a / b);
Console.WriteLine("a % b = {0}", a % b);
}
}
}
3.5 例題2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem202
{
class Program
{
static void Main(string[] args)
{
string name = "Tom";
Console.WriteLine("My name is {0}.", name);
}
}
}
3.6 例題3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem203
{
class Program
{
static void Main(string[] args)
{
string name = "Tom";
Console.WriteLine("My name is {0}.", name);
}
}
}
4. 条件分岐
ある条件で処理の流れが変わる分岐処理を条件分岐という。
4.1 if文
条件分岐の最も簡単なものがif文である。条件式を満たした場合、処理を行う(満たさない場合は何もしない)。
if(条件式)
{
処理
}
条件式として(たとえば比較演算子による)真偽値(trueまたはfalse)を与える。
条件を満たさない場合に別の処理を行なわせたい場合は、if~else文とする。
if(条件式)
{
処理①
}
else
{
処理②
}
条件が複数に渡る場合は、else if文を用いる。
if(条件式①)
{
処理①
}
else if(条件式②)
{
処理②
}
else
{
処理③
}
更に複数の条件を用いる場合は、if~else, else if文を組み合わせたネスト上の構文を書くのが1つの方法である。
4.2 switch文
(ある値に等しいという)複数の条件を課したい場合は、switch文が便利である。
switch(値)
{
case 値①;
処理①
break;
case 値②;
処理②
break;
…
default: // 以上のcaseで与えた値Xのいずれにも該当しない場合に処理させる場合に追記。なければ、条件に該当しない場合は何もしない。
処理③
break;
}
C#はbreakを入れない処理(フォールスルーという。)を原則禁止している。
4.3 例題4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem204
{
class Program
{
static void Main(string[] args)
{
Console.Write("月(1~12)を入力して下さい:");
int a = int.Parse(Console.ReadLine());
string date;
switch (a)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("{0} 月は {1} 日あります", a, "31");
break;
case 2:
Console.WriteLine("{0} 月は {1} 日あります", a, "28または29");
break;
case 4:
case 6:
case 9:
case 11:
Console.WriteLine("{0} 月は {1} 日あります", a, "30");
break;
default:
Console.WriteLine("適正な値を入力して下さい。");
break;
}
}
}
}
4.4 例題5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem205
{
class Program
{
static void Main(string[] args)
{
Console.Write("文字列を入力:");
string s = Console.ReadLine();
if (s .Equals("Hello",StringsComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Helloが入力されました。");
}
else
{
Console.WriteLine("Helloと入力して下さい。");
}
}
}
}
4.5 例題6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem206
{
class Program
{
static void Main(string[] args)
{
Console.Write("文字列を入力:");
string s = Console.ReadLine();
if (s .Equals("Hello",StringsComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Helloが入力されました。");
}
else
{
Console.WriteLine("Helloと入力して下さい。");
}
}
}
}
4.6 例題7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleProblem207
{
class Program
{
static void Main(string[] args)
{
Console.Write("H2Oの温度:");
double temp = double.Parse(Console.ReadLine());
if (temp >= 100.0)
{
Console.WriteLine("気体");
}
else if (temp > 0.0)
{
Console.WriteLine("液体");
}
else
{
Console.WriteLine("固体");
}
}
}
}
}
