Fundamentals of Software Development

General Information

  • Presenter: Brahim El Boudani

  • Institution: South Bank University, London

  • Course Year: EST 1892

Lecture Series Overview

  • Course Title: Fundamentals of Software Development

  • Lecture Numbers: 3 and 4 Recap

    • Lecture 3: Constants and Literals

    • Key Topics:

      • Constant keywords

      • Constants types

    • Lecture 4: C Basic Syntax

    • Key Topics:

      • Operators and operator precedence

Today's Lecture Content Details

Operators

  • Definition: An operator is a symbol that instructs the compiler about which logical or mathematical function to use.

  • The C language provides several types of operators:

    • Arithmetic Operators

    • Relational Operators

    • Logical Operators

    • Bitwise Operators

    • Assignment Operators

    • Miscellaneous Operators

Assignment Operator

  • Definition: An assignment operator allows the evaluation of an expression resulting in a value to be assigned to a variable.

  • Important Notes:

    • All expressions must evaluate to a value, including those with the assignment operator.

    • The outcome typically reflects the value on the right-hand side.

    • Type conversion may affect the assigned value (e.g., float vs int).

  • Example Usage:

    • Expression:
      y = x = 5

    • Outcome: y gets the value 5 because x is assigned 5 first.

  • Sample Code:
    ```c
    #include
    int main() {
    int m, n, z; // Variable Declaration
    m = n = z = 1;
    printf(" The value of m is: %d\n The value of n is: %d\n The value of z is: %d\n", m, n, z);
    return 0;
    }

## Arithmetic Operators  
- Definition: Arithmetic operators conduct calculations on one or more operands.  
- Important Note: Integer division truncates results (e.g., 9/10 = 0, 17/5 = 3).  
- Operator Summary:  
  | Operator | Description                               | Example          |  
  |----------|-------------------------------------------|-------------------|  
  | +        | Adds two operands.                       | A + B = 30        |  
  | -        | Subtracts second operand from the first. | A - B = -10       |  
  | *        | Multiplies both operands.                | A * B = 200       |  
  | /        | Divides numerator by denominator.        | B / A = 2         |  
  | %        | Modulus operator (remainder).            | B % A = 0         |  
  | ++       | Increments the operand by one.           | ++A = 11          |  
  | --       | Decrements the operand by one.           | --A = 9           |  

## Arithmetic Operators Example  
- Sample Code:  

c
#include
int main() {
int x = 17, y = 5, result;
// Addition
result = x + y;
printf("The results of x + y is: %d\n", result);
// Subtraction
result = x - y;
printf("The results of x - y is: %d\n", result);
// Division
result = x / y;
printf("The results of x / y is: %d\n", result);
// Multiplication
result = x * y;
printf("The results of x * y is: %d\n", result);
// Modulus
result = x % y;
printf("The results of x %% y is: %d\n", result);
// Increment
result = ++x;
printf("The results of ++x is: %d\n", result);
// Decrement
result = --y;
printf("The results of --y is: %d\n", result);
return 0;
}

## Relational Operators  
- Definition: Relational operators compare the first operand against the second to test their relationship.  
- Result Type: Integer (int), where 0 indicates false and 1 indicates true.  
- Example Variables: Assume A = 10, B = 20.  

## Logical Operators  
- Definition: Logical operators do not perform any arithmetic operation but determine the truth value of expressions based on zero equivalence.  
- Example Variables: Assume A = 1, B = 0.  

## Logical Operators Example  
- Sample Code:  

c
#include
int main() {
int x = 5, y = 15, z = 20;
printf(" The logical output of the evaluated expression is: %d", (x < y && y < z));
return 0;
}

## Bitwise Operators  
- Definition: Bitwise operators operate on bits and perform bit-by-bit operations.  
- Truth Tables for Operators:  
    - **AND (&)**  
    - **OR (|)**  
    - **XOR (^)**  
- Important Note: Do not confuse bitwise operators with logical operators.  

## Bitwise Operators Example  
- Sample Code:  

c
#include
int main() {
int a = 60;
int b = 13;
printf("the a & b bitwise operation is equal: %d\n", a & b);
printf("the a | b bitwise operation is equal: %d\n", a | b);
printf("the a ^ b bitwise operation is equal: %d\n", a ^ b);
return 0;
}
```

Special & Miscellaneous Operators

  • Description of Operators:

    • Shorthand: +=, -=, *=, etc.

    • Simple assignment: Assigns values from right to left.

    • Examples:

    • C = A + B results in C being assigned the value of A + B.

    • C += A equates to C = C + A.

  • Miscellaneous Operators Description:

    • sizeof(): Returns size of variable, e.g., sizeof(x) where x is an integer returns 4.

    • &: Returns the address of a variable, e.g., &a returns the address of a variable.

    • *: Pointer to a variable.

Operators Precedence & Associativity

  • Operator Precedence: Determines grouping in expressions and the order of evaluation. Higher precedence operators execute first. For instance:

    • Expression example: 1 + 2 * 3 is treated as 1 + (2 * 3) due to multiplication's higher precedence.

  • Operator Associativity: Evaluates expression based on associativity criteria when operators share the same precedence level.

  • Example: 72 / 2 / 3 is treated as (72 / 2) / 3 since division has left-to-right associativity.

Summary of Today's Lecture

  • C offers various sets of operators: arithmetic, logic, relationships, and bits.

  • Key Operators:

    • Arithmetic: *, /, %, - , +, ++, --

    • Relational: ==, !=, >=, <=

    • Logical: &&, ||, !

    • Bitwise: &, |, ^, ~

    • Assignment: =, *=, +=, <<=, etc.

    • Miscellaneous: sizeof(), ? , & , *

Next Lecture Preview

  • Topic: Decision making and loops.

Inspirational Note

  • Quote: "The most damaging phrase in the language is: 'It's always been done that way.'"

    • Author: Grace Hopper