cis_103/Language_Resources

advertisement
[Jack Wilson – Cerritos College]
CIS 103 Quick Reference Guide
Last Update: Sunday, February 7, 2016
Binary Operators [ have two operands ]
Assignment Operator
= “is assigned” Example: x = 3 //x “is assigned” 3
Compound Assignment Operators: +=, -=, *=, /=
Arithmetic Operators
+ Addition
Subtraction
* Multiplication
/ Division
integer division
floating-point division
%
4/6 --> 0, 6/4 --> 1
4/6 --> 0.666667
6/4 --> 1.5
Modulus (integer remainder) 6 % 4 --> 2
[ RAPTOR uses REM and also MOD ]
Relational Comparison Operators
<
Less than
<= Less than or equal to
>
Greater than
>= Greater than or equal to
Equality and Inequality Operators
= Equal to
[ == is used in C++ and Java ]
!= Not equal to
Data Type [ These are the data types we will use in class ]
Note:
RAPTOR only recognizes numbers and strings
int
an integer number
Examples: -2, 17, 0, 53
double
a floating-point number
Examples: -3.5, 17.25, 0.379
string
a sequence of 0 or more characters in double quotes (“ “)
Examples: “Hello”, “CIS 103”, “123.45”
boolean
either True or False
Expressions evaluate to a value of some data type.
The data type of a value determines:
the type of variable or named constant to which it can be assigned
the types of operations that can be performed using the value
Declaration
Usually a variable must be declared before it can be used. A declaration
specifies the data type and the identifier for a variable or named
constant.
Initialization
A variable or constant is initialized the first time it is assigned a value.
Logical Operators
and | or | not
Single Declaration
String Concatenation Operator [ + ]
“Hello “ + name | “Your gross pay is “ + grossPay
s4 = s1 + “ “ + s2 + “ “ + s3 [ s1-s4 are string variables ]
Precedence of Operators
Precedence of arithmetic operators:
( )
---------------------------------^, ** [RAPTOR]
---------------------------------*, /, %, [RAPTOR MOD]
---------------------------------+, Precedence of relational comparison operators:
All operators have the same precedence.
Precedence of logical operators:
In order from highest to lowest: not, and, or
To evaluate an expression with logical operators:
1. Establish the binding of operands to
operators
2. Evaluate the expression
Precedence for operators in an expression
(1) arithmetic
(2) relational
(3) logical
(4) assignment
Associativity of operators
L --> R left to right [ most operators ]
R --> L right to left [ assignment and unary operators ]
data-type variable
Examples: int quantity / double price / string name
Multiple Declaration
data-type variable, variable, … variable
Examples: string lastName, firstName, city, state, zip
double x, y, z | double sales, totalSales
Declaration and Initialization
Examples:
double
string
int
string
data-type variable = initial-value
total = 0
lastName = “Wilson”
count = 0, number = 0
fName, lName, college = “Cerritos”
Identifiers
An identifier is anything for which you provide a name, including:
variables, named constants and modules [ aka functions, methods,
procedures, etc. ]
Rules for creating an identifier:





can use letters, digits, and some special characters
o most common special character is an underscore [ _ ]
cannot contain any spaces
cannot begin with a digit, letter or underscore is okay
cannot be a keyword in a language or in pseudocode
identifiers may be case-sensitive [ language dependent ]
o RAPTOR is NOT case-sensitive
Identifier Naming Conventions:


use underscores [ _ ] or camel casing to separate words.
E.g. gross_pay, total_gross_pay | grossPay, totalGrossPay
use all capital letters in a named constant, e.g. FED_TAX_RATE
CIS 103 Quick Reference Guide
[Jack Wilson – Cerritos College]
Last Update: Sunday, February 7, 2016
Selection and Loop Control Structures
Pseudocode forms of the if Statement [ selection structure ]
Selection [ aka decision ]:
 Unary or single selection
 Binary or dual selection
 Case structure possible when branching on a variable
 Simple selection
 One condition
 Compound selection
 Multiple conditions joined with logical operators or
nested if statements
Simple if
Example
if condition then
statement(s)
endif
if x < y then
x = x+1
endif
------------------------------------------------------
------------------------------------------------------
Loop [ aka iteration and repetition ]:
Pre-test loop
 Test precedes loop body
 while
 for
Post-test loop
 Test follows loop body
 do-while
------------------------------------------------------
if/else
Example
if condition then
statement(s)
else
statement(s)
endif
if x < y then
x = x+1
else
x = x-1
endif
if/else if (nested if)
Example
if condition then
statement;
else
if condition then
statement(s)
else
statement(s)
endif
endif
if x < y then
x = x+1
else
if x < z then
x = x-1
else
y = y * 2
endif
endif
Mid-test loop
 supported directly in some languages and indirectly in
others
 RAPTOR was designed using Ada which supports a mid-test
loop directly
Note: some languages (such as Python) include an elif ("else
if") keyword. if – elif – else – endif
Form:
Example:
Loop Control:
3 types of expressions that are used to control loops:
 initialize
 test
 update
initialize
x = 0, sum = 0
while condition [test]
statement(s)
update
endwhile
while x < 10
sum += x
x = x + 1
endwhile
 Counter-controlled loop [ aka definite loop ]
works with a loop control variable (lcv)
 Sentinel-controlled loop [ aka indefinite loop ]
works with a sentinel value (preselected value)
A sentinel value is a value outside of the range of valid data.
Expressions
An expression may be any of the following and also
combinations of the following:
 variable
x, y, sum
 named constant
PI, MAX_PERSONS
 literal
3, .5, “ok”, true
 string expression
“Hello” + name
 arithmetic expression
x * y
 relational expression
x < y
 logical expression
x < y and y >= z

call to a module that returns a value
square(x)
In a selection or loop control structure, the test expression is usually
called the condition. The structure will execute if the condition
evaluates to true.
An expression is a component of most statements.
Statement: sum = num1 + num2 [ num1 + num2 is an expression ]
Pseudocode form of a while Loop [ pre-test loop ]
------------------------------------------------------
Pseudocode form of a do-while Loop [ post-test loop ]
Form:
Example:
initialize;
x = 5, sum = 0
do
do
sum += x
x = x - 1
while x >= 1
statement
update
while condition [test]
Pseudocode form of a for Loop [ pre-test loop ]
for count = i to j [step k]
statement(s)
endfor
for x = 1 to 10
sum += x
endfor
The loop control variable starts at i and continues until it is greater
than j. Note: i to j means i through j inclusive [i,j]
The step is the amount added to or subtracted from the loop control
variable after each iteration.
The step is optional. It defaults to +1 if not specified.
Statement [ a single operation to be executed ]


one flowchart symbol in RAPTOR
one pseudocode statement
Download