You are on page 1of 7

# Computer Science Summary

## Abhinav Kohli Ms. Sajan Semester 1 2012 2013

Binary:
2^4| 2^3| 2^2| 2^1| | | | | | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 2^0 1 | 1 | 0 | 1 |

0 1 1 0

Instead of using the digits 0-9, we only use 0-1 (again, if we used anything larger it would be like multiplying 2*2^n and getting 2^n+1, which would not fit in the 2^n column. Therefore, it would shift you one column to the left. For example, "3" in binary cannot be put into one column. The first column we fill is the right-most column, which is 2^0, or 1. Since 3>1, we need to use an extra column to the left, and indicate it as "11" in binary (1*2^1) + (1*2^0).

Variables:
A variableDeclaration is one of: var id { ,id } [ :typeSpec] [:=initializingValue] (a) collectionDeclaration (b)

Put:

## put [ : fileNumber , ] putItem { , putItem } [ .. ]

put "" % Output null string then new line The put statement outputs each of the putItems. Usually, a new line is started in the output after the final putItem. If the optional dot-dot (..) is present, though, subsequent output will be continued on the current output line. With character graphics, the omission of dot-dot causes the remainder of the output line to be cleared to blanks.

Get:
A getStatement is: get [ : streamNumber , ] getItem { , getItem } var query : string get query : * % Entire line is input into query The get statement inputs each of the getItems. Ordinarily, the output comes from the keyboard. However, if the streamNumber is present, the input comes from the file specified by the stream number (see the open statement for details). Also, input can be redirected so it is taken from a file rather than the keyboard. Check the documentation on the environment for instructions on doing so.

Mathematical Operators:
Addition: + Subtraction: Multiplication: * Division: / or div 4 Exponents: ** eg 10 ** 4 is 10

For:
A forStatement is: for [decreasing] [id ] : first .. last [by increment] statementsAndDeclarations end for for i : 1 .. 10 put i end for

The statements and declarations in a for statement are repeatedly executed. In the first iteration, the identifier is assigned the value of first. With each additional iteration, the identifier increases by 1 (or by increment, if the by clause is present). The loop stops executing when adding 1 (or increment) to the identifier would cause the identifier to exceed last. first and last must be integer values (or else enumerated or char values). If you specify decreasing, then the identifier decreases by 1 (or byincrement) each time through. Increment must be a positive integer value. When the by clause is present, the for loop terminates as soon as the identifier would become greater than last, unlessdecreasing is present. If decreasing is present, the loop terminates when the identifier would become less than last.

If:
An ifStatement is: if trueFalseExpn then statementsAndDeclarations { elsif trueFalseExpn then statementsAndDeclarations } [ else statementsAndDeclarations ] end if if mark >= 50 then put "You pass" else put "You fail" end if

An if statement is used to choose among a set of statements (and declarations). One set (at most) is chosen and executed and then execution continues just beyond end if. The expressions (the trueFalseExpressions) following the keyword if and each elsif are checked one after the other until one of them is found to be true, in which case the statements (and declarations) following the corresponding then are executed. If none of these expressions evaluates to true, the statements following else are executed. If no else is present and none of the expressions are true, no statements are executed and execution continues following the end if.

Array:
array indexType { , indexType } of typeSpec var marks : array 1 .. 100 of int An array consists of a number of elements. The typeSpec gives the type of these elements. There is one element for each item in the (combinations of) range(s) of theindexType(s). In the following example, the array called marks consists of 100 elements, each of which is an integer.

Function:
A functionDeclaration is: function id [ ( [paramDeclaration {, paramDeclaration } ] ) ] : typeSpec statementsAndDeclarations end id function doubleIt ( x : real ) : real result 2.0 * x end doubleIt put doubleIt ( 5.3 ) % This outputs 10.6

A function declaration creates (but does not run) a new function. The name of the function ( id) is given in two places, just after function and just after end. The set of parameters declared with the function are called formal parameters. For example, in the doubleIt function, x is a formal parameter. A function is called (invoked) by a function call which consists of the function's name followed by the parenthesized list of actual parameters (if any). For example, doubleIt (5.3) is a call having 5.3 as an actual parameter. If there are no parameters and no parentheses, the call does not have parentheses. The keyword function can be abbreviated to fcn. See also functionCall and procedureDeclaration.

Procedure:
A procedureDeclaration is: procedure id [(paramDeclaration {, paramDeclaration })] statementsAndDeclarations end id

procedure greetings put "Hello world" end greetings greetings % This outputs Hello world

A procedure declaration creates (but does not run) a new procedure. The name of the procedure ( id) is given in two places, just after procedure and just after end.

Parameter:
A paramDeclaration is one of: [ var ] id {, id } : typeSpec (a) subprogramHeader (b)

procedure putTitle ( title : string ) % The parameter declaration is: title : string put title end putTitle procedure x (var s : array 1 .. * of string (*)) % Set each element of s to the null string for i : 1 .. upper ( s ) s ( i ) := "" end for end x A parameter declaration, which is part of the header of a procedure or function, specifies a formal parameter (see also procedure and function declarations). Form (a) above is the most common case. Form (b) specifies procedures and functions that are themselves passed as parameters.

Animations:
drawarc (x, y, xRadius, yRadius : int, initialAngle, finalAngle, Color : int) drawbox (x1, y1, x2, y2, Color : int) Draw.Cls drawdot (x, y, Color : int) drawfillarc (x, y, xRadius, yRadius : int, initialAngle, finalAngle, Color : int) drawfillbox (x1, y1, x2, y2, Color : int) drawfill (x, y : int, fillColor, borderColor : int) drawfilloval (x, y, xRadius, yRadius, Color : int) *Place animations in a for loop, change the location by utilizing the id of the for loop.

View.Set:
View.Set ( s : string ) Here are example uses of the View.Set procedure. In many cases, these will appear as the first statement of the program. However, they can appear any place in a program. View.Set ("graphics") % Switch to graphics mode View.Set ("screen") % Switch to screen mode View.Set ("nocursor") % Turn off cursor View.Set ("noecho") % Do not echo keystrokes The View.Set statement is used to change the mode of the screen, as well as the way in which Turing does input and output. The parameter to View.Set is a string, such as "graphics". The string contains one or more options separated by commas, such as "text, noecho". View.Set affects the active window.

View.Update:
View.Update The View.Update procedure updates a Run window from an offscreen bitmap. It is used with the command View.Set ("offscreenonly") which prevents the Run window from being updated until the View.Update command is given.

Getch:
getch ( var ch : string ( 1 ) ) The getch procedure is used to input a single character without waiting for the end of a line. The parameter ch is set to the next character in the keyboard buffer (the oldest not-yet-read character). This program contains a procedure called getKey which causes the program to wait until a key is pressed. setscreen ("graphics") procedure getKey var ch : string (1) getch (ch) end getKey for i : 1 .. 1000 put i : 4, " Pause till a key is pressed" getKey end for

Hasch:
hasch : Boolean The hasch procedure is used to determine if there is a character that has been typed but not yet been read. This program simulates the rolling of a pair of dice until the user pressed the keyboard.

put "Press any key to stop the dice rolling" var die1, die2 : int var ch : string (1) loop exit when hasch randint (die1, 1, 6) randint (die2, 1, 6) locate (1, 1) put "You rolled ", die1 + die2 end loop getch (ch) % Discard the character

The screen should be in a "graphics" mode. See the setscreen procedure for details. If the screen is not in "graphics" mode, it will automatically be set to "graphics" mode.

String Manipulation:
Declaration: vword : string (n) %n is number of chars in string String Functions: 1. Length of a String Length (word) returns int value 2. Concatenate Strings (join) Eg: word + 2013 3. Selecting part of a string Word (6 .. *) %end of word 4. Searching for pattern in string Index (string, pattern) Eg: index (getting, t) returns int value of the first encounter of the pattern Eg2: index (dandelion, lion)