1 / 9

Program style and Form

Program style and Form. Original Source : http://www.ftsm.ukm.my/zma/TK1914/05-Algorithms and Problem Solving.ppt. This statement is syntactically incorrect. USE OF WHITESPACE.

luce
Download Presentation

Program style and Form

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program style and Form Original Source : http://www.ftsm.ukm.my/zma/TK1914/05-Algorithms and Problem Solving.ppt

  2. This statement is syntactically incorrect. USE OF WHITESPACE • Insert white space characters (such as blanks, tabs and newlines) if necessary to increase the readability of your source code. Example: int matrix[][3] = {1, 0, 0, 0, 1, 0, 0, 0, 1}; int matrix[][3] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; • White space characters are ignored by the compiler during compilation. • Remember to separate reserved words and identifiers from each other and other symbols. Example: inta, b, c;

  3. COMMAS AND SEMICOLONS • Commas separate items in a list. Example: inta, b, c; • All C++ statements end with a semicolon. Example: area = length * width; • Semicolon is also called a statement terminator.

  4. DOCUMENTATION • Programs are easier to read and maintain if they are well-documented. • Comments can be used to document code • Single line comments begin with // anywhere in the line • Multiple line comments are enclosed between /* and */

  5. DOCUMENTATION • Avoid putting in useless comments such as shown below: int main() { … min = elapsed_time / 60; // assign elapsed_time / 60 to min sec = elapsed_time % 60; // assign elapsed_time % 60 to sec hr = min / 60; // assign min / 60 to hr min = min % 60; // assign min % 60 to min … }

  6. DOCUMENTATION • The program comments below are more useful: int main() { … // Convert elapsed_time to min:sec min = elapsed_time / 60; sec = elapsed_time % 60; // Convert min:sec to hr:min:sec hr = min / 60; min = min % 60; … }

  7. DOCUMENTATION • Name identifiers with meaningful names. • For example, which of the statements below is more meaningful? a = l * w; area = length * width;

  8. Form and Style • Consider two ways of declaring variables: • Method 1 int feet, inch; double x, y; • Method 2 int a,b;double x,y; • Both are correct, however, the second is hard to read

  9. YOU SHOULD NOW KNOW… • importance of program readability • using whitespace characters • inserting comments • using meaningful names for identifiers

More Related