chapter 3 structure of a c program - smp.bsk.im

91
Computer Science: A Structured Programming Approach Using C 1 Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand the result of side effects in expression evaluation To be able to predict the results when an expression is evaluated To understand implicit and explicit type conversion To understand and use the first four statement types: null, expression, return, and compound Chapter 3 Structure of a C Program

Upload: others

Post on 01-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 1

Objectives ❏ To be able to list and describe the six expression categories❏ To understand the rules of precedence and associativity in

evaluating expressions❏ To understand the result of side effects in expression evaluation❏ To be able to predict the results when an expression is evaluated❏ To understand implicit and explicit type conversion❏ To understand and use the first four statement types: null,

expression, return, and compound

Chapter 3Structure of a C Program

Page 2: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 2

3-1 ExpressionsAn expression is a sequence of operands and operators that reduces to a single value. Expressions can be simple or complex. An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed; it receives an operator’s action.

Primary ExpressionsPostfix ExpressionsPrefix ExpressionsUnary ExpressionsBinary Expressions

Topics discussed in this section:

Page 3: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 3

An expression always reduces to a single value.

Note

Page 4: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 4

FIGURE 3-1 Expression Categories

Page 5: Chapter 3 Structure of a C Program - smp.bsk.im

Primary Expressions

Names a, b12, price, INT_MAX, SIZE

Literal constants 5, 123.98, ‘A’, “Welcome”

Parenthetical expressions (2 * 3 + 4), (a = 23 + b * 6)

Computer Science: A Structured Programming Approach Using C 5

Page 6: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 6

FIGURE 3-2 Postfix Expressions

Page 7: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 7

(a++) has the same effect as (a = a + 1)

Note

Page 8: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 8

FIGURE 3-3 Result of Postfix a++

Page 9: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 9

The operand in a postfix expression must be a variable.

Note

Page 10: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 3-1 Demonstrate Postfix Increment

Page 11: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 3-1 Demonstrate Postfix Increment (continued)

Page 12: Chapter 3 Structure of a C Program - smp.bsk.im

Function Call

A postfix expression Function call operator () printf(“hello world\ n”)

Computer Science: A Structured Programming Approach Using C 12

Page 13: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 13

FIGURE 3-4 Prefix Expression

Page 14: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 14

(++a) has the same effect as (a = a + 1)

Note

Page 15: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 15

FIGURE 3-5 Result of Prefix ++a

Page 16: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 16

The operand of a prefix expression must be a variable.

Note

Page 17: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 3-2 Demonstrate Prefix Increment

Page 18: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 3-2 Demonstrate Prefix Increment (continued)

Page 19: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 19

If ++ is after the operand, as in a++, the increment takes place after the expression is evaluated.

If ++ is before the operand, as in ++a, the incrementtakes place before the expression is evaluated.

Note

Page 20: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 20

FIGURE 3-6 Unary Expressions

Page 21: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 21

Table 3-1 Examples of Unary Plus And Minus Expressions

Page 22: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 22

FIGURE 3-7 Binary Expressions

Page 23: Chapter 3 Structure of a C Program - smp.bsk.im

Binary Expressions

10 * 3, true * 4, ‘A’ * 2, 22.3 * 2 10 / 3, true / 4, ‘A’ / 2, 22.3 / 2 10 % 3, true % 4, ‘A’ % 2, 22.3 % 2 3 / 5, 3 % 5 3 + 7, 3 – 7 a = 5, b = x + 1, i = i + 1

Computer Science: A Structured Programming Approach Using C 23

Page 24: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 24

Both operands of the modulo operator (%) must be integral types.

Note

Page 25: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 3-3 Binary Expressions

Page 26: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 3-3 Binary Expressions (continued)

Page 27: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 3-3 Binary Expressions (continued)

Page 28: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 28

The left operand in an assignment expression must be a single variable.

Note

Page 29: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 29

Table 3-2 Expansion of Compound Expressions

Page 30: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 3-4 Demonstration of Compound Assignments

Page 31: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 3-4 Demonstration of Compound Assignments

Page 32: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 3-4 Demonstration of Compound Assignments

Page 33: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 33

3-2 Precedence and Associativity

Precedence is used to determine the order in which different operators in a complex expression are evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression.

PrecedenceAssociativity

Topics discussed in this section:

Page 34: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 3-5 Precedence

Page 35: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 3-5 Precedence

Page 36: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 36

Page 37: Chapter 3 Structure of a C Program - smp.bsk.im

a + b + c + d a * b * c * d

Computer Science: A Structured Programming Approach Using C 37

FIGURE 3-8 Left-to-Right Associativity

Page 38: Chapter 3 Structure of a C Program - smp.bsk.im

a = b = c = d = 10

Computer Science: A Structured Programming Approach Using C 38

FIGURE 3-9 Right-to-Left Associativity

Page 39: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 39

3-3 Side EffectsA side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression on the right of the assignment operator and then places the value in the left variable. Changing the value of the left variable is a side effect.

Page 40: Chapter 3 Structure of a C Program - smp.bsk.im

Side Effects

x = 4; x = x + 4; a++ printf(“x = %d\ n”, x); printf(“x = %d\ n”, x = x + 4); scanf(“%d”, &x);

Computer Science: A Structured Programming Approach Using C 40

Page 41: Chapter 3 Structure of a C Program - smp.bsk.im

Side Effects

A side effect also means an unexpected change due to the use of a global variable

You are advised not use a global variable! A clear example of this kind of side effect

could be made when we have multiple functions

Computer Science: A Structured Programming Approach Using C 41

Page 42: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 42

3-4 Evaluating Expressions

Now that we have introduced the concepts of precedence, associativity, and side effects, let’s work through some examples.

Expressions without Side EffectsExpressions with Side Effects

Topics discussed in this section:

Page 43: Chapter 3 Structure of a C Program - smp.bsk.im

Expressions and Side Effects

a * 4 + b / 2 – c * b d = a * 4 + b / 2 – c * b f = a * 4 + b++ / 2 – c * --b

Computer Science: A Structured Programming Approach Using C 43

Page 44: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 44

PROGRAM 3-6 Evaluating Expressions

Page 45: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 45

PROGRAM 3-6 Evaluating Expressions

Page 46: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 46

PROGRAM 3-6 Evaluating Expressions

Page 47: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 47

Warning

Page 48: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 48

3-5 Type Conversion

Up to this point, we have assumed that all of our expressions involved data of the same type. But, what happens when we write an expression that involves two different data types, such as multiplying an integer and a floating-point number? To perform these evaluations, one of the types must be converted.

Implicit Type ConversionExplicit Type Conversion (Cast)

Topics discussed in this section:

Page 49: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 49

FIGURE 3-10 Conversion Rank

Promotion

Demotion

Page 50: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 50

PROGRAM 3-7 Implicit Type Conversion

Page 51: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 51

PROGRAM 3-7 Implicit Type Conversion

Page 52: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 52

PROGRAM 3-7 Implicit Type Conversion

Page 53: Chapter 3 Structure of a C Program - smp.bsk.im

Explicit Type Conversion: Cast

Cast Operator: (type-to-cast) operand

Exampleint a=3; float b=4.0;

b = (float)a;

Computer Science: A Structured Programming Approach Using C 53

Page 54: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 54

PROGRAM 3-8 Explicit Casts

Page 55: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 55

PROGRAM 3-8 Explicit Casts

Page 56: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 56

PROGRAM 3-8 Explicit Casts

Page 57: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 57

3-6 Statements

A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.

You may have noticed that we have used a semicolon at the end of the statements in our programs. Most statements need a semicolon at the end; some do not.

Statement TypeThe Role of the SemicolonStatements and Defined Constants

Topics discussed in this section:

Page 58: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 58

FIGURE 3-11 Types of Statements

Page 59: Chapter 3 Structure of a C Program - smp.bsk.im

Null Statement

Just a semicolon Example

; // null statement

They do nothing, but are still valid syntactical objects.

Computer Science: A Structured Programming Approach Using C 59

Page 60: Chapter 3 Structure of a C Program - smp.bsk.im

Expression Statement

A statement consisting of an expression Example

expression;

a = 2;

b = c = 3;

Computer Science: A Structured Programming Approach Using C 60

Page 61: Chapter 3 Structure of a C Program - smp.bsk.im

Return Statement

A return statement terminates a function.

Examplereturn;return expression;

Computer Science: A Structured Programming Approach Using C 61

Page 62: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 62

FIGURE 3-12 Compound Statement

Page 63: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 63

The compound statement does not need a semicolon.

Note

Page 64: Chapter 3 Structure of a C Program - smp.bsk.im

The Role of the Semicolon

Every declaration in C is terminated by a semicolon

Most statements in C are terminated by a semicolon

Computer Science: A Structured Programming Approach Using C 64

Page 65: Chapter 3 Structure of a C Program - smp.bsk.im

Statements and Defined Constants

#define TAX_RATE 0.825;

tax = TAX_RATE * amount;

Computer Science: A Structured Programming Approach Using C 65

Page 66: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 66

3-7 Sample Programs

This section contains several programs that you should study for programming technique and style.

Page 67: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 67

PROGRAM 3-9 Calculate Quotient and Remainder

Page 68: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 68

PROGRAM 3-9 Calculate Quotient and Remainder

Page 69: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 69

PROGRAM 3-10 Print Right Digit of Integer

Page 70: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 70

PROGRAM 3-10 Print Right Digit of Integer

Page 71: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 71

PROGRAM 3-11 Calculate Average of Four Numbers

Page 72: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 72

PROGRAM 3-11 Calculate Average of Four Numbers

Page 73: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 73

PROGRAM 3-11 Calculate Average of Four Numbers

Page 74: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 74

PROGRAM 3-11 Calculate Average of Four Numbers

Page 75: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 75

PROGRAM 3-12 Convert Radians to Degrees

Page 76: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 76

PROGRAM 3-12 Convert Radians to Degrees

Page 77: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 77

PROGRAM 3-13 Calculate Sales Total

Page 78: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 78

PROGRAM 3-13 Calculate Sales Total

Page 79: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 79

PROGRAM 3-13 Calculate Sales Total

Page 80: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 80

PROGRAM 3-13 Calculate Sales Total

Page 81: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 81

PROGRAM 3-14 Calculate Student Score

Page 82: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 82

PROGRAM 3-14 Calculate Student Score

Page 83: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 83

PROGRAM 3-14 Calculate Student Score

Page 84: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 84

PROGRAM 3-14 Calculate Student Score

Page 85: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 85

PROGRAM 3-14 Calculate Student Score

Page 86: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 86

PROGRAM 3-14 Calculate Student Score

Page 87: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 87

PROGRAM 3-14 Calculate Student Score

Page 88: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 88

3-8 Software Engineering

In this section we discuss three concepts that, although technically not engineering principles, are important to writing clear and understandable programs.

KISSParenthesesUser Communication

Topics discussed in this section:

Page 89: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 89

Blocks of code should be no longer than one screen.

Note

KISS (Keep It Simple and Short)

Page 90: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 90

Computers do what you tell them to do, not what you intended to tell them to do.

Make sure your code is as clear and simple as possible.

Note

Use parentheses whenever you are not sure of precedence.

Page 91: Chapter 3 Structure of a C Program - smp.bsk.im

Computer Science: A Structured Programming Approach Using C 91

PROGRAM 3-15 Program That Will Confuse the User

Always provide the user with clear information

printf(“Enter two integers and key <return>\n”);