How to Code in Pascal

How to Code in Pascal thumbnail
Pascal translates your code into a format a computer can understand.

Named for a French mathematician and early trailblazer in computer development, Pascal is a structured programming language created in 1970 as an improvement upon earlier, high-level languages. Due to its clear organization and syntax, Pascal soared in popularity as a software development and teaching tool in the 1980s. Variations of the Pascal language were created for commercial development, and it is still being put to impressive and large-scale uses today -- including a major video chat client. Knowledge of Pascal will provide you small-scale development abilities and a comfortable transition into your next programming language.

Instructions

    • 1

      Use a consistent, logical naming scheme. Separate multiple words in a name with an underscore, and do not use capitalization when naming. Use nouns when naming variables, types and constants, and name functions and procedures using verb phrases. Use “total_money” as a variable name, for example, but never to name the function that adds the money; call the function “total_the_money” to indicate a clear action. Indicate types by adding “_type” at the end of the name.

    • 2

      Order your code for ideal readability and proper functioning. Declare simple constants and types first, in that order, followed by variables. List the headings “type” and “var” only once -- each followed by the identifiers within the category; “const” may be used twice to differentiate between simple and structured constants. Organize identifiers within each particular group alphabetically or in another logical order with one identifier per line. Align the equal signs when declaring constants and types, as well as the colons when declaring variables.

    • 3

      Enclose your executable code within the words “begin” and “end.” Place each on its own line of code, and punctuate the latter with a period when it appears at the end of your code. Punctuate each statement within the program with a semicolon.

    • 4

      Use the “write” or “writeln” statement to display text to the user. Enclose desired output text within parentheses and single quotation marks immediately after the command. Use “readln,” on the other hand, to receive a value input by the user and return it back to the program. Follow this with the name of the variable to which the input will be assigned, notated within parentheses. Use “readkey,” alternately, to acknowledge a pressed key from the user and continue with the program. No variable must be indicated.

    • 5

      Use “if” statements to separate code that should be executed only under certain conditions. Indicate “if” followed by a condition -- such as “money > 100” -- and the word “then.” Write the code that is to be executed, should such conditions exist, on the following line. Enclose multi-line code within “begin” and “end” -- following the “if” clause. Include the line “else” on its own line, followed by executable code for the program to follow a second course of action in the event that the condition of the “if” statement is not met.

    • 6

      Use loops to repeat the same series of events many times. Incorporate a “while” loop to repeat the given code for as long as the specified condition remains true; if it is false from the outset, the loop may be skipped. Use a “repeat” loop to specify the condition upon which to stop the loop. Be sure that the variables laid out in the condition of either loop will change as the loop repeats, or your code will result in an infinite loop. Specify a fixed number of repetitions by using a “for” loop.

    • 7

      Add comments within your executable code. Enclose comments within curly brackets to differentiate them from code at run time. Use these to document your code as you create it for later reference. Type comments in sentence case, aligned to a particular column beyond the code indentation or at a consistent six spaces past the current code. Leave a blank line before and after comments, to separate them visually from code.

    • 8

      Customize the appearance of your program. Position output at a particular location by typing “gotoxy” followed by x and y coordinates in parentheses separated by a comma. Use “textbackground,” followed by a color in parentheses, to change the screen's background color. Represent the color with a word or an integer; both “red” and “5” are viable options. Indicate “textcolor” in the same way.

Related Searches:

References

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images

Comments

Related Ads

Featured