To be eligible to graduate from Loyola University Chicago, you must have 120 credits and a GPA of at least 2.0. C# does not use the word and. Instead it uses &&. Then the statement translates directly into C# as a compound condition:
credits >= 120 && GPA >=2.0
This is true if both credits >= 120 is true and GPA >= 2.0 is true. A short example function using this would be:
static void checkGraduation(int credits, double GPA)
{
if (credits >= 120 && GPA >=2.0) {
Console.WriteLine('You are eligible to graduate!')
}
else {
Console.WriteLine('You are not eligible to graduate.')
}
}
The new C# syntax for the operator &&:
condition1 && condition2
The compound condition is true if both of the component conditions are true. It is false if at least one of the conditions is false.
Suppose we want a condition that is true if the mathematical condition is true: low < val < high. Unfortunately the math is not a C# expression. The operator < is binary. There is a C# version:
low < val && val < high
Now suppose we want the opposite condition: that val is not strictly between low and high. There are several approaches. One is that val would be less than or equal to low or greater than or equal to high. C# translate or into ||, so a C# expression would be:
val <= low || val >= high
The new C# syntax for the operator ||:
condition1 || condition2
The compound condition is true if at least one of the component conditions are true. It is false if at both conditions are false.
Another logical way to express the opposite of the condition low < val < high is that it is not the case that low < val && val << high. C# translates not as !. Another way to state the condition would be
!(low < val && val < high)
The parentheses are needed because the ! operator has higher precedence than <.
A way to remember this strange not operator is to think of the use of ! in the not-equal operator: !=
The new C# syntax for the operator !:
! condition
This whole expression is true when condition is false, and false when condition is true.
Because of the precedence of !, you are often going to write:
!( condition )
Remember when such a condition is used in an if statement, outer parentheses are also needed:
if (!( condition )) {
For other examples and different words of introduction to if statements, braces, and compound conditions, you might look at Miles, section 2.3.2.
A person is eligible to be a US Senator who is at least 30 years old and has been a US citizen for at least 9 years. Write a version of a program Congress.cs to obtain age and length of citizenship from the user and print out if a person is eligible to be a Senator or not. A person is eligible to be a US Representative who is at least 25 years old and has been a US citizen for at least 7 years. Elaborate your program Congress.cs so it obtains age and length of citizenship and prints whether a person is eligible to be a US Representative only, or is eligible for both offices, or is eligible for neither.