PROGRAMMING LOGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 1 Week 1 1 • Computer Components and Operations 2 • Planning Logic 3 • Pseudo Statements 4 • Variables 5 • Data types 6 • Keywords Learning Objectives • Explain how scripts are included, handled and processed by browsers Computer components & operations • Your computer is composed of the hardware, or computer equipment, and the software, the instructions that tells the hardware what to do • Software programs are instructions written by programmers Computer components & operations • Generally, there are two types of software: – Application or task software like Word or PhotoShop – System software, the programs that manage your computer like your operating system of Windows • We will focus on the logic in writing application programs. The instructions you will write is called program code. Computer components & operations • Computers perform 3 major operations – Input – keyboard and mice entry of data – Processing – Organizing, checking, perhaps performing mathematical operations, on the data, performed by the central processing unit or CPU – Output – the bits of entered data is processed into information, usable by people to see, interpret and use via a monitor, printer, or other output device Computer components & operations • Our software instructions are written in a programming language, such as Java or C+ • Rules for writing is called the syntax • Computers need you to be exact when you write instructions • If the syntax isn’t perfect, the computer won’t understand Computer components & operations • Computers operate on circuitry that consists of millions of on/off switches • Each programming language uses a piece of software to translate your statements into object code, or machine language • Machine language is a series of 0s and 1s, called binary form Computer components & operations • The software that translates your statements into binary form is called a compiler or interpreter • It will issue a message warning if you had made a syntax error, such as misspelled a word or used incorrect punctuation • Syntax errors are relatively easy to find and fix Computer components & operations • The compiler highlights the errors and your program will not run until the error is fixed • There are different compilers and interpreters but generally their function is to translate your programming statements into code the computer can understand • The program runs and executes a program’s instructions Computer components & operations • If there are no syntax errors, the program will run • However, it may still not give correct information Computer components & operations • To work right, the instructions need to go to the computer in a specific sequence • No instruction can be left out • You may not add extraneous instructions • …in other words, you develop the logic of the computer program Computer components & operations • • • • • Stir Add two eggs Add a gallon of gasoline Bake at 350 degrees for 45 minutes Add three cups flour Computer components & operations • Though the instructions were written in correct English spelling and grammar, the instructions were out of sequence, some instructions were missing, and some instructions belonged to a different procedure. • These are logical errors, and they are more difficult to find than syntax errors. Computer components & operations • While compilers stop at a syntax error until it is fixed, errors in logic cannot be as easily located by the compiler. It usually needs to execute the program before errors can be found. • Logic is the same across all the different languages you can write Computer components & operations • Write a program that takes a number (input) • Double it (processing) • Tell the answer (output) • Input originalNumber. • Compute calculatedAnswer = originalNumber times 2. • Output calculatedAnswer. Computer components & operations • Input originalNumber is an example of an input operation. • The computer knows to interpret the instructions using an input device to obtain a number and store it in memory named originalNumber • You would tell the computer what input device to use, but by default it’s the keyboard • These are processing steps Programming process • • • • • • • Understanding the problem Plan the logic Code the program Translate the program into machine language Test the program Put program into production Maintain the program Programming process • Understanding the problem – Give me a list of students who are over 25 years of age with 25 or more college credits • • • • • • All students, AI students, currently attending? 25 years of age to no limit? Credits earned at this college? How does the information need to be delivered? What data is currently available? ????? Programming process • Plan the logic – Plan the steps of your program, decide what actions to include and their order – Called developing an algorithm – Algorithm is a sequence of steps to solve a problem – Flowcharts and pseudocode are common planning tools to plan and show all necessary steps – Writing out the steps on paper is called desk-checking Programming process • Code the program – After the developing the logic, a language is chosen to handle the type of operation the programmer wants – Then using the correct syntax can be employed – The prior planning step is actually more difficult than writing correct syntax code since now you are translating – There are many programming languages, but computers only understand one: machine language Programming process • Code the program – Machine language is understood by the computer because the computer is made of the same system of 1s and 0s, thousands of tiny electrical switches turned on (1) and off (0) state – Languages like Java are used because someone wrote a translator (compiler or interpreter) program that turns the high-level programming language into lowlevel machine language Programming process • Code the program – Writing your program with misspelled, missing or “illegal” grammar will cause the translator program to issue an error message identifying a syntax error – It will not execute a program that contains even one syntax error Programming process • Code the program – Usually you will write some code and then submit it, troubleshoot any errors, and repeat – An executable program is created only when there are no syntax errors Programming process • Test the program – No syntax errors does NOT mean no logical errors – When the program is syntax error free, the programmer executes the program with some sample data to find if results are logically correct – If not, maybe the program has logic errors that need to be corrected – Programs should be tested with many sets of data Programming process • Put program into production – Upon completing the production and testing of a program, it is put into use – If it is a simple program like providing a list, it might just be ran once as needed – If it is a large company-wide program, there may be months or years of feedback and training as a conversion to the new program occurs Programming process • Maintain the program – Maintenance involves updating the program as needed after it is put into use – Writing clear code, commenting your steps, using reasonable identifiers for values, and documentation will all make this process easier for you or whoever takes over the program implementation Pseudocode statements & flowchart symbols • Pseudocode is an English-like listing of the logical steps it would take to solve a problem. It means false code • A flowchart is the pictorial representation of the same thing start input originalNumber compute calculatedAnswer as originalNumber times 2 output calculatedAnswer stop Pseudocode statements & flowchart symbols • A flowchart is the pictorial representation of the same thing start input originalNumber compute calculatedAnswer as originalNumber times 2 output calculatedAnswer stop Pseudocode statements & flowchart symbols Programmers usually use either pseudocode or a flowchart, not usually both. Pseudocode is similar to step by step instructions, while a flowchart is like a map LAB: VARIABLES AND DATA TYPES Using and naming variables • We ask the computer to store our program in variables • Variables are named memory locations • originalNumber is stored in a location as a variable, and its content can vary • One memory location can be used many times, with different values Using and naming variables • You can write program instructions once but use for thousands of separate calculations • This is what makes programming valuable • The ability of memory variables to change in value makes computers and programming valuable Using and naming variables • A variable name is called an identifier • Each programming language has its own set of rules for creating identifiers • Most allow bot letters and digits • Some allow hyphens (-), while others allow underscores (_). Some do not allow either. Using and naming variables • Some languages, like php, allow dollar ($) signs, and others foreign alphabet characters • Different languages put different limits on the length of variables but in general, newer languages allow longer names Using and naming variables • Newest versions of C++, C#, and Java, allow almost unlimited identifier names • These languages are case sensitive • Camel casing is when programmers use an uppercase letter for the second word, like hourlyWage • Pascal casing is when the first letter is uppercase, as in HourlyWage Using and naming variables • Logic works with any language so there are only two rules when naming a variable: – Variable names must be one word – Variable names must have meaning • The first is a rule: no programming language allows spaces with a name. The second is a best practice, making your logic more useful Assigning value to variables • Assignment statements involve two actions: – The computer calculates the arithmetic value – The computed value is stored in memory • In pseudocode this could be written as: Compute calculatedAnswer as originalNumber times 2 • An assignment statement would be written like this: calculatedAnswer = originalNumber * 2 • The assignment operator is the equal (=) sign • = means assignment not equivalency Named constants • Most programming languages allow creating named constants • You would use a constant when you want a value that doesn’t change • Unlike a variable, a constant would not have different values over time • Giving an appropriate name in uppercase and underscores is naming the constant, instead of leaving unnamed (called a magic number) that maybe isn’t apparent Named constants • Example: If a program needs the sales tax included, you could declare the named constant: num SALES_TAX = 0.06 Then write the program statement: finalPrice = price + price * SALES_TAX • Named constants are declared differently among programming languages. We are using a standard convention Arithmatic operations • • • • + (plus sign) addition - (minus sign) subtraction * (asterisk) multiplication / (slash) division Most modern languages also include a remainder operator, represented by a percent sign (%), to represent a remainder after a division operation Arithmetic operations • You can combine arithmetic statements, each operator following rules of precedence • Rules of precedence dictates the order the operations are carried out • Multiplication and division always takes precedence over addition and subtraction. a+b*c In this expression, you would multiply b and c, then add a Arithmetic operations • In arithmetic statements, the rules of precedence can be overridden by using parentheses 2+3*4 (2 + 3) * 4 rules of precedence says the answer is 14 using parentheses gives 20 as the answer • Forgetting to add parenthesis or the rules of precedence can lead to logical errors hard to find in a program when you’re troubleshooting • Choose to use parenthesis to help make your intention clear as needed Data types and declaring variables • Computers use two basic types of data – Numeric with a specific numeric value called a numeric constant. 43 is always 43 and you write using the digits, no quotation marks – A string constant, or text, is enclosed within quotation marks, unnamed and literal “Amanda” Data types and declaring variables • A variable’s data type describes kind of value – A numeric variable can have math operations performed on it – A string variable can hold alphabet and special characters like punctuation marks. It is text • Computers handle the two types differently, so you’re distinguishing the two when you declare the variable Data types and declaring variables • When you declare the variable you let the computer know the data type and an identifier to expect • You must declare the variable first string lastName num salary • then you can assign value, send to output, or perform an operation allowed for its data type Data types and declaring variables • You add a sentinel value to end a program, to prevent situations like an infinite loop • Computers are binary so you could give a dummy value that is not real data but signals stop, and in a flowchart, would be represented by a decision symbol shaped like a diamond • There would be only two mutually exclusive answers, yes or no, true or false Data types and declaring variables • originalNumber = 0? for “Is the value of originalNumber equal to 0?” • Many programming languages use the term eof for “end of file” to refer to a marker automatically stored at the end of data • This acts as a more efficient sentinel value Case Study • Create the logic for a Mad Lib program that displays a message asking the user to provide five words And then accepts those words and creates and displays a short story or nursery rhyme using them Require two nouns, an adjective, and a past-tense verb Discussion • What are you going to use, pseudocode or flowchart? • What data type? • What are the sequence of steps? • How would you signify the sentinel value? Homework Assignment • Develop the logic for your favorite recipe. Due the night before the next class, in the dropbox for week 1