The logical operators let you test more than one expression.
A double ampersand, && means AND. A double pipe or bar || means OR. The pipe is typed using shift + backspace.
Truth table
a |
b |
a&&b |
a||b |
false |
false |
false |
false |
false |
true |
false |
true |
true |
false |
false |
true |
true |
true |
true |
true |
Notice that || (or) is an inclusive or. If the boss says you will get a bonus if you get 10 new customers or sell 100 widgets, then you would get the bonus if you did both.
int newCustomers = 12;
int widgetsSold = 100;
if (newCustomers>=10 || widgetsSold>=100) Text = "You get a bonus";
else Text = "Sorry, you don't get a bonus";
Also, in English we might say "Children and Senior get a discount." However one person can't be both a child and a senior. Reword this for the inidvidual: "You will get a bonus is you are a child or a senior."
if (age <= 12 || age >= 55) Text = "You get a discount";
else Text = "Sorry, you don't get a discount";
All of these examples can be tested by starting a new Windows form app and testing in form load without adding any controls.
If you want to really test, you can add a scroll bar to select the values.
End of lesson, Next lesson: