Chapter:Expressions and operators in C

From Juneday education
Jump to: navigation, search


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 character (or symbol, token) to make them easy to use. These are referred to as operators. Some operators have the form of more than one character (e.g. the equality token ==, the increment operator ++ etc).

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 method or 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, expressions become natural and not something one need to think about that much, still this introduction to expressions 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

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

  • understand method calls and their argument lists
  • understand pointers (in languages having those) and reference variables
  • understand the order of evaluation in statements using expressions (such as assignment statements)
  • understand nested method calls (when the argument to a method is the result of calling another method)

Our experience is that it is time well spent to stress that expressions are evaluated before being used. Understanding this makes it easier to understand a simple statement such as assigning a variable the result of a method call. It's a good idea to come back to this and repeat that assignment statements have a left-hand side (with the variable to be assigned) and a right-hand side with an expression for the value the variable will be assigned.

Be aware of the fact that the type of a compound expression (with operators and operands) depends on the types of the operands according to a set of rules. At this stage, it is enough to focus on whole (integer) division (and its remainder companion) when the operands are both integral, and contrast that to real division when (at least one of the) operands are of real types.

It may also be confusing to students what type a literal will have (e.g. that integral literals have type int while floating point literals have type double).

Chapter videos

All videos in this chapter:

  • Expressions and operators Videos (eng) (swe) (The presentation and video are based on Java but will do fine here)

See below for individual links to the videos.

Operators

Description

Operators are constructs found in a programming language. These operators correspond to some kind of functionality, which can be mathematical or of some other kind. One simple example of operators is +, which can be used to add two integers (these integers are then called operands) like this:

int a;
a = 3 + 7;


Some operators require two "operands" (values the operator operates on). Such operators are placed in between the two operands such as + in a + b. Some people and literature calls such operators "binary" "infix" operators (binary referring to the two operands needed, infix referring to the position in the middle). Some operators require only one operand and are called "unary" operators. A simple unary operator is - (minus) which changes the sign of an expression: negativeA = -a;

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.

  1. Answer:
    16
    
  2. Answer:
    63
    
  3. Answer:
    -2
    
  4. 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.
  5. Answer:
    4
    
  6. Answer:
    4
    
  7. Answer:
    1
    
    This is the remaining orange from exercise (4).
  8. 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.
  9. Answer:
    26
    
    Multiplications are calculated before additions, so first 8*3,
    which is 24, and than add 2, which gives us 26
  10. Answer:
    14
    
    Multiplications are calculated before additions, so first 3+2, which is 6, and than add 8, which gives us#
  11. 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
  12. 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
  13. Answer:
    false
    
    12 is not bigger than 12, so false.
  14. Answer:
    false
    
    12 is not bigger than 90, so false.
  15. Answer:
    true
    
    12 is not bigger than 12 but it is equal to 12, so true.
  16. Answer:
    false
    
    12 is not bigger than 90 or equal to it, so false.
  17. Answer:
    false
    
    12 is not smaller than 12, so false.
  18. Answer:
    true
    
    12 is smaller than 12 (although not equal to) 90, so true.
  19. Answer:
    false
    
    12 is not bigger than 12, so false.
  20. Answer:
    false
    
    12 is not bigger than 90, so false.
  21. Answer:
    true
    
    12 is not bigger than 12 but it is equal to 12, so true.
  22. Answer:
    false
    
    12 is not bigger than or equal to 90 so false.

Presentation

Presentation: (pdf) (The presentation is Java based)

Videos

Operator (eng) (swe) (The presentation and video are based on Java but will do fine here)

Links

Expression

Description

In its simplest form an expression is a literal (e g 0). In the code below we assign the variable a the value 0. 0 is a literal but as such also an expression.

int a;
a = 0;

If we combine two literals with an operator:

int a;
a = 0 + 12;

we get a slightly more complex expression. So for now an expression is either a literal or a combination of two literals and and an operator, like this: literal operator literal.

How about 0 + 12 - 7. Is that an expression?

It is, if we want to change literal operator literal to be expression operator expression. This means that an expression can contain other expressions.


Let's analyse 0 + 12 - 7 a bit. We know from before that 0 + 12 is an expression. This means that 0 + 12 - 7 can be written as expression - 7 which we now see itself is an expression. So 0 + 12 - 7 is a valid 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
    
  4. What is the resulting value of the following expression?
     1+2*3
    
  5. What is the resulting value of the following expression?
     (1+2)*3
    


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!
  4. 7
    
    Harder, but still easy! 2*3 is evaluated before the addition.
  5. 9
    
    Harder, but still easy! Parentheses are always evaluated before anything else.

Presentation

Presentation: (pdf) (The presentation is based on Java but is usable for C)

Videos

Expressions (eng) (swe) (The presentation and video are based on Java but will do fine here)

Chapter Links

Our C FAQ

External links

Books this chapter is a part of

Programming with C book

Book TOC | previous chapter | next chapter