CIS 103 Computer Programming Logic Quick Reference Guide

vetinnocentΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια)

178 εμφανίσεις

CIS 103


Computer Programming Logic
Quick

Reference Guide

This guide is NOT language specific

La
st Update:
Thursday, November 7, 2013








Identifier
s



An identifier is anything you provide a name for
such as



variables



named constants



modules

(aka functions, methods, procedures, etc)

R
ules for creating an identifier



can
not be a keyword

(in a programming language)



can use letters, digits, and some special characters



most common special character is
an underscore ( _ )



cannot contain any spaces



cannot begin with a digit (letter or
special character

okay)



identifiers are case
-
sensitive in some languages and
NOT case
-
sensitive in others

Naming Conventions



naming convention
for variables and modules
is to use

undercores
or camel casing
: houseKeeping( ) / finish_up( )

gross_pay, net_p
ay /
totalSales,
grandTotalSales



naming convention for
named constants

is to use
all uppercase
letters


and
underscores
: MAX_PERSONS
, SPEED_LIMIT





Variable
Declarations

Single
Declaration



format:

data
-
type variable



Examples:


num

quantity /
num

price /
string

name

Multiple Declaration


format: d
ata
-
t
ype variable, variable




Examples:


string

lastName, firstName, city, state, zip





num

x, y, z

Declaration and Initialization

data
-
type variable

=
initial
-
value


Examples:



num

count = 0



string


last
_n
ame = “Wilson”

num

count=0, total=0, x,y,z

string

fName, lName, college = “Cerritos”

bool


eof = F
alse

For languages that do not

require a variable
to
be declared before it is
used
,
(
s
uch as Python
and also for RAPTOR)
,

a simple assignment
statement
serves as the "declaration" of the variable.

Variable declarations in Python and RAPTOR
:

x = 3

// x is numeric (
integer
)

type

y = 2.5 // y is numeric
(floating
-
point) type

course = "CIS 103" //course is a
string

type

Arithmetic Operators

^,** Exponentiation

*


Multiplication

/


Division

(integer and/or floating
-
point)

%


Modulus (integer remainder)

+


Addition

(also used for concatenation
of strings)

-


Subtraction

Relational Operators

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to


=

Equal to

//equality operator

(often == is used)

!=

Not equal to

//inequality operator

Logical Operators

(highest
-
to
-
lowest priority)


not / and / or

How t
o evaluate
logical operators

1.

bind operators to operands (
precedence)

2.

evaluate left
-
to
-
right (associativity)


y = A or B and C or D


y =
(

(

A or
(
B and C
)

)

or D
)

Assignment Operator

= “is assigned”


Example: x = 3 //x “is assigned” 3

Compound Assignment
Operators

+=
-
= *= /= %=
LHS

<
operator
>
=
RHS



Example:
total
=

total
+

sales ~
total
+=

sales








D
ata Type Categories


num
eric
integer
or
floating
-
point
numbers





Examples:
-
2, 3.5, 17
,

0.379

string



any s
equence of characters in quotes

(single or double)




Examples: “Hello”, ‘CIS 103’
, “123.45”
,

"" (empty string)

boolean

true or false

(first letter may be capitalized


True/False)


Examples:

5 > 7
--
> false, 5 <= 7
--
> t
rue
, found = true;



Operator
Precedence


( )


----------------------------------

** or

^ (exponentiation)

----------------------------------

*


/


%

----------------------------------

+

-




Expressions

may contain arithmetic, relational


and logical operators. Be
low is the precedence


of these types of operators


arithmetic and string

concatenation

----------------------------------



relational

----------------------------------




logical


Operator
Associativity


l
eft
-

to
-

right:
-
most operators

(binary operators)

right
-

to
-

left:
unary and a
ssignment operator
s


CIS 103


Computer Programming Logic
Quick

Reference Guide

This guide is NOT language specific

La
st Update:
Thursday, November 7, 2013




E
xpressions and Statements

An
Expressio
n
is

usually part of a
statement
.


z = 5 * y + x

//this is a
statement

5 * y + x


//this is an
expression

print
(

"
Your total is " + total
)

//this is a
statement




"
Your total is " + total
//this is an
expression

An
expression

may be a
ny of the following and also com
binations
of the following:



variable
,
named constant
,
literal



string
expression



arithmetic

expression



relational
expression



logical
expression



call to a module that returns a value

A
programming language
statement

is
equivalent to



a

flowchart symbol



a

pseudocode statement



Selection and Loop Structures


Selection:



Unary or single selection



Binary or dual selection



Case structure
possible when branching on a variable



Simple selection



One
test



Compound selection



Multiple
test
s joined with AND / OR operators


Looping:


Pre
-
test

loop



Test
precedes

loop body



while
, for

Post
-
test

loop



Test
follows

loop body



do

.. while

Mid
-
test
loop



supported
directly

in some languages and
indirectly

in others



RAPTOR was designed using Ada which supports a mid
-
test
loop directly

Loop
Control:



3 types of expressions that are used to control loops:



initiali
ze



test



update



Counter
-
controlled

loops,

aka
definite

loops, work with a
loop control variable

(lcv)



Sentinel
-
controlled

loops,

aka
indefinite

loops, work with a
sentinel value


A sentinel value is a value
outside

of the range of valid data




Pseudocode forms of the if Statement (selection
structure)



Simple if



Example


if

test

then



if

x < y

then





statement(s)



x = x+1


endif






endif


if/else




Example


if

test

then



if

x < y

then




statement(s)



x = x+1


else




else




statement(s)



x = x
-
1


endif




endif


if/else if

(nested if)


Example


if

test

then



if

x < y
then




statement





x = x+1


else






else




if

test

then



if

x < z
then




statement(s)



x = x
-
1



else





else




statement(s)





y = y * 2



endif

endif


endif

endif


Note: some languages

(such as Python) include an
elif



("else if
") keyword. if


elif


else


endif

Pseudocode form of a

while

Loop ( pre
-
test loop )

Form:


Example:

init
ialize




x = 0

while

test




while

x < 10


statement(s)




sum += x


u
pdate




x

= x+
1

endwhile



endwhile


Pseudocode form of a
do
-
while

Loop ( post
-
test loop )

Form:



Example:

init
ialize



x = 0

do


do


statement
(s)





sum += x


update







x = x+
1

while

test





while

x < 10


Pseudocode form of a

for

Loop ( pre
-
test loop )


for

c
ount = m to n

[
step k
]


for

x
= 1 to 10


statement(s)



sum
= sum
+

x

en
dfor





endfor


The l
oop control variable starts at
m

and cont
inues until it is
greater than
n
. Range is

m
through
n

inclusive

[m,n]

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.



Pseudocode and languages

have "while", "for", and "do"
statements



Flowcharts

have pre
-

mid
-

and post
-
test
repetition
structures