C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Types and Arithmetic Operators
Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
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.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
Operators & Expressions
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
C++ Programming Language Lecture 4 C++ Basics – Part II
CSE 220 – C Programming Expressions.
Operators And Expressions
Operators and Expressions
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Relational Operations
Intro to C Tutorial 4: Arithmetic and Logical expressions
By: Muhammad Zidny Naf’an
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 5 from (Chapter 4, pages 73 to 96)
OPERATORS (2) CSC 111.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3 Operators and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Presentation transcript:

C++ Basics Tutorial 6 Operators

What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..) Increment and decrement(++, --) Relational and equality(==, !=, >, =, <=) Logical(!, &&, ||) Conditional(?:) Comma operator(,) Bitwise operator(&, |, ^, ~, >) sizeof() Precedence

Assignment operator(=) Assign a value to variable Example: x=23; Right to left rule. Assigns always the right part to left. C++ allows to use ‘=‘ as rvalue too Example x=23+(y=53); is same as y=53; x = 23+y; Multiple assignment operator is also valid a=b=c= 23 assigns 23 to all a, b and c lvalue rvalue

Arithmatic operators (+, -, /, *, %) + is addition, - is subtraction, * multiplication, / is division and % is modulo. When % is used between two numbers it will give the remainder of the division between two numbers. Example: 15%4 = 3

Compound Assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) Changing the value of a variable, using the current value stored in the same variable, compound assignment become useful Example: a+=5 is same as a = a+5 (add 5 more to whatever is in a and replace the old value of a to a+5). Similar to a-=5; or a/=5, a*=b%2 is same as a = a*(b%2);

Increment and Decrement (++, --) ++: Increment the value in variable by one unit Same as +=1; Example: a++, a+=1; both increase the value of a by one. --: Decrement the value stored in variable by one unit Same as -=1; Example a--, a-=1; both decrease the value of a by one. Can be used and prefix or suffix Example a++ or ++a In case result of this operation and evaluated and stored, writing a++ or ++a makes a big difference. Example: a=5; b=a++; gives us a=6 and b=5 But a = 5; b=++a; gives a=6 and b=6

Relational and equality operators (==, !=, >, =, <=) == Equal to? (not same as ‘=‘) != Not equal to? > greater than? < smaller than? >= greater than or equal to? <= smaller than or equal to? Example: (23==53) is false (23>=23) is true Or using variable: (a>b) returns true if a is bigger than b (a!=b) returns true if a is not equal to b

Logical operators (&&, ||, ! ) && and || are used to obtain a relational result between two expressions. Example: If we have two expression a and b, then we can use as (a && b) or (a||b). Using && will give result as true only of both expression are true and false otherwise whereas || will give false only if both expression are false and true otherwise. && corresponds to boolean logic AND || corresponds to boolean logic OR ! Corresponds to boolean logic NOT Is located on the left of operand. Inverses the value of operand Example (!(23==23)) is returned as false.

Conditional operator( ? : ) It evaluates an expression and returns a result. Condition ? (Return If True) : (Return If False) Example: (7>5)?10:20  Will return 20 because 7 is greater than 5. (7<5)?10:20 will return 20 because 7 < 5 is a false statement. (!(5==5))?1:2 will return 2 because 5 is equal to 5 but ! will change that true to false statement hence 2 is returned.

Comma Operator (,) Used when two or more expressions is included where only one is expected Example: x = (y=27, y+23); will first assign 27 to y and store 50 (y+23=27+23=50) to x.

Bitwise operators (&, |, ^, ~, >) Used only when working with bits of data on low level. Currently we are only working on bytes of data. & is bitwise AND | is bitwise OR ^ is bitwise XOR ~ is bitwise NOT << shift left (SHL) >> shift right (SHR)

Explicit type casting operators Type casting: converting a data type from one to the other. Example: int a; float b = 6.67; a = (int)b (or, a = int(b);) will have a = 6.

sizeof() Return size in bytes for a data type or variable itself. Example SizeOfChar = sizeof(char) will store 1 to variable SizeOfChar because the size of char data type is 1 bytes (we talked about it in previous videos)

Other?? There are other operators that we will go on later when we get to pointers and specific object oriented programming.

Precedence Order in which the operators are performed according to the priority. Priority order is defined for each operators. For basic leve: Level 1 is :: (called scope) Level 2: () or [] Level 3: ++, -- Level 4: Type casting (type) Level 5: * / % (Goes from left to right) Level 6: +, - (Left to right) Level 7:, = relational (left to right) Level 8: ==, != Level 9: bitwise operators Level 10: && Logical AND (left to right) Level 11: || Logical OR (left to right) Level 12: ?: conditional (right to left) Level 13: =, /= … Assignment operators (Right to left) Level 14: comma(,) left to right

Please rate, comment and subscribe Next Video: Basic input and output Ends the basics Then we will program, learn and implement all we learned in basics Please visit for more materials to learn  Quizzes, challenges, articles and news.