Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -

Expressions and operators

Expand using link to the right to see the full content.

Introduction

Calculating the age of a person would be hard to do without using the - sign. Programming languages provide us with the most used mathematical operations, such as plus and minus using a special reserved keyword to make them easy to use. These are referred to as operators.

Expression is in the simplest form easy to understand. We will start from some simple examples and continue with a bit more complete definition of expression. It is not possible to give a complete definition of expression without knowing what a function/method, operators etc are so we will settle for a limited definition of expression and extend the concept as the course progresses.

Purpose

There would not be much of a program if we didn’t have operators. The purpose of this lecture is to introduce the student to operators. The knowledge gained from this lecture provides a foundation for coming lectures.

Expression are used all over programs. After a while expression become natural and not something one need to think about that much, still this introduction to expression is good for the coming lectures.

Requirements

Goal

The student shall:

• be familiar with the operator concept.
• be able to use operators when writing programs
• be able to use expressions in variable assignment and in other places in a program
• understand the role of the type of an expression

Instructions to the teacher

Common problems

Variables are very important to understand. Without this understanding we have found it hard for the students to:

• to understand functions and their parameter/argument list
• pointers and reference variables

Videos

All videos in this chapter:

• Expressions and operators Videos (eng) [ (swe)]

See below for individual links to the videos.

Operators

Exercises

1. What is the value after the following operation has been evaluated/calculated?
7 + 9

2. What is the value after the following operation has been evaluated/calculated?
7 * 9

3. What is the value after the following operation has been evaluated/calculated?
7 - 9

4. What is the value after the following operation has been evaluated/calculated?
7 / 3

5. What is the value after the following operation has been evaluated/calculated?
8 / 2

6. What is the value after the following operation has been evaluated/calculated?
9 / 2

7. What is the value after the following operation has been evaluated/calculated?
7 % 3

8. What is the value after the following operation has been evaluated/calculated?
8 % 3

9. What is the value after the following operation has been evaluated/calculated?
8 * 3 + 2

10. What is the value after the following operation has been evaluated/calculated?
8 + 3 * 2

11. What is the value after the following operation has been evaluated/calculated?
(8 * 3) + 2

12. What is the value after the following operation has been evaluated/calculated?
8 * (3 + 2)

13. What is the resulting value of the following expression?
12>12

14. What is the resulting value of the following expression?
12 > 90

15. What is the resulting value of the following expression?
12>=12

16. What is the resulting value of the following expression?
12>=90

17. What is the resulting value of the following expression?
12<12

18. What is the resulting value of the following expression?
12<=90

19. What is the resulting value of the following expression?
12>12

20. What is the resulting value of the following expression?
12>90

21. What is the resulting value of the following expression?
12>=12

22. What is the resulting value of the following expression?
12>=90

Solutions

Expand using link to the right to see the full content.

16

63

-2

2

Think of this as splitting 7 oranges between 3 persons and you're only allowed to hand out complete or wholy oranges. Each person would get two oranges.
There will however be one orange left over.
4

4

1

This is the remaining orange from exercise (4).
2

If each of the three persons get 2 oranges, it means six oranges are given away. Following that there are 2 left over.
26

Multiplications are calculated before additions, so first 8*3,
which is 24, and than add 2, which gives us 26
14

Multiplications are calculated before additions, so first 3+2, which is 6, and than add 8, which gives us#
26

The operations inside paranthesises are calculated before other
operations, so first 8*3, whch is 24, and than add 2, which gives us
40

The operations inside paranthesises are calculated before other operations, so first 3+2, which is 5, and than multiply with 8, which gives us 40
false

12 is not bigger than 12, so false.
false

12 is not bigger than 90, so false.
true

12 is not bigger than 12 but it equal to 12, so true.
false

12 is not bigger than 12 or equal to io, so false.
false

12 is not smaller than 12, so false.
true

12 is smaller than 12 (although not equal to) 90, so true.
false

12 is not bigger than 12, so false.
false

12 is not bigger than 90, so false.
true

12 is not bigger than 12 but it is equal to 12, so true.
false

12 is not bigger than 90 so false.

Presentation

Presentation: (pdf)

Videos

Operator (eng) [ (swe)]

Expression

Exercises

1. What is the resulting value of the following expression?
1

2. What is the resulting value of the following expression?
1+2

3. What is the resulting value of the following expression?
1+2+234

Solutions

Expand using link to the right to see the full content.

1.  1

Yeah, it is that simple!
2.  3

Still easy!
3.  237

Harder, but still easy!

Presentation

Presentation: (pdf)

Videos

Expressions (eng) [ (swe)]