C++ and Algorithmic Thinking for the Complete Beginner: Learn to Think Like a Programmer (Part 1 of 5) by Aristides Bouras & Loukia Ainarozidou
Author:Aristides Bouras & Loukia Ainarozidou [Bouras, Aristides]
Language: eng
Format: epub
Published: 2016-02-16T22:00:00+00:00
//This is equivalent to a = a + 1
b = a;
cout << a << endl;
cout << b << endl;
and another one with a post-incrementing operator.
a = 5;
a++;
//This is equivalent to a = a + 1
b = a;
cout << a << endl;
cout << b << endl;
In both examples a value of 6 is assigned to variable b! So, where is the catch? Are
these two examples equivalent? The answer is “yes,” but only in these two examples.
In other cases the answer will likely be “no.” There is a small difference between
them.
Let’s spot that difference! The rule is that a pre-increment/decrement operator
performs the increment/decrement operation first and then delivers the new value.
Chapter 7
Operators
79
A post-increment/decrement operator delivers the old value first and then performs
the increment/decrement operation. Look carefully at the next two examples.
a = 5;
b = ++a;
cout << a << endl;
//Outputs: 6
cout << b << endl;
//Outputs: 6
and
a = 5;
b = a++;
cout << a << endl;
//Outputs: 6
cout << b << endl;
//Outputs: 5
In the first example, variable a is incremented by one and then its new value is
assigned to variable b. In the end, both variables contain a value of 6.
In the second example, the value 5 of variable a is assigned to variable b, and then
variable a is incremented by one. In the end, variable a contains a value of 6 but
variable b contains a value of 5!
Notice: The double slashes after the cout statements indicate that the text that
follows is a comment; thus, it is never executed.
7.6
String Operators
There are two operators that can be used to concatenate (join) strings.
Operator Description
Example
Equivalent to
+
Concatenation
a = "Hi" + " there";
+=
Concatenation assignment
a += "Hello";
a = a + "Hello";
Notice: Joining two separate strings into a single one is called “concatenation.”
The following example displays “What’s up, dude?”
#include <iostream>
using namespace std;
int main() {
string a, b, c;
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12112)
Hello! Python by Anthony Briggs(9520)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9488)
The Mikado Method by Ola Ellnestam Daniel Brolund(9439)
Dependency Injection in .NET by Mark Seemann(8995)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7981)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7515)
Grails in Action by Glen Smith Peter Ledbrook(7412)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7239)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6109)
Kotlin in Action by Dmitry Jemerov(4764)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(4460)
Microservices with Go by Alexander Shuiskov(4183)
Practical Design Patterns for Java Developers by Miroslav Wengner(4100)
Test Automation Engineering Handbook by Manikandan Sambamurthy(4069)
Practical Vim (for Kathryn Amaral) by Drew Neil(3805)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3683)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3681)
Learn SOLIDWORKS 2020 by Tayseer Almattar(3577)