- Day 4 のゴールと全体イメージ
- 算術演算子(足し算・引き算など)
- 剰余演算子 %
- 比較演算子(大小・等しいかどうか)
- 代入演算子(= と複合代入)
- インクリメント・デクリメント
- 論理演算子(条件を組み合わせる)
- 演算子の総合例:簡単な商品購入チェック
- Day 4 のまとめ
- Day 4 ミニ課題のゴール
- ステップ1:日本語で「簡易計算機」を設計する
- ステップ2:変数を使って数値を管理する
- ステップ3:算術演算子で 4つの計算を行う
- ステップ4:剰余 % を使って「余り」を確認する
- ステップ5:代入演算子と複合代入を意識する
- ステップ6:比較演算子と論理演算子を軽く組み合わせる
- ステップ7:インクリメント・デクリメントをカウンタで体験する
- ミニ課題の完成版コード
- Day 4 ミニ課題のまとめ
Day 4 のゴールと全体イメージ
Day 4 では、C# の「演算子」をまとめて学びます。 演算子は、値どうしを計算したり、比較したり、条件を組み立てたりするための「記号」です。
学ぶ項目は次のとおりです。
- 算術演算子
- 比較演算子
- 代入演算子
- 論理演算子
- インクリメント
- デクリメント
- 剰余
%
これらを、初心者向けにかみ砕きながら、例題とコードを交えてステップバイステップで説明していきます。
算術演算子(足し算・引き算など)
基本の算術演算子
算術演算子は、数値の計算に使う演算子です。
+足し算-引き算*掛け算/割り算%剰余(割り算の余り)
まずは、足し算・引き算・掛け算・割り算から見ていきます。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 3;
int sum = a + b; // 足し算:10 + 3 = 13
int diff = a - b; // 引き算:10 - 3 = 7
int product = a * b; // 掛け算:10 * 3 = 30
int quotient = a / b; // 割り算:10 / 3 = 3(整数同士の割り算は小数点以下切り捨て)
Console.WriteLine($"a = {a}, b = {b}");
Console.WriteLine($"足し算:{sum}");
Console.WriteLine($"引き算:{diff}");
Console.WriteLine($"掛け算:{product}");
Console.WriteLine($"割り算:{quotient}");
Console.ReadLine();
}
}
C#ここでの重要ポイントは、
- 整数同士の割り算は「小数点以下が切り捨てられる」こと
- 小数を含む割り算をしたい場合は
doubleやdecimalを使うこと
です。
剰余演算子 %
「割り算の余り」を求める
% は「剰余演算子」と呼ばれ、割り算の余りを求めるために使います。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 3;
int remainder = a % b; // 10 を 3 で割った余り → 1
Console.WriteLine($"a = {a}, b = {b}");
Console.WriteLine($"a % b = {remainder}");
Console.ReadLine();
}
}
C#剰余は、
- 「偶数か奇数か」を判定するとき
- 「何回目ごとに〜する」といった処理をするとき
によく使われます。
例えば、偶数・奇数の判定は次のように書けます。
int number = 7;
bool isEven = (number % 2 == 0); // 2 で割った余りが 0 なら偶数
Console.WriteLine($"number = {number}");
Console.WriteLine($"偶数かどうか:{isEven}");
C#ここでのポイントは、
% 2で「2 で割った余り」を求めることで、偶数・奇数を判定できること- 剰余は「周期的な処理」にもよく使われること
です。
比較演算子(大小・等しいかどうか)
比較演算子の一覧
比較演算子は、2つの値を比べて「真偽値(true / false)」を返します。
==等しい!=等しくない<より小さい>より大きい<=以下>=以上
例を見てみます。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 3;
bool isEqual = (a == b); // a と b が等しいか
bool isNotEqual = (a != b); // a と b が等しくないか
bool isGreater = (a > b); // a が b より大きいか
bool isLessOrEqual = (a <= b); // a が b 以下か
Console.WriteLine($"a = {a}, b = {b}");
Console.WriteLine($"a == b : {isEqual}");
Console.WriteLine($"a != b : {isNotEqual}");
Console.WriteLine($"a > b : {isGreater}");
Console.WriteLine($"a <= b : {isLessOrEqual}");
Console.ReadLine();
}
}
C#ここでの重要ポイントは、
- 比較演算子の結果は
bool型になること ==と=は意味が違うこと(==は比較、=は代入)
です。
代入演算子(= と複合代入)
基本の代入演算子 =
= は「右側の値を左側の変数に代入する」演算子です。
int x = 5; // x に 5 を代入
x = 10; // x に 10 を代入し直す
C#複合代入演算子
C# には、計算と代入をまとめて書ける「複合代入演算子」があります。
+=足して代入-=引いて代入*=掛けて代入/=割って代入%=剰余を取って代入
例を見てみます。
using System;
class Program
{
static void Main(string[] args)
{
int x = 10;
x += 5; // x = x + 5 と同じ → x は 15
x -= 3; // x = x - 3 と同じ → x は 12
x *= 2; // x = x * 2 と同じ → x は 24
x /= 4; // x = x / 4 と同じ → x は 6
x %= 5; // x = x % 5 と同じ → x は 1
Console.WriteLine($"最終的な x の値:{x}");
Console.ReadLine();
}
}
C#ここでのポイントは、
- 複合代入演算子は「自分自身を更新する」処理を簡潔に書けること
x = x + 5;のようなパターンをx += 5;と短く書けること
です。
インクリメント・デクリメント
インクリメント ++
インクリメントは、「1 を足す」ための演算子です。
int count = 0;
count++; // count = count + 1 と同じ → count は 1
C#++ には、
- 前置インクリメント:
++count - 後置インクリメント:
count++
の2種類があります。
違いは「式の評価順」に関係しますが、 初心者のうちは「単独で使う」形から慣れていくとよいです。
using System;
class Program
{
static void Main(string[] args)
{
int count = 0;
Console.WriteLine($"初期値:{count}");
count++; // 1 増やす
Console.WriteLine($"1 回目のインクリメント後:{count}");
count++; // さらに 1 増やす
Console.WriteLine($"2 回目のインクリメント後:{count}");
Console.ReadLine();
}
}
C#デクリメント --
デクリメントは、「1 を引く」ための演算子です。
int count = 5;
count--; // count = count - 1 と同じ → count は 4
C#こちらも、前置・後置がありますが、 まずは単独で使う形から慣れていきます。
using System;
class Program
{
static void Main(string[] args)
{
int count = 5;
Console.WriteLine($"初期値:{count}");
count--; // 1 減らす
Console.WriteLine($"1 回目のデクリメント後:{count}");
count--; // さらに 1 減らす
Console.WriteLine($"2 回目のデクリメント後:{count}");
Console.ReadLine();
}
}
C#インクリメント・デクリメントは、
- ループ(
for文など)で「カウンタを増やす/減らす」とき - 回数を数えるとき
によく使われます。
論理演算子(条件を組み合わせる)
論理演算子の一覧
論理演算子は、複数の条件を組み合わせるための演算子です。
&&論理積(AND)||論理和(OR)!否定(NOT)
AND &&:両方とも true のときだけ true
using System;
class Program
{
static void Main(string[] args)
{
int age = 25;
bool isMember = true;
// 年齢が 20 以上 かつ 会員である
bool canUseService = (age >= 20) && isMember;
Console.WriteLine($"年齢:{age}");
Console.WriteLine($"会員:{isMember}");
Console.WriteLine($"サービス利用可能:{canUseService}");
Console.ReadLine();
}
}
C#&& は、
- 左右の条件が両方とも
trueのときだけtrue - どちらか一方でも
falseならfalse
になります。
OR ||:どちらかが true なら true
using System;
class Program
{
static void Main(string[] args)
{
bool hasCoupon = true;
bool isPremiumMember = false;
// クーポンを持っている または プレミアム会員である
bool canGetDiscount = hasCoupon || isPremiumMember;
Console.WriteLine($"クーポンあり:{hasCoupon}");
Console.WriteLine($"プレミアム会員:{isPremiumMember}");
Console.WriteLine($"割引適用可能:{canGetDiscount}");
Console.ReadLine();
}
}
C#|| は、
- 左右の条件のどちらか一方でも
trueならtrue - 両方とも
falseのときだけfalse
になります。
NOT !:真偽値を反転する
using System;
class Program
{
static void Main(string[] args)
{
bool isActive = true;
bool isInactive = !isActive; // true を反転して false にする
Console.WriteLine($"アクティブ:{isActive}");
Console.WriteLine($"非アクティブ:{isInactive}");
Console.ReadLine();
}
}
C#! は、
trueをfalseにfalseをtrueに
反転させます。
演算子の総合例:簡単な商品購入チェック
Day 4 の演算子をまとめて使ってみる
最後に、Day 4 の演算子を組み合わせた例として、 「商品購入チェック」の簡単なプログラムを書いてみます。
using System;
class Program
{
static void Main(string[] args)
{
// 商品情報
string productName = "ノートPC";
decimal price = 100000m; // 税抜価格
decimal taxRate = 0.10m; // 消費税率 10%
int stockCount = 5; // 在庫数
// 購入希望個数
int orderCount = 2;
// 在庫が足りているかどうか(比較演算子)
bool hasEnoughStock = stockCount >= orderCount;
// 消費税額と税込価格(算術演算子)
decimal tax = price * taxRate;
decimal priceWithTax = price + tax;
// 合計金額(算術演算子)
decimal total = priceWithTax * orderCount;
// 在庫あり・購入可能かどうか(論理演算子)
bool canPurchase = hasEnoughStock && (orderCount > 0);
Console.WriteLine("=== 購入チェック ===");
Console.WriteLine($"商品名:{productName}");
Console.WriteLine($"税抜価格:{price} 円");
Console.WriteLine($"税込価格:{priceWithTax} 円");
Console.WriteLine($"在庫数:{stockCount} 個");
Console.WriteLine($"購入希望数:{orderCount} 個");
Console.WriteLine($"在庫が足りているか:{hasEnoughStock}");
Console.WriteLine($"購入可能か:{canPurchase}");
Console.WriteLine($"合計金額:{total} 円");
Console.ReadLine();
}
}
C#このコードの中で、
- 算術演算子 → 金額計算(
*,+) - 比較演算子 → 在庫数と購入数の比較(
>=,> 0) - 代入演算子 → 計算結果を変数に入れる(
=) - 論理演算子 → 条件の組み合わせ(
&&) - 剰余
%→ 今回は使っていませんが、個数の偶数・奇数判定などに応用可能
というように、Day 4 の演算子が自然な形で使われています。
Day 4 のまとめ
Day 4 では、C# の演算子として、
- 算術演算子 →
+,-,*,/,% - 比較演算子 →
==,!=,<,>,<=,>= - 代入演算子 →
=,+=,-=,*=,/=,%= - 論理演算子 →
&&,||,! - インクリメント →
++(1 を足す) - デクリメント →
--(1 を引く) - 剰余
%→ 割り算の余りを求める
を、具体的なコードとともに確認しました。
演算子は、
- 計算
- 条件分岐
- ループ
- フラグ管理
など、あらゆる場面で登場します。
ぜひ、
- 自分で数値を変えながら計算結果を確認する
- 比較演算子の結果(
true/false)を意識して眺めてみる ++や--を使ってカウンタを増減させる練習をする%を使って偶数・奇数判定や「何回目ごとに〜する」処理を試してみる
といった練習を通して、「演算子で値を動かす感覚」を身につけていってください。
Day 4 ミニ課題のゴール
このミニ課題では、「簡易計算機」を題材にして、Day 4 で学んだ演算子を実際のコードで体験していきます。 計算内容は次の4つです。
10 + 2010 - 2010 × 2010 ÷ 20
これを C# の算術演算子で実装しながら、 代入演算子・比較演算子・論理演算子・インクリメント・デクリメント・剰余 % も、あわせて軽く触れていきます。
ステップ1:日本語で「簡易計算機」を設計する
どんな処理をするプログラムかを言葉で整理する
簡易計算機の処理を、日本語で整理すると次のようになります。
- 2つの数値(ここでは 10 と 20)を用意する
- 足し算・引き算・掛け算・割り算を行う
- 結果を画面に表示する
まずはこの「流れ」を意識してから、C# のコードに落とし込んでいきます。
ステップ2:変数を使って数値を管理する
計算に使う数値を変数に入れる
計算に使う数値を、変数として宣言・代入します。
using System;
class Program
{
static void Main(string[] args)
{
// 計算に使う 2 つの数値
int a = 10; // 1つ目の数値
int b = 20; // 2つ目の数値
// ここから演算子を使って計算していきます
}
}
C#ここでのポイントは、
aとbに「計算対象の値」を入れておくことで、後の処理が読みやすくなること- 変数を使うことで、後から値を変えてもプログラム全体が柔軟になること
です。
ステップ3:算術演算子で 4つの計算を行う
足し算・引き算・掛け算・割り算
次に、算術演算子を使って 4つの計算を行います。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
// 算術演算子を使った計算
int sum = a + b; // 足し算:10 + 20
int diff = a - b; // 引き算:10 - 20
int product = a * b; // 掛け算:10 * 20
int quotient = a / b; // 割り算:10 / 20(整数同士なので 0)
Console.WriteLine("=== 簡易計算機 ===");
Console.WriteLine($"{a} + {b} = {sum}");
Console.WriteLine($"{a} - {b} = {diff}");
Console.WriteLine($"{a} * {b} = {product}");
Console.WriteLine($"{a} / {b} = {quotient}");
Console.ReadLine();
}
}
C#ここでの重要ポイントは、
+,-,*,/が算術演算子であること- 整数同士の割り算では、小数点以下が切り捨てられるため、
10 / 20は0になること
です。
割り算で小数を扱いたい場合は、double を使うとよいです。
double quotientDouble = (double)a / b; // 10.0 / 20 → 0.5
C#ステップ4:剰余 % を使って「余り」を確認する
10 を 20 で割った余り
簡易計算機に、剰余 % を使った計算も追加してみます。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int sum = a + b;
int diff = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b; // 剰余:10 を 20 で割った余り → 10
Console.WriteLine("=== 簡易計算機 ===");
Console.WriteLine($"{a} + {b} = {sum}");
Console.WriteLine($"{a} - {b} = {diff}");
Console.WriteLine($"{a} * {b} = {product}");
Console.WriteLine($"{a} / {b} = {quotient}");
Console.WriteLine($"{a} % {b} = {remainder}");
Console.ReadLine();
}
}
C#ここでのポイントは、
%は「割り算の余り」を求める演算子であること10 % 20の場合、20 で割り切れないので余りは 10 になること
です。
剰余は、
- 偶数・奇数の判定(
number % 2 == 0なら偶数) - 「何回目ごとに〜する」といった周期的な処理
などにも応用できます。
ステップ5:代入演算子と複合代入を意識する
計算結果を変数に「代入」していることを意識する
先ほどのコードでは、
int sum = a + b;
C#のように、= を使って計算結果を変数に代入していました。
これは、
a + bを計算する- その結果を
sumに「代入」する
という 2ステップを、1行で書いているイメージです。
複合代入演算子を使うと、次のような書き方もできます。
int x = 10;
x += 20; // x = x + 20 と同じ → x は 30
x -= 5; // x = x - 5 と同じ → x は 25
C#簡易計算機では、まずは「計算結果を別の変数に代入する」という基本形に慣れておくとよいです。
ステップ6:比較演算子と論理演算子を軽く組み合わせる
計算結果を比較してみる
簡易計算機に、比較演算子と論理演算子を少しだけ取り入れてみます。 例えば、「足し算の結果が掛け算の結果より小さいかどうか」を判定してみます。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int sum = a + b; // 30
int product = a * b; // 200
// 比較演算子:sum が product より小さいかどうか
bool isSumLessThanProduct = sum < product;
Console.WriteLine("=== 比較結果 ===");
Console.WriteLine($"sum = {sum}, product = {product}");
Console.WriteLine($"sum < product : {isSumLessThanProduct}");
Console.ReadLine();
}
}
C#ここでのポイントは、
<が「より小さい」を表す比較演算子であること- 比較の結果は
bool型(true/false)になること
です。
論理演算子で条件を組み合わせる
例えば、「足し算の結果が 0 より大きく、かつ掛け算の結果が 100 以上かどうか」を判定することもできます。
bool condition = (sum > 0) && (product >= 100);
C#&& は「両方とも true のときだけ true」になる論理演算子です。
ステップ7:インクリメント・デクリメントをカウンタで体験する
計算回数を数える
簡易計算機に「計算回数」を追加して、インクリメント・デクリメントを体験してみます。
using System;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int calculationCount = 0; // 計算回数を数えるカウンタ
int sum = a + b;
calculationCount++; // 足し算を 1 回実行
int diff = a - b;
calculationCount++; // 引き算を 1 回実行
int product = a * b;
calculationCount++; // 掛け算を 1 回実行
int quotient = a / b;
calculationCount++; // 割り算を 1 回実行
Console.WriteLine("=== 簡易計算機 ===");
Console.WriteLine($"{a} + {b} = {sum}");
Console.WriteLine($"{a} - {b} = {diff}");
Console.WriteLine($"{a} * {b} = {product}");
Console.WriteLine($"{a} / {b} = {quotient}");
Console.WriteLine($"実行した計算の回数:{calculationCount} 回");
Console.ReadLine();
}
}
C#ここでのポイントは、
calculationCount++が「1 を足す」インクリメントであること- カウンタ変数を使うことで、「何回処理したか」を簡単に管理できること
です。
デクリメント -- は、「1 を減らす」ための演算子で、 例えば「残り回数」を管理するときなどに使えます。
ミニ課題の完成版コード
Day 4 の学びを簡易計算機に詰め込む
最後に、Day 4 の演算子をバランスよく取り入れた簡易計算機の完成版例を示します。
using System;
class Program
{
static void Main(string[] args)
{
// 計算に使う 2 つの数値
int a = 10;
int b = 20;
// 計算回数を数えるカウンタ
int calculationCount = 0;
// 算術演算子
int sum = a + b;
calculationCount++;
int diff = a - b;
calculationCount++;
int product = a * b;
calculationCount++;
int quotient = a / b; // 整数同士の割り算 → 0
calculationCount++;
int remainder = a % b; // 剰余 → 10
calculationCount++;
// 比較演算子と論理演算子の例
bool isSumPositive = sum > 0;
bool isProductLarge = product >= 100;
bool goodResult = isSumPositive && isProductLarge;
Console.WriteLine("=== 簡易計算機 ===");
Console.WriteLine($"{a} + {b} = {sum}");
Console.WriteLine($"{a} - {b} = {diff}");
Console.WriteLine($"{a} * {b} = {product}");
Console.WriteLine($"{a} / {b} = {quotient}");
Console.WriteLine($"{a} % {b} = {remainder}");
Console.WriteLine();
Console.WriteLine($"実行した計算の回数:{calculationCount} 回");
Console.WriteLine($"sum > 0 : {isSumPositive}");
Console.WriteLine($"product >= 100 : {isProductLarge}");
Console.WriteLine($"両方の条件を満たしているか(AND):{goodResult}");
Console.ReadLine();
}
}
C#このコードの中には、
- 算術演算子 →
+,-,*,/,% - 比較演算子 →
>,>= - 代入演算子 →
= - 論理演算子 →
&& - インクリメント →
++ - 剰余
%→ 余りの計算
という Day 4 の学習項目がすべて登場しています。
Day 4 ミニ課題のまとめ
簡易計算機を通して、
- 2つの数値を変数で管理する
- 算術演算子で基本的な計算を行う
- 剰余
%で余りを求める - 代入演算子で計算結果を変数に保存する
- 比較演算子・論理演算子で「条件」を組み立てる
- インクリメントで「回数」を数える
という流れを体験しました。
演算子は、プログラムの「思考」をそのまま記号にしたような存在です。 ぜひ、
- 数値や条件を少しずつ変えながら、結果の変化を確認する
- 自分で別の計算(例:
30 + 40や15 % 4)を追加してみる - カウンタやフラグを使って「状態」を管理する練習をする
といったアレンジをしながら、「演算子でロジックを組み立てる感覚」を育てていってください。
