1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions.

Slides:



Advertisements
Similar presentations
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Advertisements

1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
JavaScript, Third Edition
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Computer Science: A Structured Programming Approach Using C1 3-7 Sample Programs This section contains several programs that you should study for programming.
Operaciones y Variables
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
OPERATORS.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
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.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Arithmetic Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Selection—Making Decisions
Nahla Abuel-ola / WIT.
Chapter 9 Pointers Objectives
Arithmetic Operator Operation Example + addition x + y
3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured.
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Topics discussed in this section:
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
A First Book of ANSI C Fourth Edition
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
C Operators, Operands, Expressions & Statements
Data Types and Expressions
Topics discussed in this section:
Using C++ Arithmetic Operators and Control Structures
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions ❏ To understand the result of side effects in expression evaluation ❏ To be able to predict the results when an expression is evaluated ❏ To understand implicit and explicit type conversion ❏ To understand and use the first four statement types: null, expression, return, and compound Chapter 3 Chapter 3 Structure of a C Program Structure of a C Program

2 3-1 Expressions An expression ( 運算式 ) is a sequence of operands ( 運算元 ) and operators ( 運算子 ) that reduces to a single value.An expression ( 運算式 ) is a sequence of operands ( 運算元 ) and operators ( 運算子 ) that reduces to a single value. Expressions can be simple or complex.Expressions can be simple or complex. An operator is a syntactical token that requires an action be taken.An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed; it receives an operator’s action.An operand is an object on which an operation is performed; it receives an operator’s action.

3 FIGURE 3-1 Expression Categories An expression always reduces to a single value. Note Primary: Name (variable): a b12 price calc … Literal Constants ( 文數字常數 ) : ‘A’ “Welcome” 後序 前序 三元 一元 二元 主要

4 FIGURE 3-2 Postfix Expressions ( 後序運算式 ) 運算子 運算元

5 FIGURE 3-3 Result of Postfix a++ (a++) has the same effect as (a = a + 1) Note The operand in a postfix expression must be a variable. Note

6 PROGRAM 3-1Demonstrate Postfix Increment

7 FIGURE 3-4 Prefix Expression ( 前序運算式 )

8 FIGURE 3-5 Result of Prefix ++a The operand of a prefix expression must be a variable. Note (++a) has the same effect as (a = a + 1) Note

9 PROGRAM 3-2Demonstrate Prefix Increment

10 If ++ is after the operand, as in a++, the increment takes place after the expression is evaluated. If ++ is before the operand, as in ++a, the increment takes place before the expression is evaluated. Note

11 FIGURE 3-6 Unary Expressions ( 一元運算式 ) Examples of Unary Plus And Minus Expressions Table 3-1

12 FIGURE 3-7 Binary Expressions ( 二元運算式 )

13 Both operands of the modulo operator (%) must be integral types. Note

14 PROGRAM 3-3 Binary Expressions

15 The left operand in an assignment expression must be a single variable. Note Table 3-2 Expansion of Compound Expressions ( 複合運算式 )

16 PROGRAM 3-4 Demonstration of Compound Assignments

Precedence and Associativity Precedence is used to determine the order in which different operators in a complex expression are evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression. Precedence ( 優先順序 ) Associativity ( 結合性 ) Topics discussed in this section:

18 PROGRAM 3-5Precedence

19

20 FIGURE 3-8 Left-to-Right Associativity FIGURE 3-9 Right-to-Left Associativity

Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression on the right of the assignment operator and then places the value in the left variable. Changing the value of the left variable is a side effect.

Evaluating Expressions Now that we have introduced the concepts of precedence, associativity, and side effects, let’s work through some examples. Expressions without Side Effects Expressions with Side Effects Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C23 PROGRAM 3-6 Evaluating Expressions

Type Conversion Up to this point, we have assumed that all of our expressions involved data of the same type. But, what happens when we write an expression that involves two different data types, such as multiplying an integer and a floating-point number? To perform these evaluations, one of the types must be converted. Implicit Type Conversion Explicit Type Conversion (Cast) Topics discussed in this section:

25 FIGURE 3-10 Conversion Rank

Computer Science: A Structured Programming Approach Using C26 PROGRAM 3-7 Implicit Type Conversion Visual C++ 不支援 bool 型態

Computer Science: A Structured Programming Approach Using C27 Explicit Casts PROGRAM 3-8

Statements A statement ( 敘述 ) causes an action to be performed by the program. It translates directly into one or more executable computer instructions. You may have noticed that we have used a semicolon at the end of the statements in our programs. Most statements need a semicolon at the end; some do not. You may have noticed that we have used a semicolon at the end of the statements in our programs. Most statements need a semicolon at the end; some do not. Statement Type The Role of the Semicolon Statements and Defined Constants Topics discussed in this section:

29 FIGURE 3-11 Types of Statements

30 FIGURE 3-12 Compound Statement The compound statement does not need a semicolon. Note

Sample Programs This section contains several programs that you should study for programming technique and style.

Computer Science: A Structured Programming Approach Using C32 PROGRAM 3-9 Calculate Quotient and Remainder

Computer Science: A Structured Programming Approach Using C33 PROGRAM 3-10 Print Right Digit of Integer

Computer Science: A Structured Programming Approach Using C34 PROGRAM 3-11 Calculate Average of Four Numbers

35 PROGRAM 3-11Calculate Average of Four Numbers

Computer Science: A Structured Programming Approach Using C36 PROGRAM 3-12 Convert Radians to Degrees

37 PROGRAM 3-13 Calculate Sales Total

38 PROGRAM 3-13Calculate Sales Total

39 PROGRAM 3-14 Calculate Student Score

Computer Science: A Structured Programming Approach Using C40

Computer Science: A Structured Programming Approach Using C41