1 cse1301 computer programming lecture 5 c primitives 2
Post on 19-Dec-2015
220 views
TRANSCRIPT
![Page 1: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/1.jpg)
1
CSE1301Computer Programming
Lecture 5C Primitives 2
![Page 2: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/2.jpg)
2
Topics
• Expressions
• Precedence
• Function calls
• Commentsprintf(“Hello World”);
![Page 3: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/3.jpg)
3
Expressions
• Combine values using operators and function calls
• Return a value of a known type
![Page 4: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/4.jpg)
4
Arithmetic Expressions
• take arithmetic (numerical) values and return an arithmetic (numerical) value
• Are composed using the following operators:+ (unary plus)
- (unary minus)
+ (addition)
- (subtraction)
* (multiplication)
/ (division or quotient)
% (modulus or remainder)
![Page 5: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/5.jpg)
5
Precedence in Expressions
• Defines the order in which an expression is evaluated
![Page 6: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/6.jpg)
6
Precedence in Expressions – Example
1 + 2 * 3 - 4 / 5 =
B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction.
B.O.D.M.A.S.1 + (2 * 3) - (4 / 5)
![Page 7: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/7.jpg)
7
More on precedence
• *, /, % are at the same level of precedence • +, - are at the same level of precedence • For operators at the same “level”, left-to-right
ordering is applied2 + 3 – 1 = (2 + 3) – 1 = 42 – 3 + 1 = (2 – 3) + 1 = 0
2 * 3 / 4 = (2 * 3) / 4 = 6 / 42 / 3 * 4 = (2 / 3) * 4 = 0 * 4
![Page 8: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/8.jpg)
8
Precedence in Expressions – Example (cont)
6.2
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 9: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/9.jpg)
9
Precedence in Expressions –Example (cont)
6.2
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 10: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/10.jpg)
10
Precedence in Expressions – Example (cont)
Integer division results in integer quotient
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 11: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/11.jpg)
11
Precedence in Expressions – Example (cont)
= 0
D’oh
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 12: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/12.jpg)
12
Precedence in Expressions – Example (cont)
7
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
![Page 13: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/13.jpg)
13
int-s and float-s
• float is a “communicable” type
• Example:
1 + 2 * 3 - 4.0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2
![Page 14: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/14.jpg)
14
int-s and float-s – Example 2
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3 / 5
= 0
![Page 15: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/15.jpg)
15
int-s and float-s – Example 2 (cont)
(1 + 2.0) * (3 - 4) / 5
= ((1 + 2.0) * (3 - 4)) / 5
= (3.0 * -1) / 5
= -3.0 / 5
= -0.6
![Page 16: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/16.jpg)
16
int-s and float-s – Example 3
(1 + 2.0) * ((3 - 4) / 5)
= (1 + 2.0) * (-1 / 5)
= 3.0 * 0
= 0.0
![Page 17: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/17.jpg)
17
Unary operators
• Called unary because they require one operand• Example
i = +1; /* + used as a unary operator */
j = -i; /* - used as a unary operator */
• The unary + operator does nothing (just emphasis that a numeric constant is positive)
• The unary – operator produces the negative of its operand
![Page 18: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/18.jpg)
18
Increment and decrement operators
++ is the increment operator i++;
is equivalent toi = i + 1;
-- is the decrement operatorj--;
is equivalent toj = j - 1;
(King, pp53-54)
![Page 19: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/19.jpg)
19
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
Example – Simple Expressions
![Page 20: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/20.jpg)
20
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
Example – Simple Expressions (cont)
![Page 21: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/21.jpg)
21
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){
return 0;}
Example – Simple Expressions (cont)
![Page 22: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/22.jpg)
22
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
return 0;}
Example – Simple Expressions (cont)
![Page 23: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/23.jpg)
23
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;
return 0;}
Example – Simple Expressions (cont)
![Page 24: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/24.jpg)
24
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);
return 0;}
Example – Simple Expressions (cont)
![Page 25: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/25.jpg)
25
Evaluate an expression
set result to 1 + 2 * 3 - 4 / 5
output result
Output: 7.000000
#include <stdio.h>
/* Evaluate an expression */
int main(){float result;
result = 1 + 2 * 3 - 4 / 5;printf(“%f\n”, result);
return 0;}
Example – Simple Expressions (cont)
![Page 26: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/26.jpg)
26
Topics
ExpressionsPrecedence
• Function calls
• Comments
![Page 27: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/27.jpg)
27
Function Calls
• Tell the computer to execute a series of C commands and (maybe) return a value– In algorithm-speak: An invocation of a named
sequence of instructions
• Example: printf, scanf, sqrt
![Page 28: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/28.jpg)
28
Find the square root of x
output ”Enter a number: "
input x
set myResult to result of squareRoot(x)
output myResult
Example – Find the square root
![Page 29: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/29.jpg)
29
Example – Find the square root (cont)#include <stdio.h>#include <math.h>/* Find square root */int main(){
/* declare variables */ float x,myResult;
/* output ”Enter a number: " */printf(“Enter a number\n”);/* input x */scanf(“%f”,&x);
/* set myResult to result of squareRoot(x) */myResult=sqrt(x);
/* output myResult */printf(“Result is %f\n”,myResult);return 0;
}
![Page 30: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/30.jpg)
30
Topics
History of CStructure of a C programValues and variablesExpressionsFunction calls
• Comments
![Page 31: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/31.jpg)
31
Comments
• Essential for documenting programs
• Run from a /* to the next */• Examples:
/* THIS IS A COMMENT */
/* So isthis */
/*** ...and this.***/
![Page 32: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/32.jpg)
32
Comments (cont)
• Comments do not “nest”
/* Comments start with a “/*”and end with a “*/”but they don’t nest! */
![Page 33: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/33.jpg)
33
Topics
ExpressionsPrecedenceFunction callsComments
![Page 34: 1 CSE1301 Computer Programming Lecture 5 C Primitives 2](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d355503460f94a0cd07/html5/thumbnails/34.jpg)
34
Reading
• King– Chapter 2, 2.1 – 2.3– Chapter 4, 4.1
• D&D: – Chapter 2, Sections 2.1 to 2.5
• Kernighan & Ritchie– Chapter 1, 1.2– Chapter 2, 2.1 – 2.3, 2.5 – 2.7, 2.12