(boolean-logic)=
# Boolean logic

[The questions we had previously](control-flow) are in fact Boolean expressions.

:::{wpd} Boolean expression
an expression that produces a Boolean value. Such a value is either true or false.
:::

Type is `bool`{l=cs}, which stands for *Boolean*.

These operators create a Boolean expression from other datatypes. Technically they input other datatypes and output a `bool`{l=cs}
- `>`, `>=`
- `<`, `<=`
- `==`, `!=`

These operators combine Boolean expressions. Technically they input `bool`{l=cs} variables and output a `bool`{l=cs}.
- `&&`
- `||`

`!` negates a Boolean expression. In other words, if `a = true`, then `!a` is `false`.

We use Boolean logic in branching (`if-else`, `switch`) and control loops (`while`, `for`).

## Examples


```cs
// Defining a Boolean variable
var iAmHavingFun = true;

// Simple boolean comparisons
var isEqual = 5 == 5; // true
var isNotEqual = 5 != 5; // false
var isGreater = 10 > 5; // true
var isLess = 5 < 10; // true
var isGreaterThanEqual = 5 >= 5; // true
var isLessThanEqual = 10 <= 5; // false

// Combination operators
var andCondition = 5 > 3 && 10 < 20; // true (both must be true)
var orCondition = 5 > 10 || 3 < 4; // true (at least one must be true)
var notCondition = !(5 == 5); // false (negation)

// Functions can also return Boolean values
List<string> fruits = ["apple", "banana", "cherry"];
var containsApple = fruits.Contains("apple"); // true
var containsOrange = fruits.Contains("orange"); // false

// Practical example: Check if a number is within a range
var number = 7;
var isInRange = number > 5 && number < 10; // true
// The last compound expression beautified using the `is` operator:
var isInRange2 = number is > 5 and < 10;
```


:::{activity}
:label: bool-expr-which-false
Which of the following are Boolean expressions *and* evaluate to `false`, assuming the following initialization code? The statements in the choices do not affect others.

```cs
var t = true;
var f = false;
var c = 3;
var i = 9;
var k = 11;
```
Choices:
1. `t == f`{l=cs}
2. `t = f`{l=cs}
3. `i >= c && k > i`{l=cs}
4. `!t`{l=cs}
5. `k != 11`{l=cs}
6. `10 < c || c <= 20`{l=cs} 
:::

:::{activity} Creating Boolean variables for user access check
:label: creating-boolean-variables-for-user-access-check
You are implementing the user access logic for a program. Create Boolean expressions for the following sentences:

- The user is an administrator if the user id is larger than `65536`
- the username has at least three characters.
- password must contain at least one of the characters `$`, `|` and `@`. 
- password must be at least 20 characters long for an admin and 16 characters long for a non-admin

Use the variables:
```cs
// Inputs
string username;
string password;
uint userId; 

// Outputs
var userIsAdmin = false;
var usernameValid = false;
var passwordValid = false;
```

User the following string functions:
- if `u` is a `string`:
  - `u.Length` gives the length
  - `u.Contains('s')` return `true` if `s` is in the string
:::

:::{exercise}
:label: user-access-check

Create a program for {ref}`creating-boolean-variables-for-user-access-check` that:
- asks the user to enter the input variables `username`, `password` etc.
- prints a meaningful message if both username and password are valid.
- prints a meaningful message/s if username or password are not valid.
:::


## Appendix
- [Boolean logical operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators)
- [The `is` operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/is)