Expressions and operators
Meta information about this chapter
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
Concepts
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
Description
Exercises
 What is the value after the following operation has been evaluated/calculated?

7 + 9

 What is the value after the following operation has been evaluated/calculated?

7 * 9

 What is the value after the following operation has been evaluated/calculated?

7  9

 What is the value after the following operation has been evaluated/calculated?

7 / 3

 What is the value after the following operation has been evaluated/calculated?

8 / 2

 What is the value after the following operation has been evaluated/calculated?

9 / 2

 What is the value after the following operation has been evaluated/calculated?

7 % 3

 What is the value after the following operation has been evaluated/calculated?

8 % 3

 What is the value after the following operation has been evaluated/calculated?

8 * 3 + 2

 What is the value after the following operation has been evaluated/calculated?

8 + 3 * 2

 What is the value after the following operation has been evaluated/calculated?

(8 * 3) + 2

 What is the value after the following operation has been evaluated/calculated?

8 * (3 + 2)

 What is the resulting value of the following expression?

12>12

 What is the resulting value of the following expression?

12 > 90

 What is the resulting value of the following expression?

12>=12

 What is the resulting value of the following expression?

12>=90

 What is the resulting value of the following expression?

12<12

 What is the resulting value of the following expression?

12<=90

 What is the resulting value of the following expression?

12>12

 What is the resulting value of the following expression?

12>90

 What is the resulting value of the following expression?

12>=12

 What is the resulting value of the following expression?

12>=90

Solutions
Expand using link to the right to see the full content.
 Answer:
16
 Answer:
63
 Answer:
2
 Answer:
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.
 Answer:
4
 Answer:
4
 Answer:
1
 This is the remaining orange from exercise (4).
 Answer:
2
 If each of the three persons get 2 oranges, it means six oranges are given away. Following that there are 2 left over.
 Answer:
26
 Multiplications are calculated before additions, so first 8*3,
 which is 24, and than add 2, which gives us 26
 Answer:
14
 Multiplications are calculated before additions, so first 3+2, which is 6, and than add 8, which gives us#
 Answer:
26
 The operations inside paranthesises are calculated before other
 operations, so first 8*3, whch is 24, and than add 2, which gives us
 Answer:
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
 Answer:
false
 12 is not bigger than 12, so false.
 Answer:
false
 12 is not bigger than 90, so false.
 Answer:
true
 12 is not bigger than 12 but it equal to 12, so true.
 Answer:
false
 12 is not bigger than 12 or equal to io, so false.
 Answer:
false
 12 is not smaller than 12, so false.
 Answer:
true
 12 is smaller than 12 (although not equal to) 90, so true.
 Answer:
false
 12 is not bigger than 12, so false.
 Answer:
false
 12 is not bigger than 90, so false.
 Answer:
true
 12 is not bigger than 12 but it is equal to 12, so true.
 Answer:
false
 12 is not bigger than 90 so false.
Presentation
Presentation: (pdf)
Videos
Operator (eng) [ (swe)]
Links
Expression
Description
Exercises
 What is the resulting value of the following expression?
1
 What is the resulting value of the following expression?
1+2
 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
 Yeah, it is that simple!

3
 Still easy!

237
 Harder, but still easy!
Presentation
Presentation: (pdf)
Videos
Expressions (eng) [ (swe)]