Practical 2: Structured Programming

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

Boolean Expressions

Like Python, C# has boolean expressions that evaluate to true or false.

bool isTrue = true;
bool isFalse = false;

Relational operators are used to compare values and return a boolean result.

bool isGreaterThan = 5 > 3; // true
bool isLessThan = 5 < 3; // false
bool isEqual = 5 == 3; // false
bool isNotEqual = 5 != 3; // true
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Boolean Expressions - Logical Operators

Logical operators are used to combine boolean expressions:

Operator Description Example
&& Logical AND, true when both are true isTrue && isFalse
|| Logical OR, true when at least one is true isTrue || isFalse
! Logical NOT, true when the expression is false !isTrue
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

If - Else If - Else

The if statement is used to execute a block of code if a condition is true.

if (condition) {
    // code to execute if "condition" is true
}
else if (anotherCondition) {
    // code to execute if "anotherCondition" is true, but "condition" is false
}
else {
    // code to execute if none of the conditions are true
}

Execute from top to bottom, and stops immediately when a condition is true.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Switch statements

Beside the if - else if - else block, we could use a switch for branching. For example:

int myNumber = 3;
switch (myNumber) {
    case 1:
        // Do something when myNumber == 1
        break;
    case 2:
        // Do something when myNumber == 2
        break;
    case 3:
        // Do something when myNumber == 3
        break;
    default:
        // Do something else if myNumber is not 1, 2 or 3
        break;
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - While

The while loop is used to execute a block of code while a condition is true.

bool condition = true;
while (condition) {
    // code to execute while "condition" is true
}

The condition is checked before the block is executed (so if the condition is false initially, the block will not be executed).

When working with a while loop, make sure the condition will eventually become false, or the loop will run forever.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - Do While

The do while loop is used to execute a block of code at least once, and then while a condition is true.

bool condition = true;
do {
    // code to execute while "condition" is true
} while (condition);

The condition is checked after the block is executed (so the block will always be executed at least once).

Again, always ensure the condition will eventually become false.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - For

The for loop is used to execute a block of code a specific number of times.

for (int i = 0; i < 5; i++) {
    // code to execute
}

The for loop has three parts, separated by semicolons:

  1. The initialisation of the loop variable (int i = 0).
  2. The condition to check before executing the block (i < 5).
  3. The increment of the loop variable after the block is executed (i++).

All three parts can be changed to suit specific cases (like i = i + 2 for the increment);

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - For Each

The foreach loop is used to iterate over a collection of items.

int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers) {
    // code to execute for each "number" in "numbers"
}

The foreach loop is used to iterate over a collection of items, and execute a block of code for each item in the collection.

The variable number is assigned to each item in the collection in turn, and cannot be changed inside the loop.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - Break

The break statement is used to exit a loop early.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    // code to execute
}

while (true) {
    if (condition) {
        break;
    }
    // code to execute
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Loops - Continue

The continue statement is used to skip the rest of the current iteration and start the next one.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        // skip the rest of the current iteration
        continue;
    }
    // code to execute
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science