COMP 102: Computers and Computing

Similar documents
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

Lecture 1: Introduction

printf( Please enter another number: ); scanf( %d, &num2);

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

COMP 102: Computers and Computing

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Introduction to Scientific Computing

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

An Introduction to Python (TEJ3M & TEJ4M)

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

These are notes for the third lecture; if statements and loops.

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Controls Structure for Repetition

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Intro. Scheme Basics. scm> 5 5. scm>

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Programming II (CS300)

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

6.001 Notes: Section 15.1

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Programming Languages, Summary CSC419; Odelia Schwartz

1007 Imperative Programming Part II

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #01 Introduction: The Process of Programming

Algorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science

CSEN 102 Introduction to Computer Science

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Programming with Python

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CS558 Programming Languages

Python for Informatics

6.001 Notes: Section 6.1

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Compilers. Prerequisites

Lecture 2 Finite Automata

PREPARING FOR PRELIM 1

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

(Refer Slide Time: 01.26)

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

2 Ambiguity in Analyses of Idiomatic Phrases

COMP 102: Computers and Computing

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

MITOCW watch?v=kz7jjltq9r4

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem

Maciej Sobieraj. Lecture 1

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Civil Engineering Computation

6.001 Notes: Section 8.1

Operational Semantics. One-Slide Summary. Lecture Outline

5. Control Statements

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Week - 01 Lecture - 04 Downloading and installing Python

Welcome to CS 115 (Winter 2018)

CPS122 Lecture: From Python to Java

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

The following content is provided under a Creative Commons license. Your support

(Refer Slide Time: 02:59)

Administration Computers Software Algorithms Programming Languages

Slide Set 1 (corrected)

First Java Program - Output to the Screen

(Refer Slide Time: 1:27)

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

CS 231 Data Structures and Algorithms, Fall 2016

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

1.7 Limit of a Function

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

COMP-202 Unit 4: Programming with Iterations

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

Transcription:

COMP 102: Computers and Computing Lecture 5: What is Programming? Instructor: Kaleem Siddiqi (siddiqi@cim.mcgill.ca) Class web page: www.cim.mcgill.ca/~siddiqi/102.html

Motivation The advantage of a computer it its ability to solve almost any problem. BUT: You need to explain what you want! With the right programming, a computer can: Create movie animations. Compose music. Play chess. At a low-level, all programs are implemented with Boolean logic blocks and finite-state machines. Programming gives you a higher-level way of expressing problems. It also means you can think about problems at a higher-level. 2

A programming language A programming language is a set of building blocks for constructing computer software. Like human language, there is a vocabulary and a grammar. Sometimes called semantic and syntactic rules. Unlike human language, there is a very precise meaning for every word and sentence. This is necessary so that the computer knows exactly how to interpret your instructions. 3

Languages Many different languages have been devised for people to communicate. How many languages do you speak? Many different languages have been devised to communicate with computers. How many can you name? Why do we need many languages? 4

Languages Each language provides a way of writing a kind of script along with rules for the computer to interpret the script as instructions. They can do very similar things, but some things are easier to say in some languages than in others. 5

Programming languages Tens of different programming languages have been created. E.g. Java, C, C++, Perl, Python, O Caml, Lisp, Each has its own vocabulary. Elements of the vocabulary can be combined to define new concepts. Each has its own syntax. Each language is good at describing different things. 6

A computer program Simplest definition of a program: A sequence of instructions that a computer can interpret and execute. Is the following a computer program? Analyze the US Census! No! As stated, it cannot be understood and therefore cannot be executed by the computer. It is an instruction written in English. With computers, it is important to be very precise! What is the census, what specific questions, with what precision,... 7

Recall 4 key steps of computing Input Output Calculate Memorize Programming languages need to handle all these components. 8

A computer program In programming, need to deal with 2 kinds of things: Data: Input Output Intermediate Procedures: List of instructions. Each instruction tells the computer to do something. Instructions are ordered correctly. 9

A Recipe for Scrambled Eggs Ingredients: Instructions: 2 eggs, 1 tbsp oil, salt Step 1: Add oil to pan. Step 2: Heat pan on stove. Step 3: Crack 1st egg into pan. Step 4: Crack 2nd egg into pan. Step 5: Add salt. Step 6: Mix until light and cooked. Output: Scrambled eggs! A recipe is a series of steps. What if we did not follow the order? Would not get scrambled eggs! Can we express this more generally? 10

Making Scrambled Eggs for 10 Ingredients: Instructions: 20 eggs, 1 tbsp oil, salt Step 1: Add oil to pan. Step 2: Heat pan on stove. Step 3: Crack 1st egg into pan. Step 4: Crack 2nd egg into pan. Step 22: Crack 20th egg into pan. Step 23: Add salt. Step 24: Mix until light and cooked. Output: Scrambled eggs for 10! 11

Another way of making eggs for 10 Repeat 10 times: Ingredients: Instructions: 2 eggs, 1 tbsp oil, salt Step 1: Add oil to pan. Step 2: Heat pan on stove. Step 3: Crack 1st egg into pan. Step 4: Crack 2nd egg into pan. Step 5: Add salt. Step 6: Mix until light and cooked. Output: Scrambled eggs! Done repeating. 12

Making Scrambled Eggs for N people Ingredients: Instructions: 2*N eggs, 1 tbsp oil, salt Step: Add oil to pan. Step: Heat pan on stove. Step: For i = 1 to 2*N Step: Crack i th egg into pan. Step: End loop Step: Add salt. Step: Mix until light and cooked. Output: Scrambled eggs for N! Introduce input variable, N Introduce a loop, with count variable, i Repeat step, until count variable is done Terminate loop when counter reaches max 13

Variables Variables are containers (in memory) for information that you want to store. The information you put in the variable is called its value. Putting a value in the container is called assigning a variable. When you write the variable name in a program, the variable name will be replaced by its value, whenever the computer gets to that point in the program. This is called accessing a variable. Variables can be reassigned values at different points in the program. (When that happens, the old value is lost.) 14

Naming variables (and functions) What names can we choose for our variables? Lots of possibilities! But it depends on the programming language. Avoid key words/characters that are used by the programming language to mean something specific. E.g. for is used to denote a loop type, so can t use it to name a variable! Same for mathematical operators and most punctuation signs. Can t have a space ( ) as part of the name, because spaces denote a change of word. These are just examples - specific rules change from one programming language to another. Good names: Bob, C3P0, The_cat_in_the_hat Bad names: 1*2, a-b, for, print, if, while 15

Types of variables Boolean: Value can be 0 or 1. E.g. Bob = 0 Integer: Value can be any whole number. E.g. Bob = 5 Float: Value can be any real number (up to some pre-set precision). E.g. Bob = 1.3333333333 Characters: Value can be any ASCII character. E.g. Bob = p Strings: Value is a sequence of characters. E.g. Bob = Happy birthday! 16

Worrying about types of variables The types listed in the previous slide are available in most programming languages. Other types are possible in some languages. Most languages require that the variable type be declared before the variable is assigned or accessed. E.g. integer bob bob = 5 bob = bob + 2 Some languages do not handle variable types (e.g. Scratch). Means that the language assumes all variables are of the same type, and each variable is stored in a memory container of a fixed size. Some languages do not require type to be declared (e.g. Matlab). Also means the language will assume something about variable type, and reserve a memory block accordingly. 17

Arithmetic Operations The arithmetic operators (+, -, *, /) are reserved for mathematical operations. Let x be a variable. x = 5 x = x+2 <- Initial variable assignment. <- Variable is re-assigned. What happens if you skip the first step? It depends on the programming language. In some cases, it may depend on what was in the memory block associated with x before! Could be bad. Most languages also accommodate logical variables and operators. 18

Comparison Operators The comparison operators compare two values (numbers or variables). There are several comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to 19

Loops Syntax for telling the computer to repeat the same instruction many times. Example: Write a program to sum the numbers from 1 to K. SumUpTo ( K ) Program name SumUpTo with input variable K n = 1 Add new variable n to count from 1 to K sum = 0 Add new variable sum to store the intermediate sum While n <= K Loop command While, with the termination defined by comparison n<=k sum = sum + n First instruction in the loop. n = n+1 Second instruction in the loop. End loop Syntax requires that you specify up to which instruction goes inside the loop PrintText The sum of numbers 1 to K is Print the result. PrintVariableValue sum What if you want to print the intermediate results? Easy to do! 20

Different types of loops: Comparing Loops SumUpTo ( K ) n = 1 sum = 0 While n <= K sum = sum + n n = n+1 End loop PrintText The sum of numbers 1 to K is PrintVariableValue sum SumUpTo ( K ) sum = 0 For n = 1 to K sum = sum + n End loop PrintText The sum of numbers 1 to K is PrintVariableValue sum Similar syntax, slightly different functionality. In this case, it does not matter, but in other cases, it might. 21

Loop that quits when it reaches its goal Consider summing integers, until the sum reaches 100. SumUpTo ( ) n = 1 sum = 0 While sum <= 100 sum = sum + n n = n+1 End loop PrintText The sum of numbers is PrintVariableValue sum Can we do the same thing for any target max value? Yes! SumUpTo ( max ) n = 1 sum = 0 While sum <= max sum = sum + n n = n+1 End loop PrintText The sum of numbers is PrintVariableValue sum This is much easier to do with a While loop than with a For loop. 22

Functions SumUpTo ( K ) Add ( n1, n2) n = 1 sum = n1 + n2 sum = 0 Return sum While n <= K sum = Add (sum, n) n = n+1 End loop PrintText The sum of numbers is PrintVariableValue sum Here both AddToSum(K) and Add(n1, n2) are examples of functions. Functions allow you to write re-usable code. When a function is called, the computer jumps to the body of the function to execute that block of code, then comes to where it left off. Functions can call other functions. E.g. AddToSum() calls Add() 23

Functions SumUpTo ( K ) Add ( n1, n2) n = 1 sum = n1 + n2 sum = 0 Return sum While n <= K sum = Add (sum, n) n = n+1 End loop PrintText The sum of numbers is PrintVariableValue sum Functions can take inputs (variables or constants). E.g. K, n1, n2, 100. Functions only know variables which are given as input, or defined inside. E.g. Add() does not know about variable n. Functions can return outputs. 24

Functions that call themselves A really compact way of summing up the first K integers: SumUpTo ( K ) If K > 0 sum = SumUpTo ( K -1) + K Else PrintText The sum of numbers is PrintVariableValue sum End conditional New element here: conditional statement 25

Conditional statement Execute a block of code only if a certain statement is true. General form: If some expression is true Do this. Else Do that. 26

Generalizing some of these ideas You can have conditionals within loops, conditionals within functions, conditionals within conditionals, functions within loops, functions within conditionals, functions within functions, loops within loops, loops within functions, loops within conditionals Most examples we saw today deal with numbers. Many programs deal with other types of data, e.g. Strings, logical variables, etc. Easy to declare variables of those types. But: Need to be careful about how we use mathematical operators and comparison operators when dealing with these types of variables. 27

Is your program correct? Very important to have the correct program! Throughout the software industry, roughly 90% of efforts goes to testing/debugging and only 10% of efforts for programming. Can you check for correctness of the program? Need to check it works for all possible inputs. E.g. Check that it won t loop forever. Check that it won t set variables to wrong values. Substantial work in this area in software engineering. 28

Take-home message Know the difference between a programming language and a program. Understand the need to be very precise when writing instructions for a computer. But realize that there are different ways of instructing the computer to do the same thing. Understand the basic concepts of programming: Variables, mathematical operators, comparison operators, loops, conditionals, functions. 29

Final comments Some material from these slides was taken from: http://cim.mcgill.ca/~sveta/comp102/ 30