1 Gentle Introduction to Programming Assaf Zaritsky Summer 2010.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
 Control structures  Algorithm & flowchart  If statements  While statements.
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
1 Gentle Introduction to Programming Session 2: Functions.
1 Gentle Introduction to Programming Assaf Zaritsky Summer 2009.
1 Gentle Introduction to Programming Session 2: Functions.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
CSE 131 Computer Science 1 Module 1: (basics of Java)
1 Computing for Engineers in Python Autumn
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
1 Programming for Engineers in Python Autumn Lecture 2: More Basics.
High-Level Programming Languages: C++
CIS Computer Programming Logic
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Programming for Engineers in Python Sawa 2015 Lecture 1: Introduction to Python 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
UniMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Lecture 1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Introduction to Computer Systems and the Java Programming Language.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Flow of Control Part 1: Selection
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
CPS120: Introduction to Computer Science Decision Making in Programs.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Algorithm Design.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CPS120: Introduction to Computer Science Variables and Constants.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 1.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
COMP Loop Statements Yi Hong May 21, 2015.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CS1010: Programming Methodology
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Chapter 10 Programming Fundamentals with JavaScript
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Week 1 – Session 1 Pn. Yasmin Yacob 04 – KUKUM Sem2-06/07
Chapter 10 Programming Fundamentals with JavaScript
Lecture 2: Logical Problems with Choices
Structured Program
1) C program development 2) Selection structure
3 Control Statements:.
Introduction to Programming
High Level Programming Languages
Computer Science Core Concepts
ICT Gaming Lesson 2.
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
Introduction to Programming
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: Check what’s new! My

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 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 Course Plan SessionMaterial 1Basic concepts in CS and programming, basic Scala 2Basic Scala (cont.), Functions 3Recursion 4Arrays, sorting algorithms 5Lists 6Object oriented programming

7 Questions?

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 Basic Terms Computer Science Computer Hardware / Software Algorithm Input / Output Pseudo-Code Programming Language Computer Program

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

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

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

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

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

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

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 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 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 Why Scala? Semester A: Scheme Semester B: Java Scala language has some features similar to Scheme and some to Java Scala is cool!

26 Hello World!

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

28 Memory

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 Variables and Types משתנה (variable): יחידת מידע המאכסנת ערך במהלך ריצת התכנית ניתן ל"גשת" למשתנה (לשנות או לקבל את ערכו) ניתן לבצע פעולות חשבוניות (ואחרות) בעזרת משתנים לכל משתנה מוגדר טיפוס (type) שקובע אילו סוגי ערכים המשתנה יכול להכיל (דוגמאות: מספר שלם, מספר ממשי, תו)

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 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 Types טיפוס של משתנה קובע: אילו ערכים יכול המשתנה להכיל מהן הפעולות שניתן לבצע על המשתנה מספר הבתים (יחידה בסיסית של זכרון המחשב) שדורש אחסון של משתנה בזכרון תלוי בטיפוסו

34 Some Basic Types

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

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 Arithmetic Operators

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 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(s c)

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 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 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 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 &&, ||, != && || !

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 All Examples at BasicExamples.scala

47 Operators in Scala

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 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 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 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 Example if (price > 100) if (price < 200) println(“reasonable price”) if (price > 100 && price < 200) println(“reasonable price”)

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

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 Examples at Factorial.scala

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 Compiler

58 Interpreter

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

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

61 Runtime

62 Hybrid Approaches

63 Hello World ;-)

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 Working Environment Interpreter vs. scripts vs. Eclipse Home vs. labs VS.

66 Setting Home Download Java ( ) Download Scala ( ) Download Eclipse ( ) Set Scala plugin to Eclipse ( ) Start programming

67 Download Java

68 Download Scala

69 Download Scala

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

71 Download Eclipse

72 Set Scala Plugin to Eclipse

73 Set Scala Plugin to Eclipse (new)

74 Set Scala Plugin to Eclipse (new)

75 Open Eclipse – Set Workspace

76 Open Eclipse

77 Define New Project

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

79 New Object

80 Write Some Code

81 Run output

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 Exercise 0 Write your first “Hello World!” program in Scala: Use the interpreter In Eclipse Make sure it compiles and executes properly

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 סלט פיבונאצ'י

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

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