Previous page Next page Navigation bar

Calculator Programming Tutorial

Programming Building Blocks I

Comments and White Space

The single most important construct in programming is the comment. A comment is a remark by the programmer, encoded into the source code of the program. The next most important construct is white space. White space is the absence of text: this includes blank lines, indentation of lines, spaces surrounding operators in the interior of lines, and so forth. Generally, both of these may appear between textual elements in the program, but not inside them.

In compiled languages, white space and comments are entirely “free.” Both are removed by an early stage of the compiler, and have no effect on the final generated code. In many interpreted languages, the source code as written by the programmer is encoded into a “token stream,” which is then interpreted. In these languages, too, comments and white space are essentially “free.” In the calculator, as with other directly interpreted languages, the source code as written by the programmer is the program interpreted. Comments in these directly compiled languages therefore consume both program space (to hold the comment) and execution time (to skip over the comment). Also, in the calculator, white space is a syntax error.

Visual Basic for Applications and most JavaScript implementations are token stream interpreters. Comments and white space do not increase execution time in these languages. The calculator, on the other hand, is directly interpreted. Comments in the calculator consume both program space and execution time.

JavaScript Comments

In JavaScript, there are two forms of comments: block comments, which start with the token /* (slash asterisk) and end with the token */ (asterisk slash); and line comments, which start with the token // (slash slash). Block comments include all text between the comment start token and the comment end token. This text may be part of a line, several lines, or even entire pages of text. Line comments, on the other hand, start with their start token, and end with the end of the text line on which they are placed. In JavaScript, block comments are traditionally used to introduce a section of program text, while line comment are traditionally used to comment a single statement or statement fragment.

/*
 *  This is a JavaScript block comment.
 *  Placement of the comment tokens on separate lines, and the use of
 *  a vertical line of asterisks, is a convention that helps make the
 *  comment more noticeable.
 */
 
var x;                  //  This is a JavaScript line comment.

Note the use of white space: there is a blank line between the block comment and the declaration of x, and there is horizontal space between the statement and its line comment.

Visual Basic for Applications Comments

In VBA, there are two forms of comments: statement comments, and line comments. Statement comments start with the keyword REM, and extend until the next line end that is not preceded with the line continuation character. Line comments start with the character ' (apostrophe) and extend until the next line. In VBA, REM statements are rarely used; almost all comments are line comments.

'   This is a block comment in VBA.
'   Vertical alignment of the comment text one tab stop after the
'   appostrophe starting the comment line is a convention that helps
'   make the code look neat.
 
Dim x As Variant        ' This is a VBA line comment.

Again, note the use of white space.

Calculator Comments

The calculator, too, has line comments. These line comment start with the character ' (apostrophe) and extend until the next colon or new line statement separator. Putting the name of the program as a comment on the first line was traditional with previous types of Casio calculators. This was useful as programs were not named, but the first line of program text was displayed in the listing of available programs. With the Casio CFX-9850G and FX-7400G, line comments no longer serve to label programs, and their use is not traditional. Instead, comments are placed as explanatory material on program listings, but not actually encoded with the program. These “non-program” comments are generally considered to start with the character ; (semicolon) and continue to the end of the line.

;   This is a block comment in Casio program listings.
;   Vertical alignment of the comment text one tab stop after the
;   semicolon starting the comment line is a convention that helps
;   make the code look neat.
 
If A>B                  ; This is a listing line comment

Again, note the use of white space. Also note that this white space is used only in the program listing, not in the program itself. The calculator’s programming language regards blank lines and embedded spaces as syntax errors.

White Space

Most languages are “free form,” and are completely insensitive to white space between language elements. The presence or absence of spaces, horizontal tabs, and the end of text lines between language elements make no difference to the syntax or semantics of the programs. As it happens, however, all of Visual Basic for Applications, JavaScript, and the calculators are sensitive to the end of the line containing program text. In VBA, the end of the text line indicates the end of the statement. In order to continue a statement to the next text line, an underscore (_) character must be the last character of a line. Thus, a long statement such as:

IsAlphaNum = _
        ((strChar >= "a") And (strChar <= "z")) Or _
        ((strChar >= "A") And (strChar <= "Z")) Or _
        ((strChar >= "0") And (strChar <= "9"))

can be broken up into several lines, as long as all lines other than the last end with the underscore character.

JavaScript statements end with a semicolon (;). However, the semicolon ending JavaScript statements is in fact optional. If the end of a text line occurs at a place where the statement can end, the end of the statement is assumed. Thus, the factorial function can be written without statement ending semicolons as:

function Factorial (x) { var f = 1
    for (var i = 1; i <= x; i ++) f = f * i
    return f }

Note the absence of semicolons ending the statements. Neither of the two line ends in the body of the function may be deleted, although they may be replaced by semicolons.

In the calculators, the end of a line of program text indicates the end of the statement. There is no way to continue a statement across multiple lines.

[ Previous page | Top of page | Next page ]

Previous page Top of page Next page Navigation bar

Copyright © 2001 Brian Hetrick
Page last updated 30 December 2001.

Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Comments

Data Types

Numbers

Variables

Expressions

Control Flow I

Control Flow II

Subprograms

Basic I/O

Algorithms

A First Program

Examples

Exercises

Answers

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information

Your Privacy

Site Map

E-mail

Site Technical Data