1 Structured Programming in C Welcome to CPSC 206.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CS1061 C Programming Lecture 6: Operators and Expressions A. O’Riordan, 2004.
true (any other value but zero) false (zero) expression Statement 2
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
1 Structured Programming in C Welcome to CPSC 206.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Chapter 4: Basic C Operators
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C H A P T E R S E V E N Expressions and Assignment Statements.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
CSE 220 – C Programming Expressions.
Chapter 4 – C Program Control
UMBC CMSC 104 – Section 01, Fall 2016
Operators And Expressions
- Standard C Statements
Computing Fundamentals
Decisions Chapter 4.
Variable Declaration, Data types, Expressions
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Fundamentals
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Structure of a C Program
C Operators, Operands, Expressions & Statements
Associativity and Prescedence
Chapter 4 - Program Control
Relational and Logical Operators
Chap 7. Advanced Control Statements in Java
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Dale Roberts, Lecturer IUPUI
Relational and Logical Operators
Lecture 9: Implementing Complex Logic
Presentation transcript:

1 Structured Programming in C Welcome to CPSC 206

2 Lecture Information

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 Pointers, Arrays, and StringsCh 8, 9, Structures, and linked listsCh 12 Features of C: 7. Enumeration type and storage classesCh 7, 8 8. RecursionCh 11

4 Review of Class on Sept. 21, Thursday

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

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 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 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 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 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 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 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 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 int a=1, b=2, c=3, d=4; printf(“%d\n”, a + b * c * d / 14 % 13); a + b * c * 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 ) ( ) ( ) ( ( 200 * d / 14 % 13) ) ( ( 800 / 14 % 13) ( 57 % 13)

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 End of Chapter 2 Read Section 2.1 – 2.12

17 Class on Sept 23

18 Chapter 3 Flow of Control

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 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 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 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 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 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 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

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 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 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 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 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

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 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 > %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 x - y 1 x + c !, ++, -- (prefix) +, - (unary) RLRL * / % LRLR + - >= == != && || false true false

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

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 Relational, Equality, and Logical Operators  Outline  Introduction  Relational Operators  Equality Operators  Logical Operators

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

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 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 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 ) ! x * ! ! y !, ++ (prefix), -- (prefix), + (unary), - (unary) RLRL * / % LRLR + - >= == != && || ) ( ) ( ) ( ) ( ) ( ) ( ) ( = 2 = -6 = 1

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 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 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 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