C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Advertisements

Lecture 1 The Basics (Review of Familiar Topics).
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Assignment Operators =, +=, *= A += B means (A+B) --->A or A = (A+B) Similarly true for -=, *=, /=, and %=. The basic rule is from right to left. Never.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Chapter 2 Data Types, Declarations, and Displays
Data Types, Expressions and Functions (part I)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2 part #4 Operator
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Unit 3, Lesson 3 Math Operations, Assignment Operators, and Arithmetic Operators Mr. Dave Clausen La Cañada High School
Object Oriented Programming with C++ Diploma in Computer System Design.
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
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.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Overview of the c Programming Language An Overview of the C/C++ Programming.
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.
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.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSE 220 – C Programming Expressions.
Computing Fundamentals
Variable Declaration, Data types, Expressions
Operators and Expressions
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
A First Book of ANSI C Fourth Edition
Conversion Check your class notes and given examples at class.
Data Types and Expressions
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side of + -Subtractionx = 6 - 2;Subtract right value from left value * Multiplicationx = 6 * 2;Subtract right hand side values /Divisionx = 6/2;Divide left value by right value %Modulusx = 6 % 2;Remainder of left divided by right What about exponentiation??? Either x = 6 ^ 2 OR x = 6 ** 2 ??? NOTE: Dividing 2 real numbers yields EXACT results Dividing 2 Integers yields TRUNCATED results No Such Thing: You must write your own function (Or use an available one) Why??

C/C++ Operators Previously, we talked about how integers were stored. For example, we said the declaration short s1 = 56, s2 = 17; Asks for two 16-bit integer locations (s1 and s2) and stores the values 56 and 17 in them (respectively) Var/Loc s1: Var/Loc s2: We know that if we divide 56/17 (on a calculator) you would get the value But, how do you store a mantissa as an integer??(You don’t) OK – But what if I wanted to get the real answer above?? All Integers can be rewritten (as floats (casting)) The opposite is NOT true Decimal to Binary Converter

C/C++ Operators Consider (i.e., enter, build and run) the code below: #include "stdafx.h" #include using namespace std; void main() { int s1 = 56, s2 = 17; cout << s1 << " divided by " << s2 << " is " << s1/s2 << endl; cout << "Casting " << s1 << " and " << s2 << " and then dividing yields: " << (float)s1/(float)s2 << endl; } The output should appear as:

C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator cout << "The remainder of 9/2 is " << 9 % 2 << endl; Add the following line to your program, compile and run: How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary

C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator cout << "The remainder of 9/2 is " << 9 % 2 << endl; Add the following line to your program, compile and run: How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary

Converting from decimal to binary Dividing any integer by 2 means that the remainder must be either 0 or 1 A few slides ago, we noted that = How can we verify this?? When you first learned division, you probably were taught to do it as follows: goes into 56, 28 times. 2 times 28 is 56. Subtracting -56 from 56 leaves a remainder of goes into 28, 14 times. 2 times 14 is 28. Subtracting -28 from 28 leaves a remainder of goes into 14, 7 times. 2 times 7 is 14. Subtracting -14 from 14 leaves a remainder of goes into 7, 3 times. 2 times 3 is 6. Subtracting -6 from 7 leaves a remainder of goes into 3, 1 times. 1 times 3 is 3. Subtracting -3 from 7 leaves a remainder of goes into 1, 0 times. 0 times 1 is 0. Subtracting 0 from 1 leaves a remainder of 1

Converting from decimal to binary Let’s rewrite this procedure in a simpler form Pass remainder 1 quotient Gathering the remainders from last to first: Stop when the quotient becomes =56 How do we do that?? We’re going to have to go over your first abstract data type: An Array

Arrays Let’s create an array of short integers An array is an arrangement of similar basic data types short binary[15], temp[15], decval = 56, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; Next, we’ll create the array as we did before Now we need to reverse the order of the array i = index; while (reverse <= index) { binary[reverse] = temp[i]; reverse++; i--; } The complete programming (including the printing of the result) is given on the next slide

Arrays #include "stdafx.h" void main() { short binary[15], temp[15], decval = 177, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; for (i = 0; i <= index; i++) printf("%d",temp[i]); printf("\n\n"); i = index; while (reverse <= index) { binary[reverse] = temp[i]; printf("%d %d %d\n",i, reverse, binary[reverse]); reverse++; } i--; for (i = 0; i <= index; i++) printf("%d", binary[i]); printf("\n"); } As usual, you should enter the code, compile it, and run it

Assignment Operators Assignment Operators: Assigning Values to RAM: Operator = Example x = 6 + 2; Definition. Location x will get the value ( = 8) +=x += 3 IF Same as x = x + 3: IF x contained the value 4, it now contains the value 7 IF Same as x = x - 3: IF x contained the value 4, it now contains the value 1 =x = 3 *=x *= 3 IF Same as x = x * 3: IF x contained the value 4, it now contains the value 12 /=x /= 3 IF Same as x = x / 3: IF x contained the value 4: IF IF x is a float (real), it now contains 1.33 IF IF x is an integer, it now contains the value 1 %=x %= 3 IF ONE Same as x = x % 3: IF x contained the value 4, it now contains the value 1 (3 goes into 4 Once with ONE left over)

Unary Operators Unary Operators: Operators on a single Operand: Operator - Meaning Minus Sign Example x = -2; Definition. Assign a negative value +Plus Signx = +2;Assign a Positive value ++Increment x = ++y; Prefix Notation OR OR x = y++; Postfix Notation -- Decrement x = --y; Prefix Notation OR OR x = y--; Postfix Notation Prefix and Postfix Notation?? What’s that all About ??? int x,y,a,b; a = b = 1;// Initialize the variables (locations) with the value 1 x = 2 * ++a;// PREFIX Notation: First increment a by 1 (a = 2) // Then Multiply by 2 & assign to x (x=2*2) y = 2 * b++;// POSTFIX Notation: First multiply b by 2 & assign to y (y=1*2) // Then increment b by 1 (b = 2) 4 2 printf("%d %d\n",x,y); // The Output would appear as: 4 2 Consider the following section of c code:

There are two (2) Additional Unary Operators: sizeofReturn the size of the operand (in bytes) int i = 12; float f = ; printf("%d sizeof = %d\n",i,sizeof i); // would produce: 12 sizeof = 2 printf("%7.2f sizeof = %d\n",f,sizeof f); // would produce: sizeof = 4 Consider the following section of c code: (type) Convert a value to the specified data type int a = 4; float b = ; printf("%d %ld\n",a,(long) a);// would produce: 4 4 printf("%d %f\n",a,(float) a);// would produce: printf("%f %d\n",b,(int) b); }// would produce: Consider the following section of c code:

Relational Operators Relational Operators: Operators on two Operands Yielding a T/F Answer: OperatorMeaningExampleDefinition. < Less Than x < 2;False if x contains the value 2 <= Less Than OR Equal To x <= 2;True if x contains the value 2 = Equal To x = = 2;True if x contains the value 2 ! = Not Equal To x ! = 2;False if x contains the value 2 > Greater than x > 2;False if x contains the value 2 >= Greater than OR Equal To x >= 2;True if x contains the value 2 && And x > 2 && x < 4 False if x contains the value 2 | Or x = = 2 | | x = = 4 True if x contains the value 2 ! Not! (x = = 2) False if x contains the value 2

Order of Operations: Order 1 Operator ( ) (unary) ! (Rel) 3 * / % (binary) 5 < > <= >= 6 == != 9 = Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; What is the value of the statement: a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1; | | 8 && 7

Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; = 56 * 7 = = 485 % 4 = 1 1 * 1 = 1 1 / 5 = 0 * 8 = 384 / 4 = = = - 95

Example 2: Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; Evaluate: ++a * b-- + c % d / e - f-- * g + 6 * h++ / b + h; 2 2 * 2 = 43 % 4 = 3 3 / 5 = 0 6 * 7 = 426 * 8 = / 2 = = = = = - 6 Note that now: a = 2, b = 1, f = 5, h = 9

Example 3: Given: int a = 1, b = 2, c = 3, d = 4; Consider the statement: a > b || b < c && c == 3 || d < 4 && b != c FalseTrueFalse True False True

scanfprintf Conversion specifiers for scanf and printf: Specifier(s)Output %cSingle Character %d %iSigned decimal Integer %fSigned floating-point, decimal notation Example B %e %ESigned floating-point, e or E notation -4.5e3 2.1E-2 %g %GUse shorter or %f or %e (%f or %E) E4 %oUnsigned octal notation4271 %u7832Unsigned decimal Integer %ld %luSigned/Unsigned long Integer %Lf %Le Long double floating-pt (also %LE) e %x %XUnsigned hexadecimal notation4d2a F6B %sCharacter stringHello %pPointer (address)4FF0: 8BC1 %Print a % sign% Additional information about printf/scanf specifiers can be found in the Supplementary Materials Link

Additional Precompiler Directives #define PI = User Defined constants: #define SQUARE (x) x * x User Defined constants: // macro example.cpp : Defines the entry point for the console application. #include "stdafxh" #include using namespace std; #define PI #define SQUARE(X) X*X void main() { float area, r = SQUARE(3.5); area = PI * r; cout << "The area is:" << area << endl; }