Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Gentle Introduction to Programming Assaf Zaritsky Summer 2010.

Similar presentations


Presentation on theme: "1 Gentle Introduction to Programming Assaf Zaritsky Summer 2010."— Presentation transcript:

1 1 Gentle Introduction to Programming Assaf Zaritsky Summer 2010

2 2 Administration August 31 - September 19 9:00 – 13:30 Mostly at דן דויד 002 No grading Web: http://www.cs.tau.ac.il/gip10/ Check what’s new! My email: assafzar@post.tau.ac.ilassafzar@post.tau.ac.il

3 3 Course Description This course will provide a gentle introduction to programming using Scala for highly motivated students with little or no prior experience in programming

4 4 Objective Bridge the gap for students without prior programming knowledge

5 5 Course Description Lectures will be interactive featuring in-class exercises with lots of support You are expected to work hard! Practical sessions in lab/class Faculty guests lectures

6 6 Course Plan SessionMaterial 1Basic concepts in CS and programming, basic Scala 2Basic Scala (cont.), Functions 3Recursion 4Arrays, sorting algorithms 5Lists 6Object oriented programming

7 7 Questions?

8 8 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay

9 9 Basic Terms Computer Science Computer Hardware / Software Algorithm Input / Output Pseudo-Code Programming Language Computer Program

10 10 Computer Science ענף מדעי העוסק בלימוד הבסיס התיאורטי למידע ולחישוביות, והחלתם על מערכות מחשב "מדעי המחשב אינם עוסקים במחשב יותר משאסטרונומיה עוסקת בטלסקופ", דייקסטרה

11 11 Computer מכונה המעבדת נתונים על פי רצף פקודות נתון מראש מחשב = חומרה + תכנה מעבדאמצעי פלט אמצעי קלט זכרון (עכבר, מקלדת, דיסק קשיח) (מדפסת, מסך, דיסק קשיח)

12 12 Algorithm סדרת פעולות סופית לביצוע משימה מסויימת Algorithm InputOutput

13 13 סופית אלגוריתם: סדרת פעולות סופית לביצוע משימה מסוימת תרשים זרימה

14 14 Pseudo Code תיאור מצומצם ולא רשמי לאלגוריתם. מיועד לקריאה של בני אדם

15 15 Example

16 16 Programming Language Machine-readable artificial language designed to express computations that can be performed by a computer

17 17 Machine Code (Language) Computers understand only machine language Basically looks like a sequence of 1’s and 0’s Very inconvenient to work with and non intuitive All other computer languages were created for human convenience The computer does not understand C/C#/Java/Scala Must be “translated” into machine language

18 18 There are Many Programming Languages Over 500 different computer languages are listed by Wikipedia

19 19

20 20 Language Selection Goal Runtime vs. Development time Operating systems Platforms

21 21 Computer Program מימוש של אלגוריתם באמצעות שפת תכנות

22 22 Computer Program (more technically) A sequence of processor instructions designed to achieve a specific purpose The instructions are executed sequentially. No instruction is executed before the previous has been completed

23 23 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay

24 24 Scala History Created by Martin Odersky at EPFL Released Jan 2004 Twitter switched large portions of their code to Scala (and intend to convert the rest) Linkedin use it Other companies Over 4000 downloads per month

25 25 Why Scala? Semester A: Scheme Semester B: Java Scala language has some features similar to Scheme and some to Java Scala is cool!

26 26 Hello World!

27 27 My First Scala Program: Hello World! Hello.scala

28 28 Memory

29 29 Memory (Cont.) The computer memory is composed of a long list of bits Bits are grouped into bytes and words Every byte is numbered sequentially This number is called an address

30 30 Variables and Types משתנה (variable): יחידת מידע המאכסנת ערך במהלך ריצת התכנית ניתן ל"גשת" למשתנה (לשנות או לקבל את ערכו) ניתן לבצע פעולות חשבוניות (ואחרות) בעזרת משתנים לכל משתנה מוגדר טיפוס (type) שקובע אילו סוגי ערכים המשתנה יכול להכיל (דוגמאות: מספר שלם, מספר ממשי, תו)

31 31 Define Variables Use var to declare variables: var x : Int = 3 var y : Int = 5 var z : Int = x + y var s1 : String = “Hello World” define variable variable name typevalue assignment operator Variable x “is of type” Int and is assigned the value 3

32 32 Why do We Need Variables? Computer programs manipulate data Data is given as input or calculated throughout the program To be later accessed, variables must be remembered Thus, variables are stored in the memory Variable name  memory address

33 33 Types טיפוס של משתנה קובע: אילו ערכים יכול המשתנה להכיל מהן הפעולות שניתן לבצע על המשתנה מספר הבתים (יחידה בסיסית של זכרון המחשב) שדורש אחסון של משתנה בזכרון תלוי בטיפוסו

34 34 Some Basic Types

35 35 Why Do We Need Different Types? Saving memory Execution speed Makes compiler “life” easier

36 36 Define Variables Int: var x : Int = 5 Double: var pi : Double = 3.14 Boolean: var term : Boolean = true Char: var c : Char = ‘A’ String: var st : String = “I want a break!”

37 37 Arithmetic Operators

38 38 Example 1 var a : Int = 3 var b: Int = 5 var c : Int = a + b println(c) c = c * 2 println(c) var first : Int = (a + b) * 2 var second : Int = a + b * 2 println(first) println(second)

39 39 Example 2 // Demonstrate strings addition var s1 : String = “He” var s2 : String = “llo” var s3 : String = s1 + s2 println(s3) var s4 : String = s3 + “ World ” var c : Char = ‘!’ println(s4 + 2009 + c)

40 40 Example 3 // Reverse a 3-digits number println( “ Enter a 3-digit number ” ) var num : Int = Console.readInt() // read a number from the user var ones : Int = num % 10 var tens : Int = (num % 100) / 10 var hundreds : Int = num / 100 var reverseNum : Int = (ones * 100) + (tens * 10) + hundreds println( “ the reverse number is “ + reverseNum)

41 41 Rational Operators OperatorNameDescription x < y Less thantrue if x is less than y, otherwise false. x > y Greater thantrue if x is greater than y, otherwise false. x <= y Less than or equal to true if x is less than or equal to y, otherwise false. x >= y Greater than or equal to true if x is greater than or equal to y, otherwise false. x == y Equaltrue if x equals y, otherwise false. x != y Not Equaltrue if x is not equal to y, otherwise false. Compares two numbers and returns a Boolean

42 42 Example // Demonstrate rational operators println( “ Enter the first number ” ) var x : Int = Console.readInt() println( “ Enter the second number ” ) var y : Int = Console.readInt() println( “ x < y is “ + (x < y)) println( “ x > y is “ + (x > y)) println( “ x <= y is “ + (x <= y)) println( “ x >= y is “ + (x >= y)) println( “ x == y is “ + (x == y)) println( “ x != y is “ + (x != y))

43 43 Logical Operators Operates on two Booleans and returns a Boolean OperatorNameDescription x && y AndTrue if both x and y are true, otherwise false. x || y Or True if at least one of x or y are true, otherwise false. ! X NotTrue if x is false, otherwise false.

44 44 &&, ||, != 01 0 00 1 01 01 0 01 1 11 01 10 && || !

45 45 Example // Demonstrate logical operators println( “ Enter the first number ” ) var x : Int = Console.readInt() println( “ Enter the second number ” ) var y : Int = Console.readInt() println( “ (x < 10) && (y < 10) is “ + ((x < 10) && (y < 10))) println( “ (x < 10) || (y < 10) is “ + ((x < 10) || (y < 10))) var state : Boolean = ((x < 10) || (y < 10) ) println( “ state is “ + state)

46 46 All Examples at BasicExamples.scala

47 47 Operators in Scala

48 48 Flow Control Usually a program is executed line after line in order It is reasonable to expect different execution orders on different inputs Computer games Illegal input Control structures if-else for loop while loop

49 49 Conditional Statement: if Used to execute conditionally a statement (or block of code) Syntax: if (expression) statement If expression is true, statement is executed statement can be replaced by a block of statements, enclosed in curly braces

50 50 if-else Statement if (expression) statement 1 else statement 2 if expression is true, statement 1 is executed. if expression is false, statement 2 is executed both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

51 51 Example // Demonstrate logical operators println( “ Enter the first number ” ) var a : Int = Console.readInt() println( “ Enter the second number ” ) var b : Int = Console.readInt() println( “ Enter the third number ” ) var c : Int = Console.readInt() if ((a + b <= c) || (a + c <= b) || (b + c <= a)) println( “ There is no triangle with these sides ” ) else println( “ There is a triangle with these sides ” )

52 52 Example if (price > 100) if (price < 200) println(“reasonable price”) if (price > 100 && price < 200) println(“reasonable price”)

53 53 Loops Used to repeat the same instructions until a stop criterion is met for, while loops: while ( ) for ( )

54 54 Example - while // factorial println( “ Enter a non-negative number ” ) var n : Int = Console.readInt() var fact : Int = 1 var i : Int = 1 while (i <= n) { fact = fact * i i = i + 1 } println(n + “ ! = “ + fact)

55 55 Examples at Factorial.scala

56 56 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay

57 57 Compiler

58 58 Interpreter

59 59 Interpreter The Read/Evaluate/Print Loop Read an expression Compute its value Print the result Repeat the above The Environment

60 60 Compiler = Clerk " ראש קטן "

61 61 Runtime

62 62 Hybrid Approaches

63 63 Hello World ;-)

64 64 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay

65 65 Working Environment Interpreter vs. scripts vs. Eclipse Home vs. labs VS.

66 66 Setting Environment @ Home Download Java (www.java.com/getjava/ )www.java.com/getjava/ Download Scala (http://www.scala-lang.org/downloads )http://www.scala-lang.org/downloads Download Eclipse (http://www.eclipse.org/downloads/ )http://www.eclipse.org/downloads/ Set Scala plugin to Eclipse (https://www.assembla.com/wiki/show/scala-ide/Requirements_and_Installation )https://www.assembla.com/wiki/show/scala-ide/Requirements_and_Installation Start programming

67 67 Download Java http://www.java.com/en/download/index.jsp

68 68 Download Scala http://www.scala-lang.org/downloads

69 69 Download Scala

70 70 Use the Interpreter Type Scala from the command prompt (linux) or using “run” (windows)

71 71 Download Eclipse http://www.eclipse.org/downloads/

72 72 Set Scala Plugin to Eclipse https://www.assembla.com/wiki/show/scala-ide/Requirements_and_Installation

73 73 Set Scala Plugin to Eclipse (new)

74 74 Set Scala Plugin to Eclipse (new)

75 75 Open Eclipse – Set Workspace

76 76 Open Eclipse

77 77 Define New Project

78 78 If you don’t find the Scala new project… File  New  Project

79 79 New Object

80 80 Write Some Code

81 81 Run output

82 82 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay

83 83 Exercise 0 Write your first “Hello World!” program in Scala: Use the interpreter In Eclipse Make sure it compiles and executes properly

84 84 Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) en.wikipedia.org/wiki/Fibonacci_number

85 85 סלט פיבונאצ'י

86 86 Exercise 1 Write a program that calculates and prints to the screen the first five Fibonacci numbers

87 87 Agenda for Today Administration General introduction The Scala programming language: Variables and Types Operators Branching and Repetition Compiler, Interpreter Working environment Home work Guest lecture by Ohad Barzilay


Download ppt "1 Gentle Introduction to Programming Assaf Zaritsky Summer 2010."

Similar presentations


Ads by Google