1.3 Comments and whitespace (All Questions)
multi-line comment
starts with /* and ends with */, where all text between /* and */ is part of the comment. A multi-line comment is also known as a block comment.
single-line comment
starts with // and includes all the following text on that line. Single-line comments commonly appear after a statement on the same line.
How many lines of code will the compiler ignore in the code below? int userAge; int currentDecade; int nextDecade; int nextMilestone; // FIXME: Get user age userAge = 29; // Testing with 29 currentDecade = userAge / 10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10;
3
How many spaces will the compiler ignore in the code below? numToBuy = numNeeded - numStock + 2;
6
Are the specified lines of code good or bad uses of whitespace? #include <iostream> using namespace std; int main () { int userAge; int currentDecade; int nextDecade; int nextMilestone; cout << "Enter your age: " << "\n"; cin >> userAge; currentDecade=userAge/10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; cout << "Next big birthday is at " << nextMilestone << "\n"; return 0; } Good or Bad? currentDecade=userAge/10;
Bad
Are the specified lines of code good or bad uses of whitespace? #include <iostream> using namespace std; int main () { int userAge; int currentDecade; int nextDecade; int nextMilestone; cout << "Enter your age: " << "\n"; cin >> userAge; currentDecade=userAge/10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; cout << "Next big birthday is at " << nextMilestone << "\n"; return 0; } Good or Bad? int nextDecade;
Bad
Are the specified lines of code good or bad uses of whitespace? #include <iostream> using namespace std; int main () { int userAge; int currentDecade; int nextDecade; int nextMilestone; cout << "Enter your age: " << "\n"; cin >> userAge; currentDecade=userAge/10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; cout << "Next big birthday is at " << nextMilestone << "\n"; return 0; } Good or Bad? nextMilestone = nextDecade * 10;
Bad
True or False? Spaces are always ignored by the compiler.
False
Are the specified lines of code good or bad uses of whitespace? #include <iostream> using namespace std; int main () { int userAge; int currentDecade; int nextDecade; int nextMilestone; cout << "Enter your age: " << "\n"; cin >> userAge; currentDecade=userAge/10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; cout << "Next big birthday is at " << nextMilestone << "\n"; return 0; } Good or Bad? nextDecade = currentDecade + 1;
Good
/* numKids = 2; /* Typical number */ numCars = 5; */
Invalid
Indicate which are valid code. // Print "Hello" Then print "Goodbye" And finally return. //
Invalid
Indicate which are valid code. /* * Author: Michaelangelo * Date: 2014 * Address: 111 Main St, Pacific Ocean */
Valid
Indicate which are valid code. /* numKids = 2; // Typical number numCars = 5; */
Valid
Indicate which are valid code. /* Determine width and height. calculate volume, and return volume squared. */
Valid
Indicate which are valid code. /* Get user input */
Valid
Indicate which are valid code. // Get user input
Valid
Indicate which are valid code. // Print "Hello" to the screen //
Valid
Indicate which are valid code. // numKids = 2; // Typical number
Valid
Whitespace
blank spaces (space and tab characters) between items within a statement and blank lines between statements (called newlines). A compiler ignores most whitespace.
Good practice
is to deliberately and consistently use whitespace to make a program more readable
comment
text a programmer adds to code, to be read by humans to better understand the code but ignored by the compiler