F452 - 3.2.4 Facilities of Programming Languages

?

3.2.4 Facilities of Programming Languages

  • Programs use variables that can be given different values.
  • Setting an avtual value to a variable is known as assigning.
  • The assignment operator is usually the equals sign (=).
    • <variable> = <expression>
  • Not to be confused with equality operator which also often uses '=' sign.
    • Hence it is often read as 'becomes' rather than 'equals'.
    • Some languages use a different sign (e.g. ':=') - most don't!
  • You CANNOT swap the left- and the right-hand sides of the assignment operator.
    • Using x = 7, swapping would give 7 = x (7 becomes x? - nonsense!).
    • Left hand side therefore must be a single variable.
    • Right hand side must be an expression that equates to a single value.
  • Arithmetic operations are used to manipulate numeri data and produce a result.
  • Numbers which are manipulated are called operands.
  • Arithmetic operations only tell us how to manipulate the data.
1 of 11

3.2.4 Facilities of Programming Languages (2)

Unary operators only have one operand - operator written before operand.

Binary operators have two operands - operator written between two operands

(http://s1.postimg.org/88nfr86fz/Screen_Shot_2015_04_16_at_16_50_31.png)

2 of 11

3.2.4 Facilities of Programming Languages (3)

Adding, subtracting or multiplying integers will result in an integer.

When dividing however, this is not the case.

Many languages allow for a number of operators to perform divisions on integers...

(http://s30.postimg.org/o7p2pow8x/Screen_Shot_2015_04_16_at_16_58_00.png)

3 of 11

3.2.4 Facilities of Programming Languages (4)

Relation operators compare data.

They produce a TRUE/FALSE result.

Also known as comparison operators.

Most common relational operators are...

(http://s27.postimg.org/j7krqhhz7/Screen_Shot_2015_04_16_at_16_59_43.png)

4 of 11

3.2.4 Facilities of Programming Languages (5)

Binary operators have two operands - operator written between two operands.

Therefore all these operators are binary.

Booolean operators are very straightforward and logical.

The three main operators are AND, OR and NOT.

The AND operation has two operands.

  • Result is TRUE if either of the operands is TRUE.
  • Result is only FALSE if both operands are FALSE.

The NOT operator has only one operand

  • Result is TRUE if operand is false, and vice versa.

Operations can be put together to make complex expressions.

Like BODMAS in maths, expression must be computed using operator precedence.

5 of 11

3.2.4 Facilities of Programming Languages (6)

(http://s12.postimg.org/3v4nkb2z1/Screen_Shot_2015_04_16_at_17_03_13.png)

Again as with maths, parentheses (brackets) can be used to change the order.

  • Items in brackets should be evaluated first.
    • If there's more than one set of brackets, then they are evaluated in turn from the innermost to the outermost.
  • Most programs need to deal with text.
  • Therefore most languages provide operations that manipulate strings.
6 of 11

3.2.4 Facilities of Programming Languages (7)

Concatenating two strings means joining them together to make one string.

Many languages use the operator '+' for this operation.

Where FullName, FirstName and Surname are string variables...

FullName = FirstName + Surname

A better version would concatenate a space between FirstName and Surname

FullName = FirstName + " " + Surname

Most languages have inbuilt functions to extract part of a string.

Often called LEFT, RIGHT and MID.

Which is used depends whereabouts in the string letters need to be extracted from.

7 of 11

3.2.4 Facilities of Programming Languages (8)

(http://s17.postimg.org/t5y3znrof/Screen_Shot_2015_04_16_at_17_20_59.png)

  • Another common requirement is to search a string for another shorter string.
  • This generally uses LOCATE, for example, as LOCATE(<search string>,<main string>).
  • In this example, if the search string is found, it will return an integer.
    • This integer is obviously the position within the main string.
    • However if the string is not found. it typically returns the number 0.
  • Locating strings varies greatly between languages.
  • The principle however remains the same
8 of 11

3.2.4 Facilities of Programming Languages (9)

Determining the length of a string is easy.

Most languages use LENGTH(<string>).

This will return an integer.

  • For example, LENGTH(<robson>) will return 6.

Dealing with character codes ASCII and CHAR is straightfoward.

  • CHAR(<character code>) returns the character that has that code.
  • ASCII(<character>) returns the character code of the given character.

Comparing strings generally uses alphanumeric comparison in most languages.

Codes of characters are compared in turn, starting from the first character.

  • This allows you to determine which string is greater

continued on next card...

9 of 11

3.2.1 Designing Solutions to Problems (9b)

It can cause unexpected results in many programs.

  • As strings are compared with each other.

For example, upper and lower case characters do not have the same code.

  • Therefore the comparison "Kent" = "kent" would result in FALSE.
    • This is because such comparisons are case sensitive

Also where strings contains digits, then "11" will be considered lower than "2".

  • This is because the first character code is lower in "11" (i.e. 1 < 2)

(http://s17.postimg.org/t24r2s34v/Screen_Shot_2015_04_16_at_17_34_33.png)

10 of 11

3.2.4 Facilities of Programming Languages (10)

For this reason, programmers must be aware of how strings are compared.

For example, they must replace accented characters with non-accented equivalents.

  • The name Zoë would have to be replaced with Zoe.
    • Otherwise "Zoë" would come after other words such as "Zoo".

(http://s7.postimg.org/457mj63sb/Screen_Shot_2015_04_16_at_17_34_39.png)

11 of 11

Comments

No comments have yet been made

Similar Computing resources:

See all Computing resources »See all F452 resources »