Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Structured Programming in C Welcome to CPSC 206.

Similar presentations


Presentation on theme: "1 Structured Programming in C Welcome to CPSC 206."— Presentation transcript:

1 1 Structured Programming in C Welcome to CPSC 206

2 2 Lecture Information http://people.cs.tamu.edu/ychen/Teaching/CPSC206

3 3 Lecture Topics: 0. Introduction to Computer Science 1. Overview of CCh 1, 2 2. Flow of control and functionsCh 3, 4 3. Character processing & fundamental data typesCh 5, 6 4. File I/O Ch 13 5. Pointers, Arrays, and StringsCh 8, 9, 10 6. Structures, and linked listsCh 12 Features of C: 7. Enumeration type and storage classesCh 7, 8 8. RecursionCh 11

4 4 Review of Class on Sept. 21, Thursday

5 5 Chapter 2: Lexical Elements, Operators, and the C System

6 6 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

7 7 Operators and Punctuators — Increment ++ and Decrement Operators --  Increment Operator ++i, i++  Each of the expressions ++i and i++ has a value. ++i othe stored value of i is incremented first othe expression takes as its value the new stored value of i i++ othe expression takes as its value the current stored value of i othe stored value of i is incremented Lexical Elements Lexical Elements, Operators, and the C System

8 8 Operators and Punctuators — Increment ++ and Decrement Operators --  Decrement Operator i-- and --i  Each expression has a value. --i othe stored value of i is decremented by 1 othe expression takes as its value the new stored valued of i i-- othe expression takes as its value the current stored valued of i othe stored value of i is decremented by 1 Lexical Elements Lexical Elements, Operators, and the C System

9 9 Operators and Punctuators — Increment ++ and Decrement Operators --  Precedence and Associativity Associativity  ++ (postfix) -- (postfix) Left to right  +(unary) –(unary) ++(prefix) --(prefix) Right to left  * / % Left to right  + -Left to right Lexical Elements Lexical Elements, Operators, and the C System

10 10 Operators and Punctuators — Increment ++ and Decrement Operators --  Rules  Applied to variables but not to constants or ordinary expressions Lexical Elements Lexical Elements, Operators, and the C System

11 11 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

12 12 Operators and Punctuators — Assignment Operators  Assignment operators  =  op=: +=, -=, *=, / =, %=, ……  Semantics:  variable op= expression equivalent to variable = variable op (expression)  Example: var*= expr  var=var * expr a *= 3  a = a * 3 oif a was 4 before the assignment, then a = 12 after that. Lexical Elements Lexical Elements, Operators, and the C System

13 13 Operators and Punctuators — Assignment Operators  Assignment operators  Precedence: all the assignment operators have the same precedence Lower than all the other operators which have been introduced (such as + - )  Associativity: right to left Lexical Elements Lexical Elements, Operators, and the C System

14 14 int a=1, b=2, c=3, d=4; printf(“%d\n”, a + b * c + 200 * d / 14 % 13); a + b * c + 200 * d / 14 % 13 Precedence and Associativity ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right Question 1 in Problem Set 6 ) ( ) ( ) ( 1 + 6 +( 200 * d / 14 % 13) ) ( 1 + 6 +( 800 / 14 % 13) 1 + 6 +( 57 % 13) 1 + 6 + 5

15 15 int a=1, b=2, c=3, d=4, e=1; printf(“%d\n”, e *= d += (a = b++) – (b = --c)); e *= d += ( a = b++ ) – ( b = --c ) Precedence and Associativity ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right ….. = += -= *= /= right to left Question 2 in Problem Set 6 ) ( )( e *= d += ( a = 2 ) – ( b = 2 ) e *= d += 2 – 2 e *= d += 0 )( ) ( e *= 4 d += 0  d = d + 0 e *= 4  e = e * 4

16 16 End of Chapter 2 Read Section 2.1 – 2.12

17 17 Class on Sept 23

18 18 Chapter 3 Flow of Control

19 19 Introduction  Sequential flow of control  Statements in a program are executed one after another. /*The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972.*/ #include int main(void) { printf(“Hello, world!\n”); return 0; } hello.c

20 20 Introduction  Nonsequential control  We have seen this example A telephone company bases its rates for long distance calls on othe time of day and othe day of the week when a call is made.

21 21 Introduction Weekday Weekend Long distance charges 8am to 5pm 5pm to 11pm 11pm to 8am Sat. Sun evening rate full rate night rate 8am to 5pm 5pm to 11pm 11pm to 8am evening rate night rate Flow of Control: Select among alternative actions  The condition to select a specific action  Statement to implement selection:  if, if-else, switch statement Decision Tree

22 22 Introduction  Another scenario /* Some powers of 2 are printed. */ #include int main(void) { int exp = 0, power_of_two = 1; while (++exp <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; } Flow of Control: Achieve iterative actions  The condition to end the iterative action.  Statement to implement iteration:  while, for, do statement

23 23 Introduction  Flow of Control  Sequential flow of control Statements in a program are executed one after another.  Selection: select among alternative actions The condition to select a specific action Select: if, if-else, switch statement  Iteraton: achieve iterative actions The condition to end the iterative action. Iteration: while, for, do statement

24 24 Outline of Chapter 3  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement and empty statement  Select among alternative actions The if and if-else statement The switch statement Select using operator: The conditional Operator  Achieve iterative actions The while statement The for statement The do statement  Nested Flow of Control

25 25 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

26 26 Relational, Equality, and Logical Operators — Introduction  Overview  They are most often used to affect flow of control  Relational operator =  Equality operators ==, !=  Logical operators !, &&, || truefalse These operators are used in expressions that we think of as being true or false. truefalse i Questions: how to represent true or false in C?

27 27 Relational, Equality, and Logical Operators — Introduction  How true and false are implemented in C  Representation of true and false false: represented by any zero value oint 0 ofloating 0.0 oNull character ‘\0’ oNull Pointer ( will be introduced in Chapter 8) true: represented by any nonzero value int int  These operators yield either the int value 0 (false) or the int value 1 (true).

28 28 Relational, Equality, and Logical Operators — Introduction  Note:  Any nonzero value can be used to represent true, int  But the relational, equality and logical operators yield the int value 1 for true.

29 29 Relational, Equality, and Logical Operators — Introduction  Precedence and associativity OperatorsAssociativity ++(postfix) –(postfix)Left to right !,++(prefix),--(prefix),+(unary),-(unary)Right to left …… + -Left to right Relational: >=Left to right Equality: == !=Left to right Logical: &&Left to right Logical: ||Left to right

30 30 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

31 31 Relational Operators and Expressions  Relational operators:  Four types of operators:, =  Operands: takes two expressions as operands  Result:int value 0(false) or int value 1(true).  Examples  1<1  1<=1  3>9  Variable: a<3  Complex Expression: -1.1>=(2.2*x+3.3)  false  0  true  1  false  0

32 32 Relational Operators and Expressions #include int main(void) { int a=1, b=2, c=3; double x=5.5, y=7.7; printf(“a %d\n", a<b-c); printf("- a + 5 * b >= c + 1 ---> %d\n", - a + 5 * b >= c + 1); printf(" x - y %d\n", x - y <= b - c - 1); printf(" x + c + 7 %d\n", x + c + 7 < y / c ); return 0; } % gcc rel.c % a.out a 0 - a + 5 * b >= c + 1 ---> 1 x - y 1 x + c + 7 0 !, ++, -- (prefix) +, - (unary) RLRL * / % LRLR + - >= == != && || false true false

33 33 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

34 34 Equality Operators and Expressions  Equality Operators:  == and !=  Operands: takes two expressions as operands  Result:int value 0(false) or int value 1(true).  Examples:  ‘A’ == ‘B’  ‘A’ != ‘B’  Variable: count !=-2  Complex expression: x+y == 2*z -5

35 35 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

36 36 Logical Operators and Expressions  Logical operators:  Three types of operators: Logical negation! && ||

37 37 Logical Operators and Expressions  Logical negation !  Unary: it takes only one operand  Result of !a a is a zero value  int value 1 a is a nonzero value  int value 0  Example:  !10 = 0  !(x+7.7)

38 38 Logical Operators and Expressions  Examples of Logical negation !  !!5 = ? !(!5) !, ++ (prefix), -- (prefix), + (unary), - (unary) RLRL * / % LRLR + - >= == != && || 5? = !0 =1 Precedence and Associativity

39 39 Logical Operators and Expressions  Examples of Logical negation ! int a=7, b = 7; double x=0.0, y=999.9; ! ( a – b ) + 1 ! a – b + 1 ! ! ( x + 3.3 ) ! x * ! ! y !, ++ (prefix), -- (prefix), + (unary), - (unary) RLRL * / % LRLR + - >= == != && || ) ( ) ( ) ( ) ( ) ( ) ( ) ( = 2 = -6 = 1

40 40 Logical Operators and Expressions  Logical operators: && and ||  a && b Return 1 (true) if both a and b are nonzero Otherwise return 0 (false).  a || b Return 0 (false) if both a and b are zero Otherwise return 1 (true)

41 41 Logical Operators and Expressions  Examples int i = 3, j=3, k=3; double x=0.0, y=2.3; i && j && k x || j && j -3 i < j && x < y i<j || x<y !, ++ (prefix), -- (prefix), + (unary), - (unary) RLRL * / % LRLR + - >= == != && || ) ( = 1 )()( = 0 )() ( )()( = 1

42 42 Relational, Equality and Logical Operators  Summary  Logical negation !a Return 1 if a is zero value Otherwise return 0  a && b Return 1 (true) if both a and b are nonzero Otherwise return 0 (false).  a || b Return 0 (false) if both a and b are zero Otherwise return 1 (true) !, ++ (prefix), -- (prefix), + (unary), - (unary) RLRL * / % LRLR + - >= == != && ||

43 43 Relational, Equality, and Logical Operators  Summary  Introduction True: non zero value False: zero value  Relational Operators =  Equality Operators == !=  Logical Operators ! && || Read Section 3.1 to 3.4


Download ppt "1 Structured Programming in C Welcome to CPSC 206."

Similar presentations


Ads by Google