Fall 2020
October 3, 2020
Please, refer to the second milestone for a brief recap of what we’ve done recently.
if, else, switch, while, do...while and for.A condition is either true or false. We can store if something is true or false in a (boolean) flag, which is simply a variable of type boolean.
We can declare, assign, initialize and display it as any other variable:
But the only two possible values are true and false, and we will study three operations on them: “and” (&&, the conjonction), “or” (||, the disjunction) and “not” (!, the negation). They have the expected meaning that the condition “A and B” is true if and only if A is true, and B is true. Similarly, “A or B” is false if and only if A is false, and B is false (that is, it takes only one to make their disjunction true).
We present this behavior with truth tables, as follows:
true && true |
true |
true && false |
false |
false && true |
false |
false && false |
false |
true || true |
true |
true || false |
true |
false || true |
true |
false || false |
false |
!true |
false |
!false |
true |
We could also have represented those tables in 2-dimensions, and will do so in lab.
| Equality Operators | ||
|---|---|---|
| Mathematical Notation | C# Notation | Example |
| = | == |
3 == 4 → false |
| ≠ | != |
3!=4 → true |
We test numerical value for equality, as well as string, char and bool!
Console.WriteLine(3 == 4);
Console.WriteLine(myStringVar == "Train");
Console.WriteLine(myCharVar == 'b');We can also test if a value is greater than another, using the following relational operators.
| Relational Operators | ||
|---|---|---|
| Mathematical Notation | C# Notation | Example |
| > | > |
3 > 4 → false |
| < | < |
3 < 4 → true |
| ≥ | >= |
3 >= 4 → false |
| ≤ | <= |
3 <= 4 → true |
We can also compare char, but the order is a bit complex (you can find it, for instance, at https://stackoverflow.com/a/14967721/).
The precedence, that we will study in lab, is as follows:
! (* / %) (+ -) (< > <= >=) (== !=) && ||
Console.WriteLine("Enter your age");
int age = int.Parse(Console.ReadLine());
if (age >= 18)
{
Console.WriteLine("You can vote!");
}The idea is that the statement Console.WriteLine("You can vote!"); is exectued only if the condition (age >= 18) evaluates to true. Otherwise, that statement is simply “skipped”.
Please observe the following.
<Condition> is something that evaluates to a bool. For instance, having a number like in if(3) would not compile.if (<condition>).} that terminates the body of the if statement) are executed in any case.With if-else statements, the idea is that the statement block 1 is exectued only if the condition evaluates to true, and that the statement block 2 is exectued only if the condition evaluates to false. Note that since a condition is always either true or false, we know that at least one of the block will be executed, and since a condition cannot be true and false at the same time, at most one block will be executed: hence, exactly one block will be executed.
<statement block> can actually be an if-else statement itself!

bool usCitizen = true;
int age = 19;
if (usCitizen == true)
{
if (age > 18)
{
Console.WriteLine("You can vote!");
}
else
{
Console.WriteLine("You are too young!");
}
}
else
{
Console.WriteLine("Sorry, only citizens can vote");
}Note that
usCitizen == true: simply write usCitizen!if(age > 18 && usCitizen) … else …, but the messages would be less accurate.if (<condition 1>)
{
<statement block> // Executed if condition 1 is true
}
else if (<condition 2>)
{
<statement block> // Executed if condition 1 is false and condition 2 is true
}
...
else if (<condition N>)
{
<statement block> // Executed if all the previous conditions are false and condition N is true
}
else
{
<statement block> // Executed if all the conditions are false
}Note that the conditions could be really different, not even testing the same thing!
We can make an example with really different conditions, not overlaping:
Giving various values to age, charVar and boolFlag, we will see which value would x get in each case.
There is an operator for if else statements for particular cases (assignment, call, increment, decrement, and new object expressions):
condition ? first_expression : second_expression;
We will have a brief look at it if time allows, otherwise you can read about it at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator.