Lesson 4: C++ Operators PDF
Document Details
Uploaded by TrustyLarch
Don Honorio Ventura State University
Ms. Angelica S. Dagal
Tags
Summary
These notes on C++ operators provide explanations and examples for various operators in C++. The document covers assignment, compound, relational, logical, and conditional ternary operators.
Full Transcript
Ms. Angelica S. Dagal Lesson 4: C++ Operators COMPROG 122 – Introduction to Programming (C++) Lesson Objectives: After completing the module, the students are expected to: ❖ Understand the input/output functions of C++...
Ms. Angelica S. Dagal Lesson 4: C++ Operators COMPROG 122 – Introduction to Programming (C++) Lesson Objectives: After completing the module, the students are expected to: ❖ Understand the input/output functions of C++ COMPROG 122 – Introduction to Programming (C++) programming language ❖ Familiarize with the terms used in C++ programming language ❖ Able to construct the basic structure of C++ ❖ Able to write codes using C++ programming language in Dev C++ IDE What is Operator? An operator is a symbol that tells the compiler to perform specific mathematical or COMPROG 122 – Introduction to Programming (C++) logical manipulations. These are special type of functions, that takes one or more arguments and produces a new value. Operators are used to perform various operations on variables and constants. C++ is rich in built-in operators and provide several types of operators. Assignment Operator (=) Operates '=' is used for assignment, it takes COMPROG 122 – Introduction to Programming (C++) the right-hand side (called rvalue) and copy it into the left-hand side (called lvalue). Assignment operator is the only operator which can be overloaded but cannot be inherited. The assignment operator assigns a value to a variable. Int a = 10; Assignment Operator (=) For example, let's have a look at the following code - I have included the evolution of the content COMPROG 122 – Introduction to Programming (C++) stored in the variables as comments: Compound Assignment Operators (+=, -=, *=, /=, %=, >>=, =, =, , < , >=, greater than = greater than or equal to Relational and Comparison Operators Sample Code: COMPROG 122 – Introduction to Programming (C++) Output: Logical Operators (!, && , ||) The operator ! is the C++ operator for the COMPROG 122 – Introduction to Programming (C++) Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. The logical operators && and || are used when evaluating two expressions to obtain a single relational result. Logical Operators (!, && , ||) The operator && corresponds to the Boolean COMPROG 122 – Introduction to Programming (C++) logical operation AND, which yields true if both its operands are true, and false otherwise. The operator || corresponds to the Boolean logical operation OR, which yields true if either of its operands is true, thus being false only when both operands are false. Logical Operators (!, && , ||) && OPERATOR (AND) A B A && B COMPROG 122 – Introduction to Programming (C++) true true true true false false false true false false false false || OPERATOR (OR) A B A || B true true true true false true false true true false false false Logical Operators (!, && , ||) Sample Code: COMPROG 122 – Introduction to Programming (C++) Output: Conditional Ternary Operator (?) The conditional operator evaluates an COMPROG 122 – Introduction to Programming (C++) expression, returning one value if that expression evaluates to true, and a different one if the expression evaluates as false. Its syntax is: condition? result1: result2 If condition is true, the entire expression evaluates to result1, and otherwise to result2 Sample Code: Operator (?) Conditional Ternary COMPROG 122 – Introduction to Programming (C++) Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. COMPROG 122 – Introduction to Programming (C++) When the set of expressions has to be evaluated for a value, only the right- most expression is considered. Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions COMPROG 122 – Introduction to Programming (C++) has to be evaluated for a value, only the right- most expression is considered. Sample Code: Output: Bitwise operators ( &, |, ^, ~, ) Bitwise operators modify variables COMPROG 122 – Introduction to Programming (C++) considering the bit patterns that represent the values they store. OPERATOR ASM EQUIVALENT DESCRIPTION & AND Bitwise AND Bitwise inclusive | OR OR Bitwise exclusive ^ XOR OR Unary complement ~ NOT (bit inversion) > SHR Shift bits right Sizeof Operator sizeof is a compile-time operator used to COMPROG 122 – Introduction to Programming (C++) calculate the size of data type or variable. sizeof operator will return the size in integer format. sizeof is a keyword. sizeof operator can be nested. Sample Code: Sizeof Operator Output: COMPROG 122 – Introduction to Programming (C++) Resources (Educational Materials): url: https://goalkicker.com/CPlusPlusBook/ https://www.w3schools.com/cpp/cpp_getstarted.asp COMPROG 122 – Introduction to Programming (C++) http://www.cplusplus.com/info/description/ https://www.programiz.com/cpp-programming https://www.studytonight.com/cpp/introduction-to- cpp.php https://docs.oracle.com/cd/E19957-01/806- 3569/Standard.html https://docs.microsoft.com/en-us/cpp/standard-library/iostream?view =vs-2019 https://www.softwaretestinghelp.com/iomanip-functions-in-cpp/ videos: https://www.youtube.com/watch?v=vLnPwxZdW4Y https://www.youtube.com/watch?v=_bYFu9mBnr4 https://www.youtube.com/watch?v=JBjjnqG0BP8 References: Goodrich, Michael et.al (2011). Data Structures and Algorithms in C++. John Wiley & Sons Inc, United States of America COMPROG 122 – Introduction to Programming (C++) Educational Technology Journals. http://www.educational-software-directory.net/publications/journals CPlusPlusNotesForProfessionals. http://www.GoalKicker.com C++ Tutorials. http://www.programiz.com C++ Tutorials. http://www.w3schools.in