Logical Equalities
Logical Equalities
Conditions for Equality
Introduction
We are now familiar with the ability to find out whether one of two values is higher or lower than the other. In some cases, you want to know whether two values share a similarity.
Practical Learning: Introducing Conditions
using static System.Console; WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Payroll Preparation"); WriteLine("-------------------------------------------------------"); WriteLine("Enter the following pieces of information"); WriteLine("-------------------------------------------------------"); WriteLine("Employee Information"); WriteLine("-------------------------------------------------------"); Write("First Name: "); string firstName = ReadLine(); Write("Last Name: "); string lastName = ReadLine(); Write("Hourly Salary: "); double hSalary = double.Parse(ReadLine()); WriteLine("-------------------------------------------------------"); WriteLine("Time worked"); WriteLine("-------------------------------------------------------"); Write("Monday: "); double mon = double.Parse(ReadLine()); Write("Tuesday: "); double tue = double.Parse(ReadLine()); Write("Wednesday: "); double wed = double.Parse(ReadLine()); Write("Thursday: "); double thu = double.Parse(ReadLine()); Write("Friday: "); double fri = double.Parse(ReadLine()); double time_worked = mon + tue + wed + thu + fri; double net_pay = hSalary * time_worked; WriteLine("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+"); WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Payroll Evaluation"); WriteLine("======================================================="); WriteLine("Employee Information"); WriteLine("-------------------------------------------------------"); WriteLine($"Full Name: {firstName} {lastName}"); WriteLine($"Hourly Salary: {hSalary:f}"); WriteLine("======================================================="); WriteLine("Time Worked Summary"); WriteLine("--------+---------+-----------+----------+-------------"); WriteLine(" Monday | Tuesday | Wednesday | Thursday | Friday"); WriteLine("--------+---------+-----------+----------+-------------"); WriteLine($" {mon:f} | {tue:f} | {wed:f} | {thu:f} | {fri:f}"); WriteLine("========+=========+===========+==========+============="); WriteLine(" Pay Summary"); WriteLine("-------------------------------------------------------"); WriteLine($" Total Time: {time_worked:f}"); WriteLine("-------------------------------------------------------"); WriteLine($" Net Pay: {net_pay:f}"); WriteLine("=======================================================");
First Name: | Michael |
Last Name: | Carlock |
Hourly Salary: | 28.25 |
Monday | 7 |
Tuesday: | 8 |
Wednesday: | 6.5 |
Thursday: | 8.5 |
Friday: | 6.5 |
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information ------------------------------------------------------- First Name: Michael Last Name: Carlock Hourly Salary: 28.25 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 7 Tuesday: 8 Wednesday: 6.5 Thursday: 8.5 Friday: 6.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Michael Carlock Hourly Salary: 28.25 ======================================================= Time Worked Summary --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 7.00 | 8.00 | 6.50 | 8.50 | 6.50 ========+=========+===========+==========+============= Pay Summary ------------------------------------------------------- Total Time: 36.50 ------------------------------------------------------- Net Pay: 1031.12 ======================================================= Press any key to close this window . . .
First Name: | Catherine |
Last Name: | Busbey |
Hourly Salary: | 24.37 |
Monday | 9.50 |
Tuesday: | 8 |
Wednesday: | 10.50 |
Thursday: | 9 |
Friday: | 10.50 |
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information ------------------------------------------------------- First Name: Catherine Last Name: Busbey Hourly Salary: 24.37 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 9.5 Tuesday: 8 Wednesday: 10.5 Thursday: 9 Friday: 10.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Catherine Busbey Hourly Salary: 24.37 ======================================================= Time Worked Summary --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 9.50 | 8.00 | 10.50 | 9.00 | 10.50 ========+=========+===========+==========+============= Pay Summary ------------------------------------------------------- Total Time: 47.50 ------------------------------------------------------- Net Pay: 1157.58 ======================================================= Press any key to close this window . . .
using static System.Console; WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Payroll Preparation"); WriteLine("-------------------------------------------------------"); WriteLine("Enter the following pieces of information"); WriteLine("-------------------------------------------------------"); WriteLine("Employee Information"); WriteLine("-------------------------------------------------------"); Write("First Name: "); string firstName = ReadLine(); Write("Last Name: "); string lastName = ReadLine(); Write("Hourly Salary: "); double hSalary = double.Parse(ReadLine()); WriteLine("-------------------------------------------------------"); WriteLine("Time worked"); WriteLine("-------------------------------------------------------"); Write("Monday: "); double mon = double.Parse(ReadLine()); Write("Tuesday: "); double tue = double.Parse(ReadLine()); Write("Wednesday: "); double wed = double.Parse(ReadLine()); Write("Thursday: "); double thu = double.Parse(ReadLine()); Write("Friday: "); double fri = double.Parse(ReadLine()); double timeWorked = mon + tue + wed + thu + fri; double regTime = timeWorked; double overtime = 0.00; double overPay = 0.00; double regPay = hSalary * timeWorked; if( timeWorked is > 40.00) { regTime = 40.00; regPay = hSalary * 40.00; overtime = timeWorked - 40.00; overPay = hSalary * 1.50 * overtime; } double netPay = regPay + overPay; WriteLine("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+"); WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Payroll Evaluation"); WriteLine("======================================================="); WriteLine("Employee Information"); WriteLine("-------------------------------------------------------"); WriteLine($"Full Name: {firstName} {lastName}"); WriteLine($"Hourly Salary: {hSalary:f}"); WriteLine("======================================================="); WriteLine("Time Worked Summary"); WriteLine("--------+---------+-----------+----------+-------------"); WriteLine(" Monday | Tuesday | Wednesday | Thursday | Friday"); WriteLine("--------+---------+-----------+----------+-------------"); WriteLine($" {mon:f} | {tue:f} | {wed:f} | {thu:f} | {fri:f}"); WriteLine("========+=========+===========+==========+============="); WriteLine(" Pay Summary"); WriteLine("-------------------------------------------------------"); WriteLine(" Time Pay"); WriteLine("-------------------------------------------------------"); WriteLine($" Regular: {regTime:f} {regPay:f}"); WriteLine("-------------------------------------------------------"); WriteLine($" Overtime: {overtime:f} {overPay:f}"); WriteLine("======================================================="); WriteLine($" Net Pay: {netPay:f}"); WriteLine("=======================================================");
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information ------------------------------------------------------- First Name: Michael Last Name: Carlock Hourly Salary: 28.25 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 7 Tuesday: 8 Wednesday: 6.5 Thursday: 8.5 Friday: 6.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Michael Carlock Hourly Salary: 28.25 ======================================================= Time Worked Summary --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 7.00 | 8.00 | 6.50 | 8.50 | 6.50 ========+=========+===========+==========+============= Pay Summary ------------------------------------------------------- Time Pay ------------------------------------------------------- Regular: 36.50 1031.12 ------------------------------------------------------- Overtime: 0.00 0.00 ======================================================= Net Pay: 1031.12 ======================================================= Press any key to close this window . . .
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information ------------------------------------------------------- First Name: Catherine Last Name: Busbey Hourly Salary: 24.37 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 9.5 Tuesday: 8 Wednesday: 10.5 Thursday: 9 Friday: 10.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Catherine Busbey Hourly Salary: 24.37 ======================================================= Time Worked Summary --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 9.50 | 8.00 | 10.50 | 9.00 | 10.50 ========+=========+===========+==========+============= Pay Summary ------------------------------------------------------- Time Pay ------------------------------------------------------- Regular: 40.00 974.80 ------------------------------------------------------- Overtime: 7.50 274.16 ======================================================= Net Pay: 1248.96 ======================================================= Press any key to close this window . . .
To compare two variables for equality, C# provides the == operator. The formula to use it is:
value1 == value2
The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. The operation can be illustrated as follows:
Unlike the "Less Than" (<) and the "Greater Than" (>) operations, the equality (==) operator cannot use the is operator.
It is important to make a distinction between the assignment "=" and the logical equality operator "==". The first is used to give a new value to a variable, as in Number = 244. The operand on the left side of = must always be a variable and never a constant. The == operator is never used to assign a value; this would cause an error. The == operator is used only to compare two values. The operands on both sides of == can be variables, constants, or one can be a variable while the other is a constant.
Logical Difference
We already know that the == operator is used to find out if two values are the same. The opposite is to find out whether two values are different. The operator to do this is !=. It can be illustrated as follows:
A typical Boolean expression involves two operands separated by a logical operator. Both operands must be of the same type. These rules apply to the logical difference. It can be used on numbers, strings, etc. If both operands are different, the operation produces a True result. If they are the exact same, the operation produces False.
The != operator can be used the same way as the equality operator (==) as long as you keep in mind that != is the opposite of ==.
Unlike the "Less Than" (<) and the "Greater Than" (>) operations, the logical difference (!=) operator cannot use the is operator.
Less Than Or Equal To: <=
The Equality (==) and the Less Than (<) operations can be combined to compare two values. This allows you to know if two values are the same or if the first value is lower than the second value. The operator used is <= and its syntax is:
value1 is <= value2
The <= operation performs a comparison as any of the last two. If both value1 and value2 hold the same value, the result is True. If the left operand, in this case value1, holds a value lower than the second operand, in this case value2, the result is still True. The <= operation can be illustrated as follows:
Here is an example that uses the <= operator:
using static System.Console;
double originalPrice = 124.50;
double discountRate = 35.00; // %
double number_of_days_in_store = 75;
if (number_of_days_in_store is <= 45)
discountRate = 25.00;
double discountAmount = originalPrice * discountRate / 100.00;
double markedPrice = originalPrice - discountAmount;
WriteLine("Fun Department Store");
WriteLine("----------------------------------");
WriteLine($"Original Price: {originalPrice}");
WriteLine($"Days in Store: {number_of_days_in_store}");
WriteLine($"Discount Rate: {discountRate:n}");
WriteLine($"Discount Amount: {discountAmount:n}");
WriteLine($"Marked Price: {markedPrice:n}");
WriteLine("==================================");
number_of_days_in_store = 22;
if (number_of_days_in_store is <= 45)
discountRate = 25.00;
discountAmount = originalPrice * discountRate / 100.00;
markedPrice = originalPrice - discountAmount;
WriteLine("Fun Department Store");
WriteLine("---------------------------------");
WriteLine($"Original Price: {originalPrice:N}");
WriteLine($"Days in Store: {number_of_days_in_store}");
WriteLine($"Discount Rate: {discountRate:N}");
WriteLine($"Discount Amount: {discountAmount:N}");
WriteLine($"Marked Price: {markedPrice:N}");
WriteLine("==================================");
This would produce:
Fun Department Store ---------------------------------- Original Price: 124.5 Days in Store: 75 Discount Rate: 35.00 Discount Amount: 43.58 Marked Price: 80.92 ================================== Fun Department Store --------------------------------- Original Price: 124.50 Days in Store: 22 Discount Rate: 25.00 Discount Amount: 31.12 Marked Price: 93.38 ================================== Press any key to close this window . . .
Practical Learning: Comparing for a Lesser Value
using static System.Console;
WriteLine("FUN DEPARTMENT STORE");
WriteLine("=======================================================");
WriteLine("Payroll Preparation");
WriteLine("-------------------------------------------------------");
WriteLine("Enter the following pieces of information");
WriteLine("-------------------------------------------------------");
WriteLine("Employee Information");
WriteLine("-------------------------------------------------------");
Write("First Name: ");
string firstName = ReadLine();
Write("Last Name: ");
string lastName = ReadLine();
Write("Hourly Salary: ");
double hSalary = double.Parse(ReadLine());
WriteLine("-------------------------------------------------------");
WriteLine("Time worked");
WriteLine("-------------------------------------------------------");
Write("Monday: ");
double mon = double.Parse(ReadLine());
Write("Tuesday: ");
double tue = double.Parse(ReadLine());
Write("Wednesday: ");
double wed = double.Parse(ReadLine());
Write("Thursday: ");
double thu = double.Parse(ReadLine());
Write("Friday: ");
double fri = double.Parse(ReadLine());
double timeWorked = mon + tue + wed + thu + fri;
double regTime = 40.00;
double regPay = hSalary * 40.00;
double overtime = timeWorked - 40.00;
double overPay = hSalary * 1.50 * overtime;
if( timeWorked is <= 40.00)
{
regTime = timeWorked;
regPay = hSalary * timeWorked;
overtime = 0.00;
overPay = 0.00;
}
double netPay = regPay + overPay;
WriteLine("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+");
WriteLine("FUN DEPARTMENT STORE");
WriteLine("=======================================================");
WriteLine("Payroll Evaluation");
WriteLine("=======================================================");
WriteLine("Employee Information");
WriteLine("-------------------------------------------------------");
WriteLine($"Full Name: {firstName} {lastName}");
WriteLine($"Hourly Salary: {hSalary:f}");
WriteLine("=======================================================");
WriteLine("Time Worked Summary");
WriteLine("--------+---------+-----------+----------+-------------");
WriteLine(" Monday | Tuesday | Wednesday | Thursday | Friday");
WriteLine("--------+---------+-----------+----------+-------------");
WriteLine($" {mon:f} | {tue:f} | {wed:f} | {thu:f} | {fri:f}");
WriteLine("========+=========+===========+==========+=============");
WriteLine(" Pay Summary");
WriteLine("-------------------------------------------------------");
WriteLine(" Time Pay");
WriteLine("-------------------------------------------------------");
WriteLine($" Regular: {regTime:f} {regPay:f}");
WriteLine("-------------------------------------------------------");
WriteLine($" Overtime: {overtime:f} {overPay:f}");
WriteLine("=======================================================");
WriteLine($" Net Pay: {netPay:f}");
WriteLine("=======================================================");
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information First Name: Michael Last Name: Carlock Hourly Salary: 28.25 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 7 Tuesday: 8 Wednesday: 6.5 Thursday: 8.5 Friday: 6.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Michael Carlock Hourly Salary: 28.25 ======================================================= Work Preparation --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 7.00 8.00 6.50 8.50 6.50 ======================================================= Pay Summary ------------------------------------------------------- Time Pay ------------------------------------------------------- Regular: 36.50 1031.12 ------------------------------------------------------- Overtime: 0.00 0.00 ======================================================= Net Pay: 1031.12 ======================================================= Press any key to close this window . . .
FUN DEPARTMENT STORE ======================================================= Payroll Preparation ------------------------------------------------------- Enter the following pieces of information ------------------------------------------------------- Employee Information First Name: Catherine Last Name: Busbey Hourly Salary: 24.37 ------------------------------------------------------- Time worked ------------------------------------------------------- Monday: 9.5 Tuesday: 8 Wednesday: 10.5 Thursday: 9 Friday: 10.5 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ FUN DEPARTMENT STORE ======================================================= Payroll Evaluation ======================================================= Employee Information ------------------------------------------------------- Full Name: Catherine Busbey Hourly Salary: 24.37 ======================================================= Work Preparation --------+---------+-----------+----------+------------- Monday | Tuesday | Wednesday | Thursday | Friday --------+---------+-----------+----------+------------- 9.50 8.00 10.50 9.00 10.50 ======================================================= Pay Summary ------------------------------------------------------- Time Pay ------------------------------------------------------- Regular: 40.00 974.80 ------------------------------------------------------- Overtime: 7.50 274.16 ======================================================= Net Pay: 1248.96 ======================================================= Press any key to close this window . . .
using static System.Console; WriteLine("==================================================================="); WriteLine("Machine Depreciation Evaluation - Straight-Line Method"); WriteLine("==================================================================="); WriteLine("Enter the values to evaluation the depreciation of the machine"); Write("Machine Cost: "); double machineCost = double.Parse(ReadLine()); Write("Salvage Value: "); double salvageValue = double.Parse(ReadLine()); Write("Estimated Life: "); int estimatedLife = int.Parse(ReadLine()); double depreciationRate = 100 / estimatedLife; double yearlyDepreciation = (machineCost - salvageValue) / estimatedLife; double bookValueYear0 = machineCost - (yearlyDepreciation * 0); double bookValueYear1 = machineCost - (yearlyDepreciation * 1); double bookValueYear2 = machineCost - (yearlyDepreciation * 2); double bookValueYear3 = machineCost - (yearlyDepreciation * 3); double bookValueYear4 = machineCost - (yearlyDepreciation * 4); double bookValueYear5 = machineCost - (yearlyDepreciation * 5); double bookValueYear6 = machineCost - (yearlyDepreciation * 6); double bookValueYear7 = machineCost - (yearlyDepreciation * 7); double bookValueYear8 = machineCost - (yearlyDepreciation * 8); double bookValueYear9 = machineCost - (yearlyDepreciation * 9); double bookValueYear10 = machineCost - (yearlyDepreciation * 10); double accumulatedDepreciation1 = yearlyDepreciation * 1; double accumulatedDepreciation2 = yearlyDepreciation * 2; double accumulatedDepreciation3 = yearlyDepreciation * 3; double accumulatedDepreciation4 = yearlyDepreciation * 4; double accumulatedDepreciation5 = yearlyDepreciation * 5; double accumulatedDepreciation6 = yearlyDepreciation * 6; double accumulatedDepreciation7 = yearlyDepreciation * 7; double accumulatedDepreciation8 = yearlyDepreciation * 8; double accumulatedDepreciation9 = yearlyDepreciation * 9; double accumulatedDepreciation10 = yearlyDepreciation * 10; WriteLine("==================================================================="); WriteLine("Machine Depreciation Evaluation - Straight-Line Method"); WriteLine("==================================================================="); WriteLine($"Machine Cost: {machineCost:n}"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Salvage Calue: {salvageValue:n}"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Estimated Life: {estimatedLife:d} years"); WriteLine("==================================================================="); WriteLine($"Depreciation Rate: {depreciationRate:n}%"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Yearly Depreciation: {yearlyDepreciation:n}/year"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Monthly Depreciation: {(yearlyDepreciation / 12):n}/month"); WriteLine("=====+=====================+============+=========================="); WriteLine("Year | Yearly Depreciation | Book Value | Accumulated Depreciation"); WriteLine("-----+---------------------+------------+--------------------------"); int year = 0; WriteLine("{0,3} | |{1,10:n} |", year, bookValueYear0); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 1, yearlyDepreciation, bookValueYear1, accumulatedDepreciation1); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 2, yearlyDepreciation, bookValueYear2, accumulatedDepreciation2); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 3, yearlyDepreciation, bookValueYear3, accumulatedDepreciation3); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 4, yearlyDepreciation, bookValueYear4, accumulatedDepreciation4); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 5, yearlyDepreciation, bookValueYear5, accumulatedDepreciation5); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 6, yearlyDepreciation, bookValueYear6, accumulatedDepreciation6); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 7, yearlyDepreciation, bookValueYear7, accumulatedDepreciation7); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 8, yearlyDepreciation, bookValueYear8, accumulatedDepreciation8); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 9, yearlyDepreciation, bookValueYear9, accumulatedDepreciation9); WriteLine("-----+---------------------+------------+--------------------------"); WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 10, yearlyDepreciation, bookValueYear10, accumulatedDepreciation10); WriteLine("=====+=====================+============+==========================");
=================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Enter the values to evaluation the depreciation of the machine Machine Cost: 8568.95 Salvage Value: 550 Estimated Life: 5 =================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Machine Cost: 8,568.95 ------------------------------------------------------------------- Salvage Calue: 550.00 ------------------------------------------------------------------- Estimated Life: 5 years =================================================================== Depreciation Rate: 20.00% ------------------------------------------------------------------- Yearly Depreciation: 1,603.79/year ------------------------------------------------------------------- Monthly Depreciation: 133.65/month =====+=====================+============+========================== Year | Yearly Depreciation | Book Value | Accumulated Depreciation -----+---------------------+------------+-------------------------- 0 | | 8,568.95 | -----+---------------------+------------+-------------------------- 1 | 1,603.79 | 6,965.16 | 1,603.79 -----+---------------------+------------+-------------------------- 2 | 1,603.79 | 5,361.37 | 3,207.58 -----+---------------------+------------+-------------------------- 3 | 1,603.79 | 3,757.58 | 4,811.37 -----+---------------------+------------+-------------------------- 4 | 1,603.79 | 2,153.79 | 6,415.16 -----+---------------------+------------+-------------------------- 5 | 1,603.79 | 550.00 | 8,018.95 -----+---------------------+------------+-------------------------- 6 | 1,603.79 | -1,053.79 | 9,622.74 -----+---------------------+------------+-------------------------- 7 | 1,603.79 | -2,657.58 | 11,226.53 -----+---------------------+------------+-------------------------- 8 | 1,603.79 | -4,261.37 | 12,830.32 -----+---------------------+------------+-------------------------- 9 | 1,603.79 | -5,865.16 | 14,434.11 -----+---------------------+------------+-------------------------- 10 | 1,603.79 | -7,468.95 | 16,037.90 =====+=====================+============+========================== Press any key to close this window . . .
=================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Enter the values to evaluation the depreciation of the machine Machine Cost: 15888.65 Salvage Value: 1250 Estimated Life: 10 =================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Machine Cost: 15889 ------------------------------------------------------------------- Salvage Calue: 1250 ------------------------------------------------------------------- Estimated Life: 10 years =================================================================== Depreciation Rate: 10 % ------------------------------------------------------------------- Yearly Depreciation: 1464/year ------------------------------------------------------------------- Monthly Depreciation: 122/month =====+=====================+============+========================== Year | Yearly Depreciation | Book Value | Accumulated Depreciation -----+---------------------+------------+-------------------------- 0 | | 15888.65 | -----+---------------------+------------+-------------------------- 1 | 1463.87 | 14424.78 | 1463.87 -----+---------------------+------------+-------------------------- 2 | 1463.87 | 12960.92 | 2927.73 -----+---------------------+------------+-------------------------- 3 | 1463.87 | 11497.06 | 4391.60 -----+---------------------+------------+-------------------------- 4 | 1463.87 | 10033.19 | 5855.46 -----+---------------------+------------+-------------------------- 5 | 1463.87 | 8569.33 | 7319.32 -----+---------------------+------------+-------------------------- 6 | 1463.87 | 7105.46 | 8783.19 -----+---------------------+------------+-------------------------- 7 | 1463.87 | 5641.59 | 10247.06 -----+---------------------+------------+-------------------------- 8 | 1463.87 | 4177.73 | 11710.92 -----+---------------------+------------+-------------------------- 9 | 1463.87 | 2713.86 | 13174.78 -----+---------------------+------------+-------------------------- 10 | 1463.87 | 1250.00 | 14638.65 Press any key to close this window . . .
A Value Greater Than or Equal to Another: >=
The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. The formula it follows is:
value1 is >= value2
The comparison is performed on both operands: value1 and value2:
This operation can be illustrated as follows:
Here is an example that uses the >= operator:
using static System.Console;
double consumption = 0.74;
double pricePerCCF = 0.00;
if (consumption is >= 0.50)
pricePerCCF = 35.00;
double monthlyCharges = consumption * pricePerCCF;
WriteLine("Gas Utility Company");
WriteLine("---------------------------------");
WriteLine($"Gas Consumption: {consumption}");
WriteLine($"Price Per CCF: {pricePerCCF}");
WriteLine($"Monthly Charges: {monthlyCharges}");
WriteLine("==================================");
This would produce:
Gas Utility Company --------------------------------- Gas Consumption: 0.74 Price Per CCF: 35.0 Monthly Charges: 25.9 ================================== Press any key to close this window . . .
Practical Learning: Comparing for a Value Greater Than or Equal to Another
using static System.Console; WriteLine("==================================================================="); WriteLine("Machine Depreciation Evaluation - Straight-Line Method"); WriteLine("==================================================================="); WriteLine("Enter the values to evaluation the depreciation of the machine"); Write("Machine Cost: "); double machineCost = double.Parse(ReadLine()); Write("Salvage Value: "); double salvageValue = double.Parse(ReadLine()); Write("Estimated Life: "); int estimatedLife = int.Parse(ReadLine()); double depreciationRate = 100 / estimatedLife; double yearlyDepreciation = (machineCost - salvageValue) / estimatedLife; double bookValueYear0 = machineCost - (yearlyDepreciation * 0); double bookValueYear1 = machineCost - (yearlyDepreciation * 1); double bookValueYear2 = machineCost - (yearlyDepreciation * 2); double bookValueYear3 = machineCost - (yearlyDepreciation * 3); double bookValueYear4 = machineCost - (yearlyDepreciation * 4); double bookValueYear5 = machineCost - (yearlyDepreciation * 5); double bookValueYear6 = machineCost - (yearlyDepreciation * 6); double bookValueYear7 = machineCost - (yearlyDepreciation * 7); double bookValueYear8 = machineCost - (yearlyDepreciation * 8); double bookValueYear9 = machineCost - (yearlyDepreciation * 9); double bookValueYear10 = machineCost - (yearlyDepreciation * 10); double accumulatedDepreciation1 = yearlyDepreciation * 1; double accumulatedDepreciation2 = yearlyDepreciation * 2; double accumulatedDepreciation3 = yearlyDepreciation * 3; double accumulatedDepreciation4 = yearlyDepreciation * 4; double accumulatedDepreciation5 = yearlyDepreciation * 5; double accumulatedDepreciation6 = yearlyDepreciation * 6; double accumulatedDepreciation7 = yearlyDepreciation * 7; double accumulatedDepreciation8 = yearlyDepreciation * 8; double accumulatedDepreciation9 = yearlyDepreciation * 9; double accumulatedDepreciation10 = yearlyDepreciation * 10; WriteLine("==================================================================="); WriteLine("Machine Depreciation Evaluation - Straight-Line Method"); WriteLine("==================================================================="); WriteLine($"Machine Cost: {machineCost:n}"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Salvage Calue: {salvageValue:n}"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Estimated Life: {estimatedLife:d} years"); WriteLine("==================================================================="); WriteLine($"Depreciation Rate: {depreciationRate:n}%"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Yearly Depreciation: {yearlyDepreciation:n}/year"); WriteLine("-------------------------------------------------------------------"); WriteLine($"Monthly Depreciation: {(yearlyDepreciation / 12):n}/month"); WriteLine("=====+=====================+============+=========================="); WriteLine("Year | Yearly Depreciation | Book Value | Accumulated Depreciation"); WriteLine("-----+---------------------+------------+--------------------------"); int year = 0; WriteLine("{0,3} | |{1,10:n} |", year, bookValueYear0); WriteLine("-----+---------------------+------------+--------------------------"); if (bookValueYear1 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 1, yearlyDepreciation, bookValueYear1, accumulatedDepreciation1); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear2 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 2, yearlyDepreciation, bookValueYear2, accumulatedDepreciation2); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear3 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 3, yearlyDepreciation, bookValueYear3, accumulatedDepreciation3); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear4 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 4, yearlyDepreciation, bookValueYear4, accumulatedDepreciation4); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear5 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 5, yearlyDepreciation, bookValueYear5, accumulatedDepreciation5); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear6 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 6, yearlyDepreciation, bookValueYear6, accumulatedDepreciation6); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear7 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 7, yearlyDepreciation, bookValueYear7, accumulatedDepreciation7); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear8 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 8, yearlyDepreciation, bookValueYear8, accumulatedDepreciation8); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear9 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 9, yearlyDepreciation, bookValueYear9, accumulatedDepreciation9); WriteLine("-----+---------------------+------------+--------------------------"); } if (bookValueYear10 is >= 0) { WriteLine("{0,3} | {1:n} |{2,10:n} | {3,10:n}", year + 10, yearlyDepreciation, bookValueYear10, accumulatedDepreciation10); WriteLine("=====+=====================+============+=========================="); }
=================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Enter the values to evaluation the depreciation of the machine Machine Cost: 8568.95 Salvage Value: 550 Estimated Life: 5 =================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Machine Cost: 8,568.95 ------------------------------------------------------------------- Salvage Calue: 550.00 ------------------------------------------------------------------- Estimated Life: 5 years =================================================================== Depreciation Rate: 20.00% ------------------------------------------------------------------- Yearly Depreciation: 1,603.79/year ------------------------------------------------------------------- Monthly Depreciation: 133.65/month =====+=====================+============+========================== Year | Yearly Depreciation | Book Value | Accumulated Depreciation -----+---------------------+------------+-------------------------- 0 | | 8,568.95 | -----+---------------------+------------+-------------------------- 1 | 1,603.79 | 6,965.16 | 1,603.79 -----+---------------------+------------+-------------------------- 2 | 1,603.79 | 5,361.37 | 3,207.58 -----+---------------------+------------+-------------------------- 3 | 1,603.79 | 3,757.58 | 4,811.37 -----+---------------------+------------+-------------------------- 4 | 1,603.79 | 2,153.79 | 6,415.16 -----+---------------------+------------+-------------------------- 5 | 1,603.79 | 550.00 | 8,018.95 -----+---------------------+------------+-------------------------- Press any key to close this window . . .
=================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Enter the values to evaluation the depreciation of the machine Machine Cost: 15888.65 Salvage Value: 1250 Estimated Life: 10 =================================================================== Machine Depreciation Evaluation - Straight-Line Method =================================================================== Machine Cost: 15,888.65 ------------------------------------------------------------------- Salvage Calue: 1,250.00 ------------------------------------------------------------------- Estimated Life: 10 years =================================================================== Depreciation Rate: 10.00% ------------------------------------------------------------------- Yearly Depreciation: 1,463.87/year ------------------------------------------------------------------- Monthly Depreciation: 121.99/month =====+=====================+============+========================== Year | Yearly Depreciation | Book Value | Accumulated Depreciation -----+---------------------+------------+-------------------------- 0 | | 15,888.65 | -----+---------------------+------------+-------------------------- 1 | 1,463.87 | 14,424.78 | 1,463.87 -----+---------------------+------------+-------------------------- 2 | 1,463.87 | 12,960.92 | 2,927.73 -----+---------------------+------------+-------------------------- 3 | 1,463.87 | 11,497.06 | 4,391.60 -----+---------------------+------------+-------------------------- 4 | 1,463.87 | 10,033.19 | 5,855.46 -----+---------------------+------------+-------------------------- 5 | 1,463.87 | 8,569.33 | 7,319.32 -----+---------------------+------------+-------------------------- 6 | 1,463.87 | 7,105.46 | 8,783.19 -----+---------------------+------------+-------------------------- 7 | 1,463.87 | 5,641.59 | 10,247.06 -----+---------------------+------------+-------------------------- 8 | 1,463.87 | 4,177.73 | 11,710.92 -----+---------------------+------------+-------------------------- 9 | 1,463.87 | 2,713.86 | 13,174.78 -----+---------------------+------------+-------------------------- 10 | 1,463.87 | 1,250.00 | 14,638.65 =====+=====================+============+========================== Press any key to close this window . . .
Options on Conditional Statements
if a Condition is True/False
One way to formulate a conditional statement is to find out whether a situation is true or false. In this case, one of the operands must be a Boolean value as true or false. The other operand can be a Boolean variable. Here is an example:
bool employeeIsFullTime = false;; if( false == employeeIsFullTime) { }
One of the operands can also be an expression that holds a Boolean value.
Imagine that you want to evaluate the condition as true. Here is an example:
bool employeeIsFullTime = true; if( employeeIsFullTime == true) { }
If a Boolean variable (currently) holds a true value (at the time you are trying to access it), when you are evaluating the expression as being true, you can omit the == true or the true == expression in your statement. Therefore, the above expression can be written as follows:
bool employeeIsFullTime = true;
. . . No Change
if( employeeIsFullTime)
{
}
This would produce the same result as previously.
Negating a Condition
On the other hand, if you have a logical expression or value, to let you get its logical opposite, the C# language provides the logical NOT operator represented by !. The formula to use it is:
!variable-or-expression
Actually there are various ways the logical NOT operator is used. The classic way is to check the state of a variable. To nullify a variable, you can write the exclamation point to its left. Here is an example:
bool employed = true;
bool validation = !employed;
When a variable has been "negated", its logical value changes. If the logical value was true, it would be changed to false. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it.
If There Is No Need
We saw that the <, the <=, the >, and the >= operators can be preceded by the is keyword. To make your code a little simpler, you can omit the is keyword. Here are examples:
using static System.Console; WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Item Preparation"); WriteLine("-------------------------------------------------------"); WriteLine("Enter the following pieces of information"); WriteLine("-------------------------------------------------------"); int discountRate = 75; double discountAmount = 0.00; double discountedPrice = 0.00; Write("Item Name: "); string itemName = ReadLine(); Write("Original Price: "); double originalPrice = double.Parse(ReadLine()); Write("Days in Store: "); int daysInStore = int.Parse(ReadLine()); if (daysInStore < 60) discountRate = 50; if (daysInStore < 45) discountRate = 35; if (daysInStore < 35) discountRate = 15; if (daysInStore < 15) discountRate = 0; discountAmount = originalPrice * discountRate / 100; discountedPrice = originalPrice - discountAmount; WriteLine("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+"); WriteLine("FUN DEPARTMENT STORE"); WriteLine("======================================================="); WriteLine("Store Inventory"); WriteLine("-------------------------------------------------------"); WriteLine($"Item Name: {itemName}"); WriteLine($"Original Price: {originalPrice}"); WriteLine($"Days in Store: {daysInStore}"); WriteLine($"Discount Rate: {discountRate}%"); WriteLine($"Discount Amount: {discountAmount:f}"); WriteLine($"Discounted Price: {discountedPrice:f}"); WriteLine("=======================================================");
Otherwise, to make your code easy to read, you can use the is keyword.
Once again, we saw that the <, the <=, the >, and the >= operators can be preceded by the is keyword. Here is an example that uses the >= operator:
using static System.Console;
WriteLine("================================================================");
WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("----------------------------------------------------------------");
Write("Type your level (1-4): ");
int level = int.Parse(ReadLine());
if(level is >= 3)
{
WriteLine("================================================================");
WriteLine("Welcome on board.");
}
else
WriteLine("We will get back to you...");
WriteLine("================================================================");
Here is an example of running the program:
================================================================ To grant you this job, we need to ask a few questions. Levels of Security Clearance: 0. Unknown or None 1. Non-Sensitive 2. National Security - Non-Critical Sensitive 3. National Security - Critical Sensitive 4. National Security - Special Sensitive ---------------------------------------------------------------- Type your level (1-4): 3 ================================================================ Welcome on board. ================================================================ Press any key to close this window . . .
To make your code easy to read, you can include the is operator, the Boolean operator, and the second operand in parentheses. Here is an example:
using static System.Console;
WriteLine("================================================================");
WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("----------------------------------------------------------------");
Write("Type your level (1-4): ");
int level = int.Parse(ReadLine());
if(level (is >= 3))
{
WriteLine("================================================================");
WriteLine("Welcome on board.");
}
else
WriteLine("We will get back to you...");
WriteLine("================================================================");
To get the opposite result of those operator, you can use an operator named not. The expression to use is is not. Here is an example:
using static System.Console;
WriteLine("================================================================");
WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("----------------------------------------------------------------");
Write("Type your level (1-4): ");
int level = int.Parse(ReadLine());
if(level is not >= 3)
{
WriteLine("================================================================");
WriteLine("We will get back to you...");
}
else
WriteLine("Welcome on board.");
WriteLine("================================================================");
Here is an example of running the program:
================================================================ To grant you this job, we need to ask a few questions. Levels of Security Clearance: 0. Unknown or None 1. Non-Sensitive 2. National Security - Non-Critical Sensitive 3. National Security - Critical Sensitive 4. National Security - Special Sensitive ---------------------------------------------------------------- Type your level (1-4): 2 ================================================================ We will get back to you... ================================================================ Press any key to close this window . . .
To make your code easy to read, you can include the is not expression, the Boolean operator, and the second operand in parentheses. Here is an example:
using static System.Console;
WriteLine("================================================================");
WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("----------------------------------------------------------------");
Write("Type your level (1-4): ");
int level = int.Parse(ReadLine());
if(level (is not >= 3))
{
WriteLine("================================================================");
WriteLine("We will get back to you...");
}
else
WriteLine("Welcome on board.");
WriteLine("================================================================");
A Summary of Logical Operators
As you might have found out, every logical operator has an opposite. They can be resumed as follows:
Operator | Meaning | Example | Opposite | |
== | Equality to | a == b | != | |
!= | Not equal to | 12 != 7 | == | |
< | is < | Less than | 25 < 84 | >= |
<= | is <= | Less than or equal to | Cab <= Tab | > |
> | is > | Greater than | 248 > 55 | <= |
>= | is >= | Greater than or equal to | Val1 >= Val2 | < |
|
|||
Previous | Copyright © 2001-2022, FunctionX | Monday 06 December 2021 | Next |
|