C# 演算子

演算子 C# 入門

算術演算子

算術演算子とは計算の演算に使う演算子です。

+ 足す
引く
* かける
/ 割る
% 余り
= 代入


サンプルソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;
int b = 2;

Console.WriteLine(a + b);
Console.WriteLine(a – b);
Console.WriteLine(a * b);
Console.WriteLine(a / b);
Console.WriteLine(a % b);
Console.WriteLine(a = b);

}
}
}

実行結果は
3
-1
2
0
1
2

代入演算子

代入演算子とはもとにある変数に代入して演算します。

y += c

yにcの値を足して代入します。

+= 足して代入
-= 引いて代入
*= かけて代入
/= 割って代入
%= 余りを代入

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int a = 1;
int b = 2;
Console.WriteLine(a += b);
Console.WriteLine(a -= b);
Console.WriteLine(a *= b);
Console.WriteLine(a /= b);
Console.WriteLine(a %= b);
}
}
}
実行結果は
3
1
2
1
1

インクリメント

インクリメントは変数に1を足します。

++
1を足す

デクリメント

デクリメントは変数に1を引きます。


1を引く

前置 後置

前置 後置とはインクリメントやデクリメントの変数の位置のことです。

前置は変数の前にインクリメントやデリクメントをつけます。

++x
先にxを代入される

前置は変数の前にインクリメントやデリクメントをつけます。
後置

x++
後にxを代入される
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;

Console.WriteLine(a++ );
Console.WriteLine(a– );
Console.WriteLine(++a);
Console.WriteLine(–a);

}
}
}

実行結果は
1
2
2
1

比較演算子

== 等しい
< 小なり
> 大なり
>= 以上
<= 以下
!= 等しくない

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;
int b = 2;

Console.WriteLine(a==b );
Console.WriteLine(a < b); Console.WriteLine(a > b);
Console.WriteLine(a >= b);
Console.WriteLine(a <= b);
Console.WriteLine(a != b);
}
}
}

実行結果は
False
True
False
False
True
True

論理型

bool型は
条件式が正しければ
True
違えば
False
を返します。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;
int b = 2;

bool c;

c = (a == b);
Console.WriteLine(c );

c = (a < b); Console.WriteLine(c); c = (a > b);
Console.WriteLine(c);

c = (a >= b);
Console.WriteLine(c);

c = (a <= b);
Console.WriteLine(c);
c = (a != b);
Console.WriteLine(c);

}

}
}

実行結果は

False
True
False
False
True
True

あまり意味はないですが
bool型に式を代入できるんだな。

条件演算子

三項演算子ともいいます。
条件式 ? treeの時 : falseの時;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;
int b = 2;

a =(a == b) ? 0 : 1;
Console.WriteLine(a);

}

}
}

実行結果は
1

論理演算子

かつ & &&
または | ||
ではない !

&&や||は不要な評価を省略します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int a = 1;
int b = 2;

a =(a == b) ? 0 : 1;
Console.WriteLine((a < 0) && (b > 0));
Console.WriteLine((a < 0) || (b > 0));

}

}
}

実行結果は

False
True

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Translate »