2.3 Comments and whitespace

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A compiler scans code line-by-line, left-to-right; whitespace is mostly irrelevant.

1. The compiler converts a high level program into an executable program using Java bytecode. 2. Comments do not generate machine code. 3. The compiler recognizes end of statement by semicolon ";"

How many lines 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 The compiler ignores the 2 blank lines and the line that only contains a comment.

How many spaces will the compiler ignore in the code below? numToBuy = numNeeded - numInStock + 2;

6 The spaces before and after the =, -, and + are ignored by the compiler.

currentDecade=userAge/10;

Bad Spaces before and after the = and / operators would make the statement more readable.

nextMilestone = nextDecade * 10;

Bad The statement is not indented the same number of spaces as the other related lines of code.

Are the specified lines of code good or bad uses of whitespace? (Refer to the image) int nextDecade;

Bad The whitespace between int and nextDecade is unnecessary and yields less readable code.

Spaces are always ignored by the compiler.

False Not all spaces are ignored by the compiler. Ex: The spaces in a string literal like "Enter age: " will be printed if the string is output.

nextDecade = currentDecade + 1;

Good The statement is indented the same amount as the preceding lines, and a space is used before and after the = and + operators.

/* numKids = 2; /* Typical number */ numCars = 5; */

Invalid The first /* starts a multi-line comment. That comment ENDS at the */ on the SECOND line. The compiler will compile numCars = 5, then generate an error when reaching a */ that isn't ending a comment. Programmers use // when possible in part so that /* ... */ can be used to "comment out" code.

// Print "Hello" Then print "Goodbye" And finally return. //

Invalid The first line's // only applies to the remainder of the line. The second and third lines would yield compiler errors.

JavaDoc comments

Java supports a third type of comment, known as a JavaDoc comment (discussed elsewhere), which is a specially formatted multi-line comment that can be converted to program documentation in HTML.

/* Get user input */

Valid A multi-line comment is allowed on a single line, but good practice is to use // for single-line comments, reserving /* */ for multi-line comments.

// Get user input

Valid A standard single-line comment

/* Determine width and height, calculate volume, and return volume squared. */

Valid A typical multi-line comment.

/* * Author: Michelangelo * Date: 2014 * Address: 111 Main St, Pacific Ocean */

Valid Programmers commonly use single asterisks to make clear which lines are contained in the comment. Those single asterisks have no special meaning; they are just text within the /* ... */.

// numKids = 2; // Typical number

Valid The entire line after the first // is ignored. The numKids = 2 statement won't execute, but the code is valid. (Perhaps the programmer intentionally commented out the code for the moment).

/* numKids = 2; // Typical number numCars = 5; */

Valid The programmer has likely temporarily "commented out" those two statements. The // within the /* .. */ will be ignored, like the other text.

// Print "Hello" to the screen //

Valid The second // is ignored just like other text after the first //.

Whitespace

refers to 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. Programmers usually follow conventions defined by their company, team, instructor, etc., such as: ---Use blank lines to separate conceptually distinct statements. ---Indent lines the same amount. ---Align items to reduce visual clutter. Use a single space before and after any operators like =, ---+, *, or / to make statements more readable.

A comment

is text a programmer adds to code, to be read by humans to better understand the code but ignored by the compiler. Two common kinds of comments exist: single-line comment multi-line comment

A 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.

A 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.


Ensembles d'études connexes

Ch. 3: Program Planning Models in Health Promotion

View Set

Ch. 21 - Ethnicity - Sherpath, Evolve, EOC, chapter 42: death and loss, Chapter 29, 230 Final Exam Review

View Set

LUOA girls health and P.E. module 6 Test

View Set

Ch 15 Mini Sim on Accounting and Accounting Information

View Set

Angle Pairs (Vertical, linear pair, adjacent, complementary & supplementary)

View Set

CompTIA Linux+ 4.5 Important Directories and Their Contents

View Set

Ch.13 Viruses Viroids and Prions

View Set

AP Bio - Previous Quiz/Test Questions

View Set