CSC 1701B Computing: Science and Creativity
Outline Types Variables Operators Control: sequence, selection, repetition Functions (block headings will be given) Lists
Types Character Single character like A 5 or $ Digit 0 through 9 only Number Word 1 or more characters, no space A_word Cat Sentence One or more words with spaces
Variables and Scripts Names that can store values that change May be global (known everywhere) like this: Or a script variable known only in that script
Operators Some operators only allow digits or numbers + - x / mod round Some operators allow many types BTW, case does not matter Others allow a mix of types
Control Sequential one block call after another Selection if if…else Repetition repeat until for Selection and repetition can be nested You won’t need to write nested control on the Midterm
Functions Functions take in 0 or more inputs and return exactly 1 output The same inputs MUST yield same outputs Output function of input only Other rules of functions No state (prior history) No mutation (no variables get modified) No side effects (nothing else happens) CS Illustrated function metaphor
Domain The type “class” of input a function accepts Examples Sqrt of Positive numbers Length of Sentence, word, number _ < _ Both: Sentence, word, number _ and _ Booleans Letter _ of _ Number from 1 to input length Sentence, word, number Range All the possible return values of a function Examples Sqrt of Non-negative numbers or Nan Length of Non-negative integer _ < _ Boolean (true or false) _ and _ Boolean (true or false) Letter _ of _ Letter More Terminology (from Math)
Why use functions? (1) The power of generalization!
Why use functions? (2) Functions can be composed together to make even more magnificent things Functions are the building blocks of almost everything we create when programming Functional decomposition: the process of breaking big problems down into smaller tasks
Command No outputs Meant for side-effects Not a function… Not on Midterm Reporter (Function) Any type of output Predicate (Function) Only Boolean output (true or false) You will not need to write any block headings on the MidTerm Types of Blocks
You will be asked to complete blocks on the midterm, given the block heading Example: Complete this custom block to return the smaller of two values: write the text, use ( and ) if (a < b) set smaller to a else set smaller to b report smaller Writing Snap! On Paper
would be written as ((a > b) and (a < 999)) Practice test question shows how to write Snap! Writing Snap! On Paper
We often need to store a collection of items Snap! has the list block Found under Variables The following list has 3 elements It is considered an anonymous list as it has no nameno name Lists 14
By default, a list has 1 item that is an empty word To create an empty list, click the left arrow to remove it New lists can have one nothing ITEM 15
A named list with 3 elements 16 Give a name to an “empty” list, add 3 names Show all, first, last, and any element in the list known as names names is a list with zero elements
Other blocks you may need 17 These will be added to the question ifthey will be needed (no need to memorize Snap blocks) length is # items in the list add adds at the end insert places a new thing at the given index 1..length delete removes the thing at the given index
No need to memorize blocks You will see needed blocks in other questions on the Midterm Or they will be given for that problem Give a preview of the practice test And finally