lecture 1 introduction
play

Lecture 1: Introduction Programming Fundamentals Gorka Guardiola - PowerPoint PPT Presentation

Lecture 1: Introduction Programming Fundamentals Gorka Guardiola LS, GSYC October 24, 2011 Gorka Guardiola (LS, GSYC) Lecture 1: Introduction October 24, 2011 1 / 32 (cc) 2010 Grupo de Sistemas y Comunicaciones. Some rights reserved. This


  1. Operators Operators represent operations in Picky. Infix: between two (Picky), i.e. a + b Prefix ( Polish notation) before the operands, i.e. + a b Postfix ( reverse Polish notation) after the operands, i.e. a b + Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 6 / 20

  2. Numerical operators float and int ◮ Sign: - + ◮ Addition: + ◮ Subtraction: - ◮ Multiplication: * ◮ Division: / ◮ Exponentiation: ** Only for int: ◮ Modulo (remainder): % Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 7 / 20

  3. Boolean operators Complement/Negation: not Conjunction: and Disjunction: or Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 8 / 20

  4. Comparison operators/relational operators Equal to: == Not equal to: ! = Greater than: > Greater or equal than: > = Less than: < Less or equal to: < = see edad.p Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 9 / 20

  5. Relational operators The resulting value is of type bool Only compatible types can be comparated How do I compare floats? eps = 0.001; esPi = x > = Pi - eps and x < = Pi + eps; Can I compare chars? see real.p Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 10 / 20

  6. Expressions Only one line Operations are evaluated in a concrete order: precedence . see expr.p Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 11 / 20

  7. Expressions Can be grouped with parenthesis Evaluation is from the inside to the outside Spaces should be used for legibility Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 12 / 20

  8. Expressions: example ((3 ∗ 2) / (5 + 6 − (3 ∗ ∗ 2))) (6 / (5 + 6 − (3 ∗ ∗ 2))) (6 / (5 + 6 − 9)) (6 / (11 − 9)) (6 / 2) 3 Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 13 / 20

  9. Precedence table (all) 1 lower 2 or and 3 == ! = < > < = > = 4 (operation) + - 5 * / % 6 ** 7 (sign) + - 8 len not 9 higher Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 14 / 20

  10. Expressions With same precedence are evaluated from left to right Operators associate to the left Parenthesize operators with same precedence Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 15 / 20

  11. Expressions Example: It is a leap year if it is multiple of 4, not multiple of a 100 unless it is multiple of 400. see bisiesto.p Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 16 / 20

  12. Expressions: De Morgan Laws not (a and b) ≡ (not a) or (not b) not (a or b) ≡ (not a) and (not b) Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 17 / 20

  13. Recomendations: Minimize number of negations: Try not to minimize the number of non-negations Which one is easier to understand? Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 18 / 20

  14. Basic Sets in Picky Explicit type conversion: ◮ int(3.14) ◮ float(3) ◮ char(3) ◮ int(’a’) Not any type can be converted to any other. Allowed: ◮ int → float ◮ float → int ◮ Ordinal → int ◮ int → Ordinal Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 19 / 20

  15. Creating our own functions Receives some parameters to make a calculation Returns only one value of the specified type Gorka Guardiola (LS, GSYC) Lecture 2: Sets and elements October 7, 2011 20 / 20

  16. Lecture 3: Direct Solution Problems Programming Fundamentals Gorka Guardiola LSUB, GSYC, URJC October 14, 2011 Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 1 / 20

  17. (cc) 2010 Grupo de Sistemas y Comunicaciones. Some rights reserved. This work is provided under Creative Commons Attribution License - NonCommercial - NoDerivs (by-nc-nd). To obtain the complete license go to http://creativecommons.org/licenses/by-sa/2.1/es. or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 2 / 20

  18. Start with simple direct solution problems Apply a formula Simple steps, no decisions need to be taken Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 3 / 20

  19. Definition: Functions Problem definition → function header What is it calculating → function name What does it need → function parameters Result → Type returned by the function Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 4 / 20

  20. Problem definition: Functions Programming style in Picky: Function name has to correspond to the object returned e.g.: circlearea . Function and parameter identifiers are lowercase (all of them) Important to choose the name appropiatively Clarity of expression is important Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 5 / 20

  21. Problem definition: Functions Argument: value given to the function when it is being called Parameter: identificator used to refer to the argument A function can have none, one or more parameters Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 6 / 20

  22. Problem definition: Functions A function must always return one and only one value The value returned is of the function return type Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 7 / 20

  23. Problem solving: Functions The solution is implemented in the body of the function The reserved word return makes the function return a value Structured programming: return always at the end of the function, only once step by step circulo.p Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 8 / 20

  24. Subprograms Functions are subprograms Subprograms solve subproblems Big programs difficult to understand/debug . Understanding a program means understanding each subprogram separately, which is easier. Divide and conquer . Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 9 / 20

  25. Constants Problem parameters Constants instead of literals → less bugs Name instead of a number, less repetitions Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 10 / 20

  26. Declaration and definition Declaration means stating something exists Define means to determine its value Scope: An object can only be used after it has been declared (when it starts existing) until the end of the program/subprogram where it was declared. Outside of its scope, it is not visible see pesos.p Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 11 / 20

  27. Problem attack: top-down Based on optimism When we detect a subproblem we consider it solved and continue To progressively refine we can use empty or fake subprograms If you don’t have it, fake it!! Later we have to implement the subprogram This way the program/problem is broken into subprograms/subproblems naturally On each phase of the implementation we will have a prototype we can execute step by step cilindro.p Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 12 / 20

  28. Example top-down : student’s day Problem: Student’s day Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 13 / 20

  29. Example top-down : student’s day Student’s day 1 Wake up 2 Go to school 3 Go back home 4 Do the homework 5 Go to bed Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 14 / 20

  30. Example top-down : student’s day D´ ıa de estudiante 1 Wake up 2 Go to school 3 Go back home 4 Do the homework Programming homework 1 Algebra homework 2 5 Go to bed Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 15 / 20

  31. Example top-down : student’s day D´ ıa de estudiante 1 Wake up 2 Go to school 3 Go back home 4 Do the homework Read the chapter of the book 1 1 Start the weekly hand-in 2 Algebra homework 2 5 Go to bed Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 16 / 20

  32. Problem attack: bottom-up Based on thinking in advance. Start with breaking the problem and thinking of all the elements needed Then we write the subprograms solving the simplest subproblems Combine them to solve more complex problems until we get to the solution Difficulty: some amount of intuition is needed to find the right elements (given by experience) Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 17 / 20

  33. Both have to be combined � ������� ���� ������� ���� ���������� ������� ���� ���������� ������� ���� ���������� ������� ��������� Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 18 / 20

  34. Problem attack: simplification and generalization 1 Think the solution for a simple, particular case 2 Try it 3 Generalize it program digitos.p Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 19 / 20

  35. Functions provided by Picky 1 acos(r: float): float 2 asin(r: float): float 3 atan(r: float): float 4 cos(r: float): float 5 exp(r: float): float 6 log(r: float): float 7 log10(r: float): float 8 pow(r: float): float 9 sin(r: float): float 10 sqrt(r: float): float 11 tan(r: float): float Gorka Guardiola (LSUB, GSYC, URJC ) Lecture 3: Direct Solution Problems October 14, 2011 20 / 20

  36. Lecture 4: Selection Problems Programming Fundamentals Gorka Guardiola LS, GSYC, URJC October 24, 2011 Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 1 / 9

  37. (cc) 2010 Grupo de Sistemas y Comunicaciones. Some rights reserved. This work is provided under Creative Commons Attribution License - NonCommercial - NoDerivs (by-nc-nd). To obtain the complete license go to http://creativecommons.org/licenses/by-sa/2.1/es. or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 2 / 9

  38. Selection Problems Problems where a decision needs to be taken. We need control structures . e.g.: piece-wise functions like the sign. Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 3 / 9

  39. If If the expression evaluates as True , the statements of the body of the if are executed: if (condition) { statements; } Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 4 / 9

  40. If-else If the expression evaluates as True , the statements of the body of the if are executed, in other case the statements of the body of the else are exectuted. if (condition) { statements; } else { statements; } do signo.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 5 / 9

  41. If-else: style Each block → one level of indentation. We can visually ignore details → legibility. The code is indented with the tab . Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 6 / 9

  42. else if Simplificates the program when there are multiple cases No new functionality, the same can be expressed with nested if-else . if (condition) { statements; } else if (condition) { statements; } else if (condition) { statements; } else { statements; } change signo.p see notasimple.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 7 / 9

  43. case Select based on an discrete types (int, char...). Can always be changed into a (longer) else if. All possible cases need to be covered , (comma) to express a discrete set of values. .. (dot dot) to express a range of ordinals. Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 8 / 9

  44. case switch (expresion) { case valx , valy , valn: statements; case val0: statements; case val0, val1 .. valn: statements; default : statements; } do fechavalida.p do sieteymedia.p do hexa2decimal.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 4: Selection Problems October 24, 2011 9 / 9

  45. Lecture 5: Variables and Procedures Programming Fundamentals Gorka Guardiola LS, GSYC, URJC November 8, 2011 Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 1 / 21

  46. (cc) 2010 Grupo de Sistemas y Comunicaciones. Some rights reserved. This work is provided under Creative Commons Attribution License - NonCommercial - NoDerivs (by-nc-nd). To obtain the complete license go to http://creativecommons.org/licenses/by-sa/2.1/es. or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 2 / 21

  47. Variables Object representing a piece of memory modifiable with an asociated identifier (name). It is of a concrete data type during its lifetime. Variables are declared inside the main program or in a subprogram. Variables have to be initialized (given a value) before being used. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 3 / 21

  48. Variables procedure sum () / ∗ d e c l a r a t i o n ∗ / number1 : int ; number2 : int ; r e s u l t : int ; { / ∗ i n i t i a l i z a t i o n ∗ / number1 = 5; number2 = 6; / ∗ use ∗ / r e s u l t = number1 + number2 ; w r i t e l n ( r e s u l t ) ; } Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 4 / 21

  49. Declaration A name is given to the variable. The name is an identifier . A space is reserved in memory, but without giving it any value. Names should be chosen: ◮ Short: more readable. ◮ Descriptive and clear: seeing the name should make the reader understand what the variable stands for. The shortest name that expresses what the variable is. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 5 / 21

  50. Declaration Example: variable used to calculate the radius of a circle for a program calculating its area: procedure a r e a c i r c l e () r a d i u s o f t h e c i r c l e : f l o a t ; / ∗ bad , too long ∗ / r a d i u s c i r c l e : f l o a t ; / ∗ redundant ∗ / r a d i o : f l o a t ; / ∗ ok ∗ / r : f l o a t ; / ∗ ok , context i s important ∗ / a : f l o a t ; / ∗ bad , not d e s c r i p t i v e ∗ / rdo : f l o a t ; / ∗ bad , u n c l e a r ∗ / { . . . } Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 6 / 21

  51. Assignment Gives value to the value Different to an equation left: identifier of the variable right: expression, same type of the variable. Initialization : first assignment, first value Before initialization, value is unknown . We can assign as many times as we want. ntimes = 7; . . . ntimes = 42; Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 7 / 21

  52. Use Expressions can be used as we have been using literals. Variables can be passed as arguments to subprograms. ntimes = 7; ntimes = ntimes + 1; w r i t e l n ( ntimes ) ; f a c t o r i a l ( ntimes ) ; Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 8 / 21

  53. Visibility and scope Scope of the variable: from the declaration to the end of the subprogram. Global variable : external to any subprogram. Local variable : declared inside a subprogram. Picky does not permit it but in any case, global variables, are a bad idea in a subprogram → use parameters. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 9 / 21

  54. Hiding There could be different objects with the same identifier in nested scopes. If the identifier is used, it references the closest scope (inner). The concrete rules depend on the language. They may not even be the same data type, kind of object. Bad idea to hide identifiers, though Picky forbids it anyway. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 10 / 21

  55. Reading a value We can read from the standard input : read . Like write is part of Picky. . . . number : int ; . . . w r i t e ( ” input a number : ” ) ; read ( number ) ; number = number + 1; w r i t e l n ( number ) ; see gets.p see leerhexa.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 11 / 21

  56. Until today... We had functions and constants Functions without parameters → constants. Functions with parameters → mathematical function. Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 12 / 21

  57. Functions Need to preserve referential transparency . Lateral effect: any effect outside of the function Breaking referential transparency , a function is not a mathematical function any more, we need to worry about the state of the whole program. To differentiate when this is necessary (as little as possible) we use procedures . Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 13 / 21

  58. Functions should not should not read or write is a lateral effect should not modify the global state (global variables) should not modify parameters (in Picky, impossible, only ref parameters in procedures) Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 14 / 21

  59. Paradigms Declaring and defining functions and constants. Preserving referential transparency: functional programming . Sequences of orders, lateral effects: imperative programming . Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 15 / 21

  60. Procedures We have already seen one, main . Declared and defined like functions but with procedure instead of function procedure p r i n t i n c r e m e n t (n : int ) { w r i t e l n (n + 1 ) ; } Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 16 / 21

  61. Parameters By value : The formal parameter (argument) is a copy of the real parameter (parameter) and is not copied back at the end of the procedure. Pass by reference : he formal parameter (argument) is the same as the real parameter (parameter) In Picky: ref name procedure increment ( r e f n : int ) { n = n + 1; } do incrementar.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 17 / 21

  62. Reference By value : Important concept, another name for the same thing (argument vs. parameter) Modifying an argument passed by reference → lateral effect do incrementar.p Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 18 / 21

  63. Functions should not should not read or write is a lateral effect should not modify the global state (global variables) should not modify parameters (in Picky, impossible, only ref parameters in procedures) Gorka Guardiola (LS, GSYC, URJC ) Lecture 5: Variables and Procedures November 8, 2011 19 / 21

Recommend


More recommend