w5 - expression (2)

36
Expression (2) Program Design (I) 2021 Fall Fu-Yin Cherng Dept. CSIE, National Chung Cheng University

Upload: others

Post on 04-Dec-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: W5 - Expression (2)

Expression (2)Program Design (I)

2021 Fall Fu-Yin Cherng

Dept. CSIE, National Chung Cheng University

Page 2: W5 - Expression (2)

Outline

● Increment and Decrement Operators● Expression Evaluation

2

Page 6: W5 - Expression (2)

Increment and Decrement Operators

● Two of the most common operations on a variable are “incrementing” (adding 1) and “decrementing” (subtracting 1)

● Incrementing and decrementing can be done using the compound assignment operators

6

i = i + 1;

j = j - 1;

i += 1;

j -= 1;

Page 7: W5 - Expression (2)

Increment and Decrement Operators

7

● C provides special ++ (increment) and -- (decrement) operators.● The ++ operator adds 1 to its operand. The -- operator subtracts 1.

○ can be used as prefix or postfix operators .● side effects: they modify the values of their operands.

○ i + 1; (this expression didn’t change the value of i; since + didn’t change value of the operand)

int i = 1, j = 1;

++i; // prefix; i value is changed to 2

i++; // postfix; i value is 3

--j; // prefix; i value is 0

j--; // postfix; i value is -1

Page 8: W5 - Expression (2)

Increment and Decrement Operators

8

● ++i: pre-increment (prefix increment)● i++: post-increment (postfix increment)

○ produce the result of i, but increment i by 1 afterwards

i = 1;

printf("i is %d\n", ++i); /* prints "i is 2" */

printf("i is %d\n", i); /* prints "i is 2" */

i = 1;

printf("i is %d\n", i++); /* prints "i is 1" add i by 1 afterward */

printf("i is %d\n", i); /* prints "i is 2" */

Page 9: W5 - Expression (2)

Increment and Decrement Operators

● ++i means “increment i immediately” ● i++ means “use the old value of i for now, but increment i later.”

○ How much later? ○ The C standard doesn’t specify a precise time, ○ but it’s safe to assume that i will be incremented before the next statement is executed.

9

Page 10: W5 - Expression (2)

Increment and Decrement Operators

10

● postfix -- and ++ ○ have higher precedence than unary plus and minus○ left associative

● prefix -- and ++○ have the same precedence as unary plus and minus○ right associative

int i = 1;

+i--++; // +((i--)++)); i is 1

i = 1;

----+i; // (--(--(+i))); i is -1

However, don’t recommend write program like this way! Hard to understand.

Page 11: W5 - Expression (2)

Increment and Decrement Operators

11

● Does ++ and -- work with float variable?○ Yes, the increment and decrement operations can be applied to

floating-point numbers. ○ However, it’s rare to increment or decrement a float variabel

Page 12: W5 - Expression (2)

Expression Evaluation

12

Precedence Name Symbol Associativity

1 (high) increment (postfix) variable++ left

decrement (postfix) variable--

2 increment (prefix) ++variable right

decrement (prefix) --variable

unary plus +variable

unary minus -variable

3 multiplicative * / % left

4 additive + - left

5 (low) (compound) assignment = *= /= %= += -= right

Page 13: W5 - Expression (2)

Expression Evaluation

● The table can be used to add parentheses to an expression that lacks them.○ just look up the table when you need it

● However, certainly don’t recommend write this kind of statement. ● Just help you to interpretate correctly if you encounter in the future

13

a = b += c++ - d + --e / -f;

(a = (b += (((c++) - d) + ((--e) / (-f)))));

Page 14: W5 - Expression (2)

Order of Subexpression Evaluation

● The value of an expression may depend on the order in which its subexpressions are evaluated/executed.

● C doesn’t define the order in which subexpressions are evaluated○ with the exception of subexpressions involving the logical and, logical or, conditional, and

comma operators (introduce in later chapters)● For example,

14

(a + b) * (c - d)

//don’t know whether (a + b) will be evaluated before (c - d).

Page 15: W5 - Expression (2)

Order of Subexpression Evaluation

● Most expressions have the same value regardless of the order in which their subexpressions are evaluated.

15

int i = 0, j = 1;

i = (1+j) * (2+j); //(a + b) * (c - d)

// the value of i will be 2*3

// no matter (a+b) or (c-d) is evaluated first

Page 16: W5 - Expression (2)

Order of Subexpression Evaluation

● However, this may not be true when a subexpression modifies one of its operands

● For example, ○ if (b = a + 2) is evaluated first, c will be 6○ if (a = 1) is evaluated first, c will be 2

● Avoid writing this kind of expression!!○ access the value of a variable and also modify the variable in the same expression!

16

a = 5;

c = (b = a + 2) - (a = 1);

// warning message “operation on ‘a’ may be undefined”

a = 5;

b = a + 2;

a = 1;

c = b - a;

// c wil always be 6

only use one assignment operator in one statment

Page 17: W5 - Expression (2)

Order of Subexpression Evaluation

● Besides the assignment operators (=), the only operators that modify their operands are increment (++) and decrement (--).

● When using these operators, be careful that an expression doesn’t depend on a particular order of evaluation.

17

i = 1; // = assign 1 to i, so change value of i

printf("%d\n", ++i); //output: 2

// ++i add 1 to i and update i, so change value of i to 2

Page 18: W5 - Expression (2)

Undefined Behavior

● Statements such as c = (b = a + 2) - (a = 1) cause undefined behavior.

● Possible effects of undefined behavior○ The program may behave differently when compiled with different compilers.○ The program may not compile in the first place.○ If it compiles it may not run.○ If it does run, the program may crash, behave erratically, or produce meaningless results.

● Undefined behavior should be avoided.

18

Page 19: W5 - Expression (2)

Expression Statements

● C has the unusual rule that any expression can be used as a statement.● For example,

○ getting the value stored in i○ incremented the value○ but then discarded○ i’s value is still 0

19

int i = 0;

i + 1;

// value of i is 0

Page 20: W5 - Expression (2)

Expression Statements

● Since its value is discarded, there’s little point in using an expression as a statement

○ you need a place (e.g., variable) to store the value● unless the expression has a side effect

○ change the value of operand (assignment, increment, and decrement operators)

20

i = 1; /* useful; assign 1 to i */

i--; /* useful; add i by 1 and update to i*/

i * j - 1; /* not useful; no variable store result of expression*/

Page 21: W5 - Expression (2)

Expression Statements

● A slip of the finger can easily create a “do-nothing” expression statement.● Some compilers can detect meaningless expression statements; you’ll get a

warning such as “statement with no effect.”● This kind of errors is more common than you might expect

21

// goal: i = j;

i + j; //accidentally type

Page 25: W5 - Expression (2)

Summary

● Increment and Decrement Operators○ ++ and --○ prefix and postfix

● Expression Evaluation○ Precedence○ Undefined Behavior

25

Page 26: W5 - Expression (2)

The Programming ProcessProgram Design (I)

2021 Fall Fu-Yin Cherng

Dept. CSIE, National Chung Cheng University

Page 27: W5 - Expression (2)

The Programming Process

● All programming involves creating something that solves a problem.● rough guide to the things you should do when entering the land of

programming.

● Identify the Problem● Design a Solution● Write the Program● Check the Solution

27https://www.cs.bham.ac.uk/~rxb/java/intro/2programming.html

actually programming, but probably the least important stage

Page 28: W5 - Expression (2)

Identify the Problem

● In the course, this stage is actually identifying the solution○ since we describe problem clearly in homework and program exercise for you○ however, in real situations, you often need to identify the probelm which can be solve by

programming■ For example, how to help people register vaccination?

○ involving computation thinking, design thinking, software desing… will not be covered in this course

28https://www.cs.bham.ac.uk/~rxb/java/intro/2programming.html

Page 29: W5 - Expression (2)

Identify the Problem

● Two stages to identifying a solution○ Requirements

■ for example, if you were asked to write a calculator program, what are the requirments?■ entering equations, pressing buttons, printing results on the screen

○ Specification■ decide exactly what your solution should do to fulfil requirements

● usually there are different solutions to a single problem● decide on which of those solutions you want

29https://www.cs.bham.ac.uk/~rxb/java/intro/2programming.html

Page 30: W5 - Expression (2)

Design a Solution

● How you're going to turn that specification into a working program○ a program is simply a list of steps describing to the computer what it should do○ design is simply a higher-level description of those steps

● Pick up you pen and paper, draw the blueprints of your program○ for example, you can write pseudocode or flow charts

● When your design is completed you should have a very clear idea of how the computer is going to fulfil your specification

30https://www.cs.bham.ac.uk/~rxb/java/intro/2programming.html

Page 31: W5 - Expression (2)

Design a Solution

31

Page 32: W5 - Expression (2)

Write the Program

● Programming is then the task of describing your design to the computer○ tell the computer your way of solving the problem

● three stages to writing a program○ Coding

■ translating the design into an actual program■ now, you sit down at the computer and type

○ Compiling○ Debugging (essential technique you need to learn as a programmer)

■ your program it is likely to come back to you with a long list of mistakes■ perfectly normal, even for experienced programmers■ So, don’t worry when you see any error or warning messages■ What you need to do is debugging by yourself first

32https://www.cs.bham.ac.uk/~rxb/java/intro/2programming.html

Page 33: W5 - Expression (2)

Write the Program - Degugging

● Debugging is simply the task of reading error/warning messages, looking at the original program, identifying the mistakes, correcting the code and recompiling it.

○ This cycle of code -> compile -> debug will often be repeated many many times● it’s not necessary to write the entire program before you start to compile and

debug it!○ In most cases it is better to write a small section of the code first, get that to work, and then

move on to the next stage. ○ This reduces the amount of code that needs to be debugged each time and generally creates

a good feeling of "getting there" as each section is completed.

33

Page 34: W5 - Expression (2)

Write the Program - Degugging

34

compile and debug; ensure the programcan read X and Y successfully.

compile and debug; ensure the programcan add X and Y successfully.

compile and debug; ensure the programcan disply SUM.

Page 35: W5 - Expression (2)

Check the Solution

● check whether what you've written actually solves your original problem.○ running it and playing with it for a bit to see if it seems to be working correctly○ example input and output

● check that it meets the requirements and specification● trying to figure out where in the code the mistake is.

○ Once identified, the problem should be fixed by changing the code and recompiling.

35

Page 36: W5 - Expression (2)

36

"Another effective debugging technique is to explain your code to someone else. This will often cause you to explain the bug to yourself.

Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind, I see what's wrong. Sorry to bother you." This works remarkably well; you can even use non-programmers as listeners.

One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor."

Brian Kernighan

That’s why we have teams and ask you to describe your problem clearly when writing a email.