Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Instructions. Integer and Float Conversions.

Similar presentations


Presentation on theme: "Arithmetic Instructions. Integer and Float Conversions."— Presentation transcript:

1 Arithmetic Instructions

2

3

4 Integer and Float Conversions

5

6 Type Conversions in Assignments k is an integer variable a is real

7 Operator Precedence

8 Precedence and Associativity When an expression contains two operators of equal priority or precedence the tie between them is settled using the associativity of the operators. Associativity can be of two types— – Left to Right eg. a=3/2*5 – Right to Lefteg. a=b=c

9 A First Book of ANSI C, 4th edition9 Operator Precedence and Associativity Example: 8 + 5 * 7 % 2 * 4 = 8 + 35 % 2 * 4 = 8 + 1 * 4 = 8 + 4 = 12 8 + 5 * 7 % 2 * 4 =

10 Order of Operations

11

12

13

14 Assume x=2.0 and y=6.0. Evaluate the statement float z= x+3*x/(y-4);

15

16 Writing Algebric Expressions in C

17 ( 3 * ( z + y ) )

18 Types of Program Errors

19 Slide 19 Program Errors Syntax errors – Violation of the grammar rules of the language – Discovered by the compiler Error messages may not always show correct location of errors Run-time errors – Error conditions detected by the computer at run-time Logic errors – Errors in the program’s algorithm – Most difficult to diagnose – Computer does not recognize an error

20 Syntax Error Example #include int main() { char ch, ch1, ch2 /* <<< semicolon removed */ scanf("%c %c %c", &ch, &ch1, &ch2); printf("ch = %c\n", ch); printf("ch1 = %c\n", ch1); printf("ch2 = %c\n", ch2); return 0; }

21 Logical Error Example #include int main() { /* We want to multiply two numbers! Find where is the error??? */ int a=10, b=2; int result = a/b; /* Logic error! */ printf("result = %d", result); return 0; }

22 Runtime Error Example #include int main() { int i=10; i = i/0; // Divide by Zero printf("Hello"); return 0; }

23 Practice Questions

24


Download ppt "Arithmetic Instructions. Integer and Float Conversions."

Similar presentations


Ads by Google