music |
OSdata.com |
program statements
summary
The actual working part of a program is its collection of program statements.
free computer programming text book projecttable of contents
|
music |
OSdata.com |
The actual working part of a program is its collection of program statements.
free computer programming text book projecttable of contents
|
The actual working part of a program is its collection of program statements. Note that some languages dont actually have statements, but this is generally true for most languages that would be taught as a first programming language.
As mentioned earlier, old programming languages often are column based (from punched cards) and modern languages are usually free form.
In a column based language (such as COBOL and FORTRAN) program statements are often separated by individual cards (80 character lines), with a special continuation character for statements that take more than one card.
In a free form language (such as C and PHP) prorgam statements can be spread over many lines or several statements can be packed into a single line.
So how do you tell when a new statement starts in a free form language?
If the language uses a terminator, then the designated separator marks the end of every statement.
If the language uses a aeparator, then the separator is placed between each statement. Separators act like dividers between groups of papers.
Each approach has its own group of subtle but common coding errors. Adding extra separators or leaving out necessary terminators can either crash your compilation or possibly even change the meaning of the program.
Pascal uses the semicolon ( ; ) as a separator.
In our example program, you will notice a semicolon separator between the program header and the begin.
program SimpleProgram (output);
begin
writeln ('Hello World')
end.
Pascal uses the period ( . ) as a terminator.
In our example program, you will also notice a period terminator at the end of the program (after end).
Pascal uses the period end of line as a terminator.
Therefore, Python includes a continuation character to indicate that a statement continues to the next line. This is the backslash (\) as the last character on a line that does not contain a comment. A backslash as the last character in a comment does not make the statement continue to the next line.
The lines used to continue a statement are called continuation lines.
Python recognizes lines that still have an open parenthesis ((), bracket ([), or brace ({) and automatically continues the statement to the following line without the need for a continuation character.
Triple quoted strings also continue across physical lines.
Python can use the semicolon ( ; ) as a terminator. This is normally only used when there are multiple statements on a single line, but a programmer can optionally add the semicolon terminator to the end of almost all statements.
Rules regarding indentation only apply to the first physical line and do not apply to any continuation lines.
ALGOL uses the semicolon ( ; ) as a separator.
There is no punctuation after the last statement or after the final END.
This issue of terminators and separators become more clear with more than one statement. We will expand our example program to include a second statement.
program SimpleProgram (output);
begin
writeln ('Hello World');
writeln ('How are you doing?')
end.
The semicolon separator goes between the two writeln statements.
The portion of the program with the statements is called the statement part.
While we wont go into details about printing output, we will discuss a few additional simple options that may be useful in your first few programs.
Some programming languages have a new line character (or pair of characters). Some programming languages may have the new line implied by a statement. Some programming languages may use the old school combination of the carriage return and the line feed characters.
A carriage return is used to return to the beginning of a line. In some languages the use of repeated carriage returns will allow for overstrike. Overstrike can be used to underline or even to create simple graphics by filling in darkness with combinations of overprinted characters.
A line feed is used to advance to the next line. This is straight down movement, which would create a staircase look if used repeatedly with short pieces of outputted text.
The writeln statement adds a carriage return and line feed to the end of each printed line. This brings printing back to the beginning of the line and advances printing to the beginning of the next line.
The write statement simply places printed output whereever the cursor (screen) or printhead (printed output) already is.
program SimpleProgram (output);
begin
write ('Hello');
writeln (' World');
writeln ('How are you doing?')
end.
Notice that you need to have the extra space character (located before World) or the two words would be crammed together (HelloWorld).
Each language uses some character or characters to indicate the start and end of the text to be printed. What happens if you need to print the character used to mark the start and end of text?
Programming languages have an escape character that is used to signal to the compiler that you want to insert a special character into the actual text.
Pascal uses two consecutive apostrophes to indicate that an apostrophe is part of the text rather than a signal for the end of text.
program SimpleProgram (output);
begin
write ('Hello');
writeln (' World');
writeln ('What''s up?')
end.
There may be a few additional quirks that you need to know about to write simple programs.
Some Pascal compilers may insert a new line after receiving typed input. This is not part of the official Pascal standard, but may apply to you.
Some Pascal compilers may insert a new line at the end of a program. This is not part of the official Pascal standard, but may apply to you.
This [the following paragraph] is document #108 [Essential Perl] in the Stanford CS Education Library --see http://cslibrary.stanford.edu/108/. This document is free to be used, reproduced, or sold so long as this paragraph and the copyright are clearly. Copyright 2000-2002, Nick Parlante, nick.parlante@cs.stanford.edu.
Perls control syntax looks like Cs control syntax. Blocks of statements are surrounded by curly braces { }. Statements are terminated with semicolons (;).
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
return to table of contents
free downloadable college text book
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
previous page | next page |
Tweets by @osdata |
free computer programming text book projectBuilding a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming. If you like the idea of this project, Supporting the entire project: If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church) free downloadable college text book on computer programming. |
This web site handcrafted on Macintosh computers using Tom Benders Tex-Edit Plus and served using FreeBSD .
UNIX used as a generic term unless specifically used as a trademark (such as in the phrase UNIX certified). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2010, 2011 Milo
Created: October 30, 2010
Last Updated: March 22, 2011
return to table of contents
free downloadable college text book
previous page | next page |