Expressions and Operators in C++ PDF
Document Details
Uploaded by AmiablePreRaphaelites
Tags
Summary
This document provides an overview of expressions and operators in C++. It details different types of operators, including assignment, arithmetic, relational, and logical operators. Through examples and explanations, the document helps to understand how these operators function and are used in C++ programs.
Full Transcript
**Expressions and Operators in C++** **MODULE OVERVIEW** Welcome to this module! By the time that you are reading this, you have been immersed with the identifiers, variables and data types in C++ programming and started your adventurous journey in programming. Further, declaration and initializat...
**Expressions and Operators in C++** **MODULE OVERVIEW** Welcome to this module! By the time that you are reading this, you have been immersed with the identifiers, variables and data types in C++ programming and started your adventurous journey in programming. Further, declaration and initialization and assignment including scope and lifetime were also introduced to you in the previous module. However, the best way to learn to program is through writing programs directly. Thus, ready your tools and let's start a new knowledge to enjoy programming! **MODULE LEARNING OBJECTIVES** By the end of this module you should be able to: Familiarize themselves with the different uses of operators, and expressions in C++ programming. Apply the different functions of operators, and expressions in building C++ programs. **LEARNING CONTENTS (Assignment Operator (=))** **Introduction** An expression is a combination of literals, variables, operators, and explicit function calls (not shown above) that produce a single output value. When an expression is executed, each of the terms in the expression is evaluated until a single value remains (this process is called evaluation). That single value is the result of the expression. Here are some examples of different kinds of expressions, with comments indicating how they evaluate: 2 // 2 is a literal that evaluates to value 2 \"Hello world!\" // \"Hello world!\" is a literal that evaluates to text \"Hello world!\" x // x is a variable that evaluates to the value of x 2 + 3 // 2 + 3 uses operator + to evaluate to value 5 x = 2 + 3 // 2 + 3 evaluates to value 5, which is then assigned to variable x std::cout \6)), C++ evaluates first whether 5==5 is true, and if so, it never checks whether 3\>6 is true or not. This is known as *short-circuit evaluation*, and works like this for these operators: This is mostly important when the right-hand expression has side effects, such as altering values: ![](media/image20.png) Here, the combined conditional expression would increase i by one, but only if the condition on the left of && is true, because otherwise, the condition on the right-hand side (++i\