music |
OSdata.com |
blocks
summary
This subchapter looks at blocks.
free computer programming text book projecttable of contents
|
music |
OSdata.com |
This subchapter looks at blocks.
free computer programming text book projecttable of contents
|
This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.
Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.
This subchapter looks at blocks.
Most modern programming languages have ablock structure. That is, programs are grouped into blocks of code that function as a unit. This can be to provide a sequence of instructions in a location that calls for only one statement. This can also be used to control the scope of variables and other identifiers.
ALGOL was the first programming language to have a block structure. Blocks served both for visual organizational purposes and for the purpose of defining the scope of variables (local or global).
label begin declaration(s);
statement(s);
statement
end
The label names the block or program or statement and is optional.
The block starts at begin and ends at end.
The begin is followed by declarations orf variables, arrays, and procedures. The declarations end with a semicolon ( ; ).
Each statement in the block is separated by a trailing semicolon ( ; ) that is, a semicolon at the end of every statement except the last statement in the block.
There is no period after the end
Blocks can be nested:
label begin declaration(s);
statement(s);
begin declaration(s);
statement(s);
end;
statement(s);
statement
end
Note that the end for the inner block has a trailing semicolon. Only the final end for the outermost or main block has no trailing semicolon.
Stanford CS Education Library This [the following paragraph] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.
C uses curly braces ({}) to group multiple statements together. The statements execute in order. Some languages let you declare variables on any line (C++). Other languages insist that variables are declared only at the beginning of functions (Pascal). C takes the middle road -- variables may be declared within the body of a function, but they must follow a {. More modern languages like Java and C++ allow you to declare variables on any line, which is handy.
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 (;).
27 A block statement comprises a sequence of statements preceded by the declaration of local entities used by the statements. :Ada-Europes Ada Reference Manual: Introduction: Language Summary See legal information
Blocks of code are indicated by indentation in Python. The language does not use such denotations as a pair of braces {} or a begin/end pair.
if expression
statement
statement
elif expression
statement
statement
elif expression
statement
statement
else expression
statement
statement
statement
statement
Blocks of code may use any combination of tabs and space characters to indicate a block, but Python does an exact match. Because tabs can vary by operating system and even software program, it is strongly recommended to use the Python convention of four space characters for each level of indentation.
A block of code is terminated by ending its indentation.
A series of blocks can be terminated simultaneously by a change in indentation.
if expression
statement
statement
if
statement
statement
statement
statement
elif expression
statement
statement
elif expression
statement
statement
else expression
statement
statement
statement
statement
Notice that the use of indentation solves the problem of matching up else if and else statements when there are also nested ifs.
A block of code in Ruby is delimited by either a pair of braces ({}) or the pair do and end
ocean = [ "Atlantic", "Pacific", "Indian", "Artic" ]
ocean.each do |element|
puts element
end
This could alternatively be coded:
ocean = [ "Atlantic", "Pacific", "Indian", "Artic" ]
ocean.each { |element| puts element }
The braces have a higher priority than do/end.
As a non-programming aside, what are the only two nations in the world that have voasts on three oceans?
The answers are: Canada and the United States (both of which border the Atlantic, Pacific, and Artic Oceans).
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: November 14, 2010
Last Updated: March 16, 2011
return to table of contents
free downloadable college text book
previous page | next page |