1 / 84

Gentle Introduction to Programming

Gentle Introduction to Programming. Assaf Zaritsky Summer 2010. 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.il. Course Description.

Download Presentation

Gentle Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Gentle Introduction to Programming Assaf Zaritsky Summer 2010

  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.il

  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. Objective Bridge the gap for students without prior programming knowledge

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

  6. Course Plan

  7. Questions?

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

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

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

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

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

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

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

  15. Example

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

  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. There are Many Programming Languages Over 500 different computer languages are listed by Wikipedia

  19. Language Selection • Goal • Runtime vs. Development time • Operating systems • Platforms

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

  21. 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

  22. Agenda for Today • Administration • General introduction • The Scala programming language: • Variables and Types • Operators • Branching and Repetition • Compiler, Interpreter • Working environment • Home work

  23. 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

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

  25. Hello World!

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

  27. Memory

  28. 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

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

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

  31. 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

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

  33. Some Basic Types

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

  35. 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!”

  36. Arithmetic Operators

  37. 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)

  38. 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)

  39. 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)

  40. Rational Operators Compares two numbers and returns a Boolean

  41. 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))

  42. Logical Operators Operates on two Booleans and returns a Boolean

  43. &&, ||, != || && !

  44. 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)

  45. All Examples at BasicExamples.scala

  46. FlowControl • 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

  47. 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

  48. if-else Statement if (expression) statement1 else statement2 • if expression is true, statement1 is executed. • if expression is false, statement2 is executed • both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

  49. Agenda for Today • Administration • General introduction • The Scala programming language: • Variables and Types • Operators • Compiler, Interpreter • Working environment • Home work

More Related