Question
Good use of whitespace. #include using namespace std; int main() { int myFirstVar; // Aligned comments yield less int yetAnotherVar; // visual clutter int thirdVar;
Good use of whitespace.
#include using namespace std;
int main() {
int myFirstVar; //
Aligned comments yield less int yetAnotherVar; // visual clutter int thirdVar;
// Above blank line separates variable declarations from the rest cout << "Enter a number: "; cin >> myFirstVar; // Note >> is under <<. Less visual clutter. // Above blank line separates user input statements from the rest yetAnotherVar = myFirstVar; // Aligned = operators thirdVar = yetAnotherVar + 1; // Also notice the single-space on left and right of + and = // (except when aligning the second = with the first =) cout << "Final value is " << thirdVar << endl; // Single-space on each side of << return 0; // The above blank line separates the return from the rest.
Figure 2.3.3: Bad use of whitespace.
#includeusing namespace std; int main() { int numPeople; int totalOuncesPasta; cout<<"Enter number of people: ";cin>>numPeople; totalOuncesPasta = numPeople * 3; cout << "Cook " << totalOuncesPasta << " ounces of pasta." << endl; return 0;}
Feedback?
PARTICIPATION
ACTIVITY
2.3.2: Whitespace.
Are the specified lines of code good or bad uses of whitespace?
#includeusing namespace std; int main() { int userAge; int currentDecade; int nextDecade; int nextMilestone; cout << "Enter your age: " << endl; cin >> userAge; currentDecade=userAge/10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; cout << "Next big birthday is at " << nextMilestone << endl; return 0; }
1)
int nextDecade;
Good
Bad
2)
currentDecade=userAge/10;
Good
Bad
3)
nextDecade = currentDecade + 1;
Good
Bad
4)
nextMilestone = nextDecade * 10;
Good
Bad
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started