3 Smooth operator –exploring C++ operators and conditions

 

This chapter covers

  • The role of arithmetical, unary, and assignment operators
  • Understanding conditional statements using if-else
  • Unraveling the logic behind logical operators and how to use them
  • Conducting various comparisons using the comparison operators
  • Taking a firsthand look into the new C++ 20 three-way-comparison operator
  • Understanding bitwise operators and their role.

In this chapter, we will delve deeper into the C++ language by learning about operators – special symbols that perform various operations within your code. You may have encountered them briefly in the previous chapter, but now we will explore them in greater detail with plenty of examples and exercises. We start with arithmetic operators and assignment operators. We then move to the basics of conditional statements, using if-else, which work well with logical and comparison operators. We will also introduce the brand-new C++20 three-way comparison operator, commonly known as the spaceship operator, which offers exciting capabilities for comparing objects within your code. By the end of this chapter, you will have a solid understanding of the role and use of operators in C++.

3.1 The C++ operators – your programs’ screws and hinges

3.2 Arithmetical operators for numerical and other operands

3.2.1 Precedence day – how C++ follows precedence rules

3.2.2 Divide and conquer - division operator good to know-hows

3.2.3 The % modus operator for integer division

3.3 Assignment operators – assign values to objects and variables

3.3.1 The anatomy of rvalues and lvalues

3.3.2 The promise to never break a promise: assignment and const

3.3.3 Unary operators - increment and decrement have never been easier

3.4 If-else statements – control your programs’ flow

3.4.1 Nested if statements – an ‘if’ statement within an ‘if’ statement

3.5 Setting conditions using logical operators

3.6 Setting conditions using comparison operators

3.6.1 Less than or greater than

3.6.2 Not all values were born equal: the == and != operators

3.6.3 Compare → trois - introduction to the three-way comparison

3.7 Bitwise operators – get into the bit (literally)

3.7.1 Why do we need to micro-manage bits?

3.7.2 Let's think for a bit

3.8 Final exercise – dive into a bit more complex code

3.9 Summary