All the usual arithmetic comparisons may be made, but many do not use standard mathematical symbolism, mostly for lack of proper keys on a standard keyboard.
Meaning | Math Symbol | C# Symbols |
---|---|---|
Less than | \(<\) | < |
Greater than | \(>\) | > |
Less than or equal | \(\leq\) | <= |
Greater than or equal | \(\geq\) | >= |
Equals | = | == |
Not equal | \(\neq\) | != |
There should not be space between the two-symbol C# substitutes.
Notice that the obvious choice for equals, a single equal sign, is not used to check for equality. An annoying second equal sign is required. This is because the single equal sign is already used for assignment in C#, so it is not available for tests.
Warning
It is a common error to use only one equal sign when you mean to test for equality, and not make an assignment!
Tests for equality do not make an assignment, and they do not require a variable on the left.
All these tests work for numbers, and characters. Strings can be tested for equality or inequality (!=).
Predict the results and try each line in csharp:
x = 5;
x;
x == 5;
x == 6;
x;
x != 6;
x = 6;
6 == x;
6 != x;
"hi" == "h" + "i";
"HI" != "hi";
An equality check does not make an assignment. Strings are case sensitive.
Try this: Following up on the discussion of the inexactness of float arithmetic, confirm that C# does not consider .1 + .2 to be equal to .3: Write a simple condition into csharp to test.
Pay with Overtime Example
Given a person’s work hours for the week and regular hourly wage, calculate the total pay for the week, taking into account overtime. Hours worked over 40 are overtime, paid at 1.5 times the normal rate. This is a natural place for a function enclosing the calculation.
Read the setup for the function:
/** Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
*/
static double CalcWeeklyWages(double totalHours, double hourlyWage)
The problem clearly indicates two cases: when no more than 40 hours are worked or when more than 40 hours are worked. In case more than 40 hours are worked, it is convenient to introduce a variable overtimeHours. You are encouraged to think about a solution before going on and examining mine.
You can try running my complete example program, Wages1.cs, also shown below. The program uses the keyboard input functions developed in class.
using System;
class Wages
{ //heading chunk
/** Return the total weekly wages for a worker working totalHours,
with a given regular hourlyWage. Include overtime for hours over 40.
*/
static double CalcWeeklyWages(double totalHours, double hourlyWage)
{ //body chunk
double totalWages;
if (totalHours <= 40) {
totalWages = hourlyWage*totalHours;
}
else {
double overtime = totalHours - 40;
totalWages = hourlyWage*40 + (1.5*hourlyWage)*overtime;
}
return totalWages;
}
//
static void Main()
{
double hours = promptDouble("Enter hours worked: ");
double wage = promptDouble("Enter dollars paid per hour: ");
double total = CalcWeeklyWages(hours, wage); //before chunk2
Console.WriteLine(
"Wages for {0} hours at ${1:F2} per hour are ${2:F2}.",
hours, wage, total);
} //after chunk2
/** Prompt user and return a line read from the keyboard.*/
static string promptLine(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
/** Prompt user and return a double read from the keyboard.*/
static double promptDouble(string prompt)
{
return double.Parse(promptLine(prompt)); //assumes legal format for now
}
}
This program also introduces new notation for displaying decimal numbers:
Console.WriteLine(
"Wages for {0} hours at ${1:F2} per hour are ${2:F2}.",
hours, wage, total);
Inside the format string you see {1:F2} and {2:F2}: inside the braces, after the parameter index, you see a new part, :F2. The part after the colon gives optional formatting information. In this case display with the decimal point fixed so 2 places beyond the decimal point are shown. Also the result is rounded. This is appropriate for money with dollars and cents. Replace the 2 to display a different number of digits after the decimal point. More formatting instructions will be discussed later.
Below is an equivalent alternative version of the body of CalcWeeklyWages, used in Wages2.cs. It uses just one general calculation formula and sets the parameters for the formula in the if statement. There are generally a number of ways you might solve the same problem!
double regularHours, overtime;
if (totalHours <= 40) {
regularHours = totalHours;
overtime = 0;
}
else {
regularHours = 40;
overtime = totalHours - 40;
}
return hourlyWage*regularHours + (1.5*hourlyWage)*overtime;
Write a program, Graduate.cs, that prompts students for how many credits they have. Print whether of not they have enough credits for graduation. (At Loyola University Chicago 120 credits are needed for graduation.)