“You have to write a program once to know how you should have written it the first time.”
Short answer: You don’t. C++ is one part using what you know, and two parts looking up how to do the rest.
1.2 — Comments
Proper use of comments
Best practice
Comment your code liberally, and write your comments as if speaking to someone who has no idea what the code does. Don’t assume you’ll remember why you made specific choices.
Commenting out code
Converting one or more lines of code into a comment is called commenting out your code. This provides a convenient way to (temporarily) exclude parts of your code from being included in your compiled program.
Tip
If you always use single line comments for your normal comments, then you can always use multi-line comments to comment out your code without conflict. If you use multi-line comments to document your code, then commenting-out code using comments can become more challenging.
If you do need to comment out a code block that contains multi-line comments, you can also consider using the #if 0
preprocessor directive, which we discuss in lesson 2.10 – Introduction to the preprocessor.
Summary
- At the library, program, or function level, use comments to describe what.
- Inside the library, program, or function, use comments to describe how.
- At the statement level, use comments to describe why.
1.3 — Introduction to objects and variables
1.7 — Keywords and naming identifiers
Identifier names that start with a capital letter are typically used for user-defined types.
1.8 — Whitespace and basic formatting
Whitespace is a term that refers to characters that are used for formatting purposes. In C++, this refers primarily to spaces, tabs, and newlines. The C++ compiler generally ignores whitespace, with a few minor exceptions (when processing text literals). For this reason, we say that C++ is a whitespace-independent language.
1.10 — Introduction to expressions
Statements are used when we want the program to perform an action.
Expressions are used when we want the program to calculate a value.
1.11 — Developing your first program
Best practice
New programmers often try to write an entire program all at once, and then get overwhelmed when it produces a lot of errors. A better strategy is to add one piece at a time, make sure it compiles, and test it. Then when you’re sure it’s working, move on to the next piece.
The preferred solution
#include <iostream>
// preferred version
int main()
{
std::cout << "Enter an integer: ";
int num{ };
std::cin >> num;
std::cout << "Double that number is: " << num * 2 << '\n'; // use an expression to multiply num * 2 at the point where we are going to print it
return 0;
}
This is the preferred solution of the bunch. When std::cout executes, the expression num * 2 will get evaluated, and the result will be double num‘s value. That value will get printed. The value in num itself will not be altered, so we can use it again later if we wish.
This version is our reference solution.
Author’s note
One more thing: You may be thinking, “C++ has so many rules and concepts. How do I remember all of this stuff?”.
Short answer: You don’t. C++ is one part using what you know, and two parts looking up how to do the rest.
As you read through this site for the first time, focus less on memorizing specifics, and more on understanding what’s possible. Then, when you have a need to implement something in a program you’re writing, you can come back here (or to a reference site) and refresh yourself on how to do so.
1.x — Chapter 1 summary and quiz
statement
function
syntax
Comments
Data
value
identifier
definition statement
instantiated
data type
integer
Copy assignment
Initialization
std::cout
std::endl
uninitialized variable
undefined behavior
keywords
literal constant
operation operands operator
Unary Binary Ternary
expression evaluation result
expression statement