62
“Operators, Expressions, and statements” Using Bloodshed Dev-C++ Heejin Park Hanyang University

“Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

“Operators, Expressions, and statements”Using Bloodshed Dev-C++

Heejin Park

Hanyang University

Page 2: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Introduction

Introducing Loops

Some Additional Operators

Expressions and Statements

Type Conversions and Type casts

Function with Arguments

A Sample Program

Summary

2

Page 3: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Introducing Loops

The shoes1.c Program

3

Page 4: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Introducing Loops

The shoes1.c Program

4

Page 5: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Introducing Loops

The shoes2.c Program

5

Page 6: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Introducing Loops

The shoes2.c Program

6

Page 7: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

Operators

7

Assignment =

Addition +

Subtraction -

Sign - and +

Multiplication *

Division /

Page 8: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

Some Terminology

• Data Objects• a region of data storage that can be used to hold values.

• Lvalues• a name or expression that identifies a particular Data object.

– A=3; // A is the lvalue

• Rvalues• quantities that can be assigned to modifiable Lvalues.

– A=3; // 3 is the R value

• Operands• Operands are what operators operate on.

8

Page 9: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The golf.c Program

9

Page 10: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The golf.c Program

10

Page 11: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

Unary and binary operators

11

Page 12: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The squares.c Program

12

Page 13: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The squares.c Program

13

Page 14: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The wheat.c Program(1/2)

14

Page 15: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The wheat.c Program(2/2)

15

Page 16: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The wheat.c Program

• Exponential Growth

…Run until square value is 64

16

Page 17: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The divide.c Program

17

Page 18: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The divide.c Program

18

Page 19: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

Operator Precedence

• Expression trees showing operators, operands, and order of evaluation.

19

Page 20: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

Operator Precedence

• Operators in Order of Decreasing Precedence

20

Operator Associativity

() Left to right

+ - (unary) Right to left

* / Left to right

+ - (binary) Left to right

= Right to left

Page 21: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The rules.c Program

21

Page 22: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Fundamental Operators

The rules.c Program

22

Page 23: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The sizeof Operator and the size_t Type

• sizeof returns a value of type size_t• Operator returns the size, in bytes, of its operand.

• The operand can be a specific data object or it can be a type.

• Additional Operators

23

Modulus %

Increment and Decrement ++ and --

Page 24: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The sizeof.c Program

24

Page 25: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The sizeof.c Program

25

Page 26: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The sizeof.c Program

26

Page 27: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The sizeof.c Program

27

Page 28: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

Additional Operators

28

Modulus %

Increment and Decrement ++ and --

Page 29: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The min_sec.c Program

29

Page 30: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The min_sec.c Program

30

Page 31: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The add_one.c Program

31

Page 32: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The add_one.c Program

32

Page 33: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

Through the loop once

33

Page 34: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The post_pre.c Program

34

Page 35: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The post_pre.c Program

35

Page 36: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

Prefix and postfix

• Prefix

• Fist, increment a by 1;

• Then, multiply a by 2 and assign to q

• Postfix

• Fist, multiply a 2, assign to q

• then, increment a by 1

36

q = 2*++a;

q = 2*a++;

Page 37: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The bottles.c Program

37

Page 38: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

The bottles.c Program

38

Page 39: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

Quiz

• squares.c program

39

Page 40: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Some Additional Operators

Quiz

• Yet another troublesome case is this:

• Guess the result.

40

Page 41: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Expressions

• An expression consists of a combination of operators and operands.

• Every expression has a value.

41

Page 42: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Statements

• The primary building blocks of a program.

• In C, statements are indicated by a semicolon at the end.

This is a statement

This is just an expression

42

num = 2;

num = 2

Page 43: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

The addemup.c Program

43

Page 44: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

The addemup.c Program

44

Page 45: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Statements

• Structure of a simple while loop

45

Page 46: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Compound Statements (Blocks)

• Two or more statements grouped together by enclosing them in braces.

• Compare the following program fragments.

• <fragment 1> <fragment 2>

46

Page 47: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Compound Statements (Blocks)

• Results <Fragment1> <Fragment2>

47

Page 48: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Expressions and Statements

Statements

• A while loop with a compound statement.

48

Page 49: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Type Conversions and type casts

Automatic type conversions

• When you add values having different data types, • both values are first converted to the same type.

• Type conversions• depend on the specified operator and the type of the operand or operators.

The cast operator

• Explicit type conversions.

49

(type) variable

Page 50: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Type Conversions and type casts

The convert.c Program

50

Page 51: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Type Conversions and type casts

The convert.c Program

51

Page 52: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Type Conversions and type casts

The Cast Operator

• This is the general form of a cast operator:

• Consider the next two code lines, in which mice is an int variable. • The second line contains two casts to type int.

52

Page 53: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Function with Arguments

Using function arguments

• Example program

53

Page 54: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Function with Arguments

Using function arguments

• Example program• pound()function: prints a specified number of pound signs (#).

• also illustrates some points about type conversion.

54

Page 55: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

A Sample Program

A Sample Program

• Example program

55

Page 56: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

A Sample Program

Using function arguments

• Source code (1/3)

56

Page 57: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

A Sample Program

A Sample Program

• Source code (2/3)

57

Page 58: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

A Sample Program

A Sample Program

• Source code (3/3)

58

Page 59: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Summary(1/4)

Operators

• = - * / % ++ -- (type)

• Unary operators• minus sign and sizeof

• Binary operators• addition and the multiplication operators

Expressions

• Combinations of operators and operands

• Rules of operator precedence

59

Page 60: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Summary(2/4)

Statements

• Complete instructions to the computer and are indicated in C by a terminating semicolon.

• Included within a pair of braces constitute a compound statement, or block.

• Declaration statements

• Assignment statements

• Function call statements

• Control statements

– While loop

60

Page 61: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Summary(3/4)

Type Conversions

• The char and short types• promoted to type int whenever they appear in expressions or as function

arguments.

• The float type • promoted to type double when used as a function argument.

• Converted from a larger type to a smaller type• long to short or double to float

• there might be a loss of data.

61

Page 62: “Operators, Expressions, and statements”calab.hanyang.ac.kr/courses/ISD_taesoo/C05... · Statements • Complete instructions to the computer and are indicated in C by a terminating

Summary(4/4)

Define a function

• When you define a function that takes an argument• Declare a variable, or formal argument, in the function definition.

• Then the value passed in a function call

– assigned to this variable, which can now be used in the function.

62