if else C# 入門
if文
if文はもし何々だったらです。
最も短いif文は
if (条件文) 処理;
サンプルソース
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;
if (a<b) Console.WriteLine(“bはaより大きいです”);
}
}
}
実行結果は
bはaより大きいです
それ以外だったら何も処理されません。
それ以外で処理したいときは
if else文
elseを使います。
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 = 7;
int b = 2;
if (a < b) {
Console.WriteLine(“bはaより大きいです”);
}
else
{
Console.WriteLine(“bはaより小さいです”);
}
}
}
}
実行結果は
bはaより小さいです
if else if文 複数条件
複数の条件を指定したいときは
if else if文を使います。
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 = 7;
int b = 2;
if (a < b) { Console.WriteLine(“bはaより大きいです”); } else if ((a>b))
{
Console.WriteLine(“bはaより小さいです”);
}
}
}
}
実行結果は
bはaより小さいです
else if を複数
たとれば条件を変えて3つ書くこともできます。
それ以外の else を最後につけたすことをできます。
if 入れ子
ifを入れ子ににすることもできます。
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;
int c = 3;
if (a < b) {
Console.WriteLine(“bはaより大きいです”);
if (b < c) Console.WriteLine(“bはcより小さいです”); } else if ((a>b))
{
Console.WriteLine(“bはaより小さいです”);
}
}
}
}
実行結果は
bはaより大きいです
bはcより小さいです
この場合は
入れ子にそれ以外がないので
cのほうが小さい場合は2回目は実行されません。
単純に二つの条件が満たしたいときは&&をつかえばいいですね。
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;
int c = 3;
if (a < b && b < c) { Console.WriteLine(“bはaより大きいです”); Console.WriteLine(“bはcより小さいです”); } else if ((a>b))
{
Console.WriteLine(“bはaより小さいです”);
}
}
}
}
実行結果は
bはaより大きいです
bはcより小さいです