Structure of a C Program

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

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Computer Science: A Structured Programming Approach Using C1 3-7 Sample Programs This section contains several programs that you should study for programming.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
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.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
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 *
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Arithmetic Expressions
Variables, Operators, and Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators And Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 2 - Introduction to C Programming
TMF1414 Introduction to Programming
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2 - Introduction to C Programming
Java Programming: From Problem Analysis to Program Design, 4e
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.
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Chapter 2 - Introduction to C Programming
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
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.
Chapter 2: Basic Elements of Java
Arithmetic Expressions & Data Conversions
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
Chapter-3 Operators.
Introduction to Programming
Associativity and Prescedence
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Chapter 3 Operators and Expressions
Data Types and Expressions
Chapter 4: Expression and Operator
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Introduction to C Programming
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
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Structure of a C Program Chapter 3 Structure of a C Program 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 Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 3-1 Expressions An expression is a sequence of operands and operators that reduces to a single value. Expressions can be simple or complex. 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. Topics discussed in this section: Primary Expressions Postfix Expressions Prefix Expressions Unary Expressions Binary Expressions Computer Science: A Structured Programming Approach Using C

An expression always reduces to a single value. Note An expression always reduces to a single value. Computer Science: A Structured Programming Approach Using C

FIGURE 3-2 Postfix Expressions Computer Science: A Structured Programming Approach Using C

(a++) has the same effect as (a = a + 1) Note (a++) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C

FIGURE 3-3 Result of Postfix a++ Computer Science: A Structured Programming Approach Using C

The operand in a postfix expression must be a variable. Note The operand in a postfix expression must be a variable. Computer Science: A Structured Programming Approach Using C

Demonstrate Postfix Increment PROGRAM 3-1 Demonstrate Postfix Increment Computer Science: A Structured Programming Approach Using C

Demonstrate Postfix Increment (continued) PROGRAM 3-1 Demonstrate Postfix Increment (continued) Note 1: The statement printf(“a++: %d\n”, a++); is equal to the following two statements: printf(“a++: %d\n”, a); a++; Note 2: The statement printf(“++a: %d\n”, ++a); is equal to the following two statements: ++a; printf(“++ a : %d\n”, a); Computer Science: A Structured Programming Approach Using C

FIGURE 3-4 Prefix Expression Computer Science: A Structured Programming Approach Using C

The operand of a prefix expression must be a variable. Note The operand of a prefix expression must be a variable. Computer Science: A Structured Programming Approach Using C

FIGURE 3-5 Result of Prefix ++a Computer Science: A Structured Programming Approach Using C

(++a) has the same effect as (a = a + 1) Note (++a) has the same effect as (a = a + 1) Computer Science: A Structured Programming Approach Using C

Demonstrate Prefix Increment PROGRAM 3-2 Demonstrate Prefix Increment Computer Science: A Structured Programming Approach Using C

Demonstrate Prefix Increment (continued) PROGRAM 3-2 Demonstrate Prefix Increment (continued) Computer Science: A Structured Programming Approach Using C

Note 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. Computer Science: A Structured Programming Approach Using C

FIGURE 3-6 Unary Expressions Computer Science: A Structured Programming Approach Using C

Examples of Unary Plus And Minus Expressions Table 3-1 Examples of Unary Plus And Minus Expressions Computer Science: A Structured Programming Approach Using C

FIGURE 3-7 Binary Expressions Computer Science: A Structured Programming Approach Using C

Both operands of the modulo operator (%) must be integral types. Note Both operands of the modulo operator (%) must be integral types. Computer Science: A Structured Programming Approach Using C

PROGRAM 3-3 Binary Expressions Computer Science: A Structured Programming Approach Using C

Binary Expressions (continued) PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C

Binary Expressions (continued) PROGRAM 3-3 Binary Expressions (continued) Computer Science: A Structured Programming Approach Using C

Note The left operand in an assignment expression must be a single variable. Computer Science: A Structured Programming Approach Using C

Expansion of Compound Expressions Table 3-2 Expansion of Compound Expressions Computer Science: A Structured Programming Approach Using C

Demonstration of Compound Assignments PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C

Demonstration of Compound Assignments PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C

Demonstration of Compound Assignments PROGRAM 3-4 Demonstration of Compound Assignments Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 3-2 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. Topics discussed in this section: Precedence Associativity Computer Science: A Structured Programming Approach Using C

PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C

PROGRAM 3-5 Precedence Computer Science: A Structured Programming Approach Using C

Computer Science: A Structured Programming Approach Using C

FIGURE 3-8 Left-to-Right Associativity Computer Science: A Structured Programming Approach Using C

FIGURE 3-9 Right-to-Left Associativity Associativity used when operators have same precedence: the += type operatorrs use R->L associativity, basic math operators like * use L->R FIGURE 3-9 Right-to-Left Associativity Computer Science: A Structured Programming Approach Using C

So you can have things like: int i =1; int j =2; i = j So you can have things like: int i =1; int j =2; i = j*= 2; // i = j = 4 after i += j+=2; // j=6, i = 10 // after Computer Science: A Structured Programming Approach Using C

You can actually say int i, sum; i = sum = 3 +1; // Right to left // associativity And i will equal 4, but it is not good programming practice. Computer Science: A Structured Programming Approach Using C

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 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. Computer Science: A Structured Programming Approach Using C

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

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

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

Warning Advices When possible, avoid expressions that are too complicated and may cause confusion. Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 3-5 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. Topics discussed in this section: Implicit Type Conversion Explicit Type Conversion (Cast) Computer Science: A Structured Programming Approach Using C

FIGURE 3-10 Conversion Rank Computer Science: A Structured Programming Approach Using C

Implicit Type Conversion PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C

Implicit Type Conversion PROGRAM 3-7 Implicit Type Conversion Computer Science: A Structured Programming Approach Using C

Implicit Type Conversion PROGRAM 3-7 Implicit Type Conversion Note: Not true. The value should be 0. Computer Science: A Structured Programming Approach Using C

Implicit Conversion of Ints and Floats int i = 3; float f = 2.5; i = f; // i will equal 2 after this f = 3/i; // f will equal 1.0 after this; // int / int performs an integer division f = i + 0.6; // f will equal 2.6 after this i = 2.0 * 2.4; // i will equal 4 after this Computer Science: A Structured Programming Approach Using C

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

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

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

3-6 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. Computer Science: A Structured Programming Approach Using C

FIGURE 3-12 Compound Statement Computer Science: A Structured Programming Approach Using C

3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C

PROGRAM 3-13 Calculate Sales Total Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C

Calculate Student Score PROGRAM 3-14 Calculate Student Score Computer Science: A Structured Programming Approach Using C