Increment and Decrement Operators
The ++ (increment) and -- (decrement) operators modify numeric values. Postfix versions (n++, n--) use the current value of the operand first, then update the variable. Prefix versions (++n, --n) update the variable first, then use the new value. While the order of evaluation differs, the final value of the operand is identical after the operation completes.
// Postfix increment: uses current value first, then increments
int total = 3 + counter++;
// Prefix increment: increments first, then uses the new value
int total = 3 + ++counter;
When multiple increment/decrement operators appear in a single expression, evaluation proceeds left-to-right, following the postfix/prefix rules for each:
int counter = 5;
int result = counter++ + ++counter * 2 + --counter + counter++;
// Step-by-step breakdown:
// 1. counter++ → returns 5, counter becomes 6
// 2. ++counter → increments to 7, returns 7 → 7 * 2 =14
// 3. --counter → decrements to 6, returns 6
// 4. counter++ → returns 6, counter becomes7
// Final total: 5 +14 +6 +6 =31
Console.WriteLine($"Final counter value: {counter}"); // Output: 7
Console.WriteLine($"Calculated result: {result}"); // Output:31
Arithmetic Operators
Arithmetic operators are split into two categories:
- Unary operators: Require a single operand, including
++,--, and unary plus/minus. Unary operators have higher precedecne than binary arithmetic operators. - Binary operators: Require two operands, including addition, subtraction, multiplication, division, and modulus.
int valueA, valueB =5, valueC=6;
// Post-increment valueB, pre-decrement valueC: 5 *5 =25
valueA = valueB++ * --valueC;
// Reset values for second example
valueB=5; valueC=6;
// Pre-increment valueB, post-decrement valueC:6 *6=35
valueA = ++valueB * valueC--;
Compound Assignment Operators
Compound assignment operators simplify common binary operation + assignment expressions. Each operator maps directly to a standard arithmetic operation:
+=→a += bis equivalent toa = a + b-=→a -= bis equivalent toa = a - b*=→a *= bis equivalent toa = a * b/=→a /= bis equivalent toa = a / b%=→a %= bis equivalent toa = a % b
Relational Operators and Boolean Types
C# uses the bool data type to represent logical true/false values, with only two valid literals: true and false (lowercase; uppercase True is invalid syntax).
Relational operators compare two values and return a bool result: >, <, ==, !=, >=, <=. Expressions using these operator are called relational expressions.
bool isEligible = age >=18;
bool valuesMatch = 10 == 20; // Returns false
Logical Operators
Logical operators operate on bool expressions and return bool results. The standard logical operators and their precedence are:
!(logical NOT): Reverses the input bool value&&(logical AND): Returnstrueonly if both operands aretrue||(logical OR): Returnstrueif at least one operand istrue
A common use case is checking for leap years:
int inputYear = Convert.ToInt32(Console.ReadLine());
bool isLeap = inputYear %400 ==0 || (inputYear%4 ==0 && inputYear%100 !=0);
Console.WriteLine(isLeap);
Unlike the bitwise & and | operators, && and || use short-circuit evaluation:
- For
A && B, ifAisfalse,Bis not evaluated because the entire expresion will always befalse - For
A || B, ifAistrue,Bis not evaluated because the entire expression will always betrue
IF Control Structures
Program execution defaults to sequential flow, running line-by-line from the start of the entry point. Control flow can be modified with three core IF-based structures:
Single-Branch IF Statement
Executes a code block only when the condition evaluates to true. The condition is implicitly a bool expression, so if (condition == true) can be shortened to if (condition). To check for a false condition, you must explicitly use == false or the ! operator:
if (testScore >=60)
{
Console.WriteLine("You passed the assessment");
}
// Explicitly check for false condition
if (testScore >=60 == false)
{
Console.WriteLine("You did not pass");
}
Two-Branch IF-ELSE Statement
Provides two mutually exclusive code blocks, one for a true condition and one for a false condition. A key rule: else clauses are always paired with the nearest preceding unpaired if statement.
if (currentTemp >30)
{
Console.WriteLine("High temperature alert");
}
else
{
Console.WriteLine("Temperate conditions");
}
Multi-Condition ELSE-IF Chain
Uses sequential conditional checks to handle multiple mutually exclusive scenarios. The first matching condition's code block runs, and the entire structure exits immediately after. If no conditions match, an optional final else block executes:
if (testScore >=90)
{
Console.WriteLine("Excellent performance");
}
else if (testScore >=80)
{
Console.WriteLine("Good performance");
}
else if (testScore >=60)
{
Console.WriteLine("Satisfactory performance");
}
else
{
Console.WriteLine("Needs improvement");
}