ARITHMETIC IN C Operators.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Lecture 2 Introduction to C Programming
Introduction to C Programming
Chapter 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2 part #4 Operator
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Ch 6: JavaScript Introduction to scripting part 2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chapter 02 (Part II) Introduction to C++ Programming.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
REEM ALMOTIRI Information Technology Department Majmaah University.
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 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.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 6 JavaScript: Introduction to Scripting
Chapter 7: Expressions and Assignment Statements
Introduction to C++ Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Chapter 2 - Introduction to C Programming
Lecture 3 Expressions Richard Gesick.
Chapter 2 - Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
1) C program development 2) Selection structure
Chapter 2 - Introduction to C Programming
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: Java Fundamentals
Data Types and Expressions
Chapter 2 - Introduction to C Programming
Operator King Saud University
Introduction to C Programming
is multiplied by a variable of type to yield a result of type
Chapter 2, Part III Arithmetic Operators and Decision Making
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

ARITHMETIC IN C Operators

Program Next step to focus on is the process (operations) Keyboard Screen Processing input data output data

1  Arithmetic in C ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1  Arithmetic in C (Cont.) Most C programs perform calculations using the C arithmetic operators (Fig. 2.9). Note the use of various special symbols not used in algebra. The asterisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below. In algebra, to multiply a times b, we simply place these single-letter variable names side by side as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier). Therefore, C requires that multiplication be explicitly denoted by using the * operator as in a * b. The arithmetic operators are all binary operators. For example, the expression 3 + 7 contains the binary operator + and the operands 3 and 7. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.1  Arithmetic in C (Cont.) Integer Division and the Remainder Operator Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.2 Arithmetic in C (Cont.) Parentheses for Grouping Subexpressions Parentheses are used in C expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a * ( b + c ) ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.3 Arithmetic in C (Cont.) Rules of Operator Precedence C applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra: Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the “highest level of precedence.” In cases of nested, or embedded, parentheses, such as ( ( a + b ) + c ) the operators in the innermost pair of parentheses are applied first. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.3  Arithmetic in C (Cont.) Multiplication, division and remainder operations are applied next. If an expression contains several multiplication, division and remainder operations, evaluation proceeds from left to right. Multiplication, division and remainder are said to be on the same level of precedence. Addition and subtraction operations are evaluated next. If an expression contains several addition and subtraction operations, evaluation proceeds from left to right. Addition and subtraction also have the same level of precedence, which is lower than the precedence of the multiplication, division and remainder operations. The assignment operator (=) is evaluated last. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.3  Arithmetic in C (Cont.) The rules of operator precedence specify the order C uses to evaluate expressions. When we say evaluation proceeds from left to right, we’re referring to the associativity of the operators. Figure 2.10 summarizes these rules of operator precedence for the operators we’ve seen so far. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

1.3 Arithmetic in C (Cont.) EX: What is the result of the following expression? In c , the expression is: 2 * 5 * 5 + 3 * 5 + 7 Figure 2.11 illustrates the order in which the operators are applied. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

LOWER HIGHER 2. Relational and equality operators ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2. Relational and equality operators (cont.) The relational operators used in the C language are shown in Fig. 2.12. The relational operators all have the same level of precedence and they associate left to right. The equality operators have a lower level of precedence than the relational operators and they also associate left to right. In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3. A SAMPLE PROGRAM ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3. A SAMPLE PROGRAM (Cont’d) ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3. A SAMPLE PROGRAM (Cont’d) The program uses scanf (line 15) to input two numbers. Each conversion specifier has a corresponding argument in which a value will be stored. The first %d converts a value to be stored in the variable num1, and the second %d converts a value to be stored in variable num2. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

4. rules of precedence - revisited Figure 2.14 lists from highest to lowest the precedence of the operators introduced in this chapter. Operators are shown top to bottom in decreasing order of precedence. The equals sign is also an operator. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

the assignment Operator Assignment is used to assign the values for the variables in C programs. Assignment operator stores a value in memory. The syntax is leftSide = rightSide ; Examples: i = 1; first = i; sum = firstNumber + secondNumber; It is either a literal | a variable identifier | an expression. Allways it is a variable identifier.

2.6 the assignment statement Integer1 + integer2 = sum Dr. Soha S. Zaghloul 26 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2.8 Memory Concepts Remember in previous example: int integer1 , integer 2 , sum; Variable names such as integer1, integer2 and sum actually correspond to locations in the computer’s memory. EX: integer1 = 45 ; integer 2 = 72; sum = 0; is executed, the value is placed into a memory location to which the name integer1 , integer2 , sum , has been assigned. Dr. Soha S. Zaghloul 27 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

sum Dr. Soha S. Zaghloul 28 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2.8 Memory Concepts (Cont.) Whenever a value is placed in a memory location, the value replaces the previous value in that location; thus, this process is said to be destructive. These locations are not necessarily adjacent in memory. EX: sum = integer1 + integer2; /* assign total to sum */ that performs the addition also replaces whatever value was stored in sum. Dr. Soha S. Zaghloul 29 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2.8 Memory Concepts (Cont.) This occurs when the calculated total of integer1 and integer2 is placed into location sum (destroying the value already in sum). After sum is calculated, memory appears as in Fig. 2.8. The values of integer1 and integer2 appear exactly as they did before they were used in the calculation. Dr. Soha S. Zaghloul 30 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Value of sum has changed after assignment Dr. Soha S. Zaghloul 31 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2.8 Memory Concepts (Cont.) They were used, but not destroyed, as the computer performed the calculation. Thus, when a value is read from a memory location, the process is said to be nondestructive. Dr. Soha S. Zaghloul 32 Copyright © Pearson, Inc. 2013. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.