PCP − Lecture 09

Fall 2020

Clément Aubert

October 3, 2020

Last Time - Wrapping up classes

Please, refer to the second milestone for a brief recap of what we’ve done recently.

Decision (or Control) Structures

Boolean and Conditions

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:

bool flag = true;
Console.WriteLine(true);

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 and Relational Operators

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:

! (* / %) (+ -) (< > <= >=) (== !=) && ||

if Statement

First Example

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”.

Syntax

if (<condition>)
{
    <statement block>
}

Please observe the following.

if-else Statements

Syntax

if (<condition>)
{
    <statement block 1>
}
else
{
    <statement block 2>
}

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.

Nested if-else Statements

<statement block> can actually be an if-else statement itself!

A flowchart representation of the nested if-else statement
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

if-else-if Statements

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!

Example

We can make an example with really different conditions, not overlaping:

if (age > 12)
    x = 0;
else if (charVar == 'c')
    x = 1;
else if (boolFlag)
    x = 2;
else 
    x = 3;

Giving various values to age, charVar and boolFlag, we will see which value would x get in each case.

?: Operator

There is an operator for if else statements for particular cases (assignment, call, increment, decrement, and new object expressions):

condition ? first_expression : second_expression;

int price = adult ? 5 : 3;

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.