CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

Introduction to C Programming
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
JavaScript, Third Edition
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Objectives You should be able to describe: Data Types
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Programming with RAPTOR
Introduction to Pascal The Basics of Program writing.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming, an introduction to Pascal
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Data Types Declarations Expressions Data storage C++ Basics.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 DAI-C5-UD 1 Main Objects in Programming Languages Academic Year DAI. Credit 5 (Structured and modular programming) Ferran Chic (Ref: TR )
Bill Tucker Austin Community College COSC 1315
Definition of the Programming Language CPRL
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CMP 131 Introduction to Computer Programming
Arithmetic Operator Operation Example + addition x + y
Data Types, Identifiers, and Expressions
Lecture3.
CS150 Introduction to Computer Science 1
Algorithms computer as the tool process – algorithm
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2: Introduction to C++.
Presentation transcript:

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)

THIS WEEK Homework #2: –Turn in the non-programming part today (in class or by after class). –Can keep working on the programming part through the March 29, but you must send it to me by March 29 midnight. This includes part b) Exercises 8 & 13. Makeup session: – Tuesday March 27, 6:30-8pm, LAB 01 Quiz #2 on Wednesday –Will cover material through today.

TODAY Pascal Data Types –Real –Ordinal Data Types Integer Character Boolean Expressions again Assignment statement again Input/Output and formatting

Predefined data types Ordinal: –integer –char –boolean Non-ordinal: –real –strings ( added to Turbo Pascal)

Appear directly as values of constants or in program without being defined or declared. Integer Data Type A type Integer object represent only whole numbers. Not all integer range can be represented inside a computer. –On each system, the predefined constant MaxInt is the largest possible integer. The smallest integer is -MaxInt-1 What kinds of Objects can be Integers? –Variables –Constants –Literals Have names associated with them. They are defined (constants) or declared (variables) in the program declaration section.

Operations in Integer Arithmetic:+ - * / div mod –div computes the integral part of the result of dividing the first operand by the second. –mod returns the integer remainder of the result of dividing its first operand by its second. –div and mod are integer operators only! they expect integer operands they return an integer result Comparison Pperators: =, <>, =, Assignment: := Standard Procedures: ReadLn, WriteLn, Read, Write Standard Functions: pred, succ, others…

Integer Data Type Examples: 3 div 15 = 0 3 div -15 = 0 15 div 3 = 5 15 div -3 = div 3 = 5 16 div -3 = div 3 = 5-17 div 3 = div 3 = 6-18 div -3 = 6 3 mod 5 = 35 mod 3 = 2 -5 mod 3 = -2 4 mod 5 = 45 mod 4 = 1 -5 mod 4 = -1 5 mod 5 = 015 mod 5 = mod 5 = 0 6 mod 5 = 115 mod 6 = mod 6 = -3 7 mod 5 = 215 mod 7 = mod -7 = -1 8 mod 5 = 315 mod 8 = 7 15 mod 0 = ?? ?? Means “Undefined” For div, the sign of the result depends on the signs of the operands, just like for regular division For mod, the sign of the result only depends on the sign of the dividend

Try running this program PROGRAM intdiv; VAR a, b: integer; BEGIN REPEAT write('Input a and b: '); readln(a,b); IF b <> 0 THEN writeln('DIV = ', a DIV b, ' MOD = ', a MOD b); writeln; UNTIL b = 0; END.

Character Data Type Used to store any single character value. Values: 0 to 255, map to specific characters according to the ASCII coding system. Ex. ‘a’, ‘A’, ‘.’, ‘,’, ‘#’,’1’, ‘0’, … Character literals must be enclosed in apostrophes. The blank characters is written as ' '. You don't type apostrophes around character data entered at the terminal. –Blank character is entered by pressing the space bar. Operations: –Assignment: := –Comparison Operators: =, <>, =, –Standard Procedures: read, readln, write, writeln –Standard Functions: pred, succ

Comparing Characters PROGRAM CharComp; VAR firstchar, secondchar: char; BEGIN write('Enter a character, without quotes > '); readln(firstchar); write('Enter another character, without quotes > '); readln(secondchar); IF firstchar = secondchar THEN writeln(firstchar,' is equal to ', secondchar) ELSE IF firstchar < secondchar THEN writeln(firstchar,' is less than ', secondchar) ELSE writeln(firstchar,' is greater than ', secondchar); readln; END.

Character Ordering PROGRAM CharOrd; VAR ch: char; BEGIN write('Enter a character, without quotes > '); readln(ch); writeln('The successor of ',ch,' is ',succ(ch)); writeln('The predecessor of ',ch,' is ',pred(ch)); readln; END.

Boolean Data Type Values: –true (1) & false (0) Operations: –Assignment: := –Comparison Operators: =, <> (the others are not useful) –Logical: AND, NOT, OR (in expressions) –Standard Procedurs: Display with write, writeln (Not read !!!) – ‘false’ and ‘true’ are string literals, not the boolean values false and true

Comparing Booleans PROGRAM BoolOrd; BEGIN writeln('The successor of ',false,' is ',succ(false)); writeln('The predecessor of ',false,' is ',pred(false)); writeln('The successor of ',true,' is ',succ(true)); writeln('The predecessor of ',true,' is ',pred(true)); readln; END.

Boolean Ordering PROGRAM CharOrd; VAR ch: char; BEGIN write('Enter a character, without quotes > '); readln(ch); writeln('The successor of ',ch,' is ',succ(ch)); writeln('The predecessor of ',ch,' is ',pred(ch)); readln; END.

String Data Type Sequence of characters enclosed in (‘) apostrophes (single quotes). –Literals Ex. : ‘Please enter a number > ‘ –Constants, Ex: CONST Border = ‘======‘ –Variable, Ex: VAR InputString : string; Up to 255 characters. For Turbo Pascal only Operations: –Read –Store in memory –Compare –Display

Expressions An expression is: –a single value (variable, constant, literal) E.g. MyVar, MyConst, ‘my literal’, 5 –2 or more values are combined to produce a single value. E.g. MyVar – MyConst + 5

Expressions Occur In constant definitions, but must use only constant arguments, E.g. –Three = 1 + 2; –CONST One = 1; Two = 2; Three = One + Two; On the right hand side of an assignment statement, e.g. –Area := PI * Radius * Radius; –Sum := Sum + NewValue; As arguments to write, writeln, and other standard and user-defined procedures and functions. Elsewhere in different kinds of statements

Operands,Operators,Types Expressions are made up of operands and operators. Each operand in an expression has a type. Operators expect operands of specific type(s) Operators return a result of a specific type. Rules govern: –priority of different types of operators and order of evaluation within the same priority class –what operand types may be combined with a specific operator –what the type of the result is

Expression Types Depends on its operands. Of type Integer only if all its operands are type integer and none of its operators is / (real division). Mixed type expression: ex –Contains both Integer & Real operands. Mixed-type assignment: –Assignment of an expression of one type to a variable of a different type. Ex. VAR r: Real; … r := 4 + 2; –Real expression can’t be assigned to a type Integer variable.

Expression Evaluation Parenthesized subexpressions must be evaluated separately. For nested parenthesized sub-expressions, the innermost sub-expression evaluated first. Left associative rule: –Operators in the same subexpression and at the same precedence level are evaluated left to right. / operator always yields a real value mod and div can only be used with integers

Operand Priority Integer: 1. ( ) : Evaluate from inside out 2.Unary + unary - 3. *, MOD, DIV : Evaluate from left to right 4. Binary + and - : Evaluate from left to right Real: 1. ( ) : Evaluate from inside out 2. Unary + and unary - 3.*, / : Evaluate from left to right 4.+, - : Evaluate from left to right

Rules for Expression Evaluation Example: X * Y * Z + A / B - C * D ==> ((((X * Y) * Z) + (A / B)) - (C * D)) Can also be drawn to show the sequence of evaluation –Nodes are for operators –Lines are for operands X * Y * Z + A / B - C * D * * / + - *

Self-Check What are the results of the following expressions? 22 div 77 div 2222 mod 77 mod 22 What is valid & what is invalid in the following constPI = ; MaxI = 1000; varX, Y : Real; A, B, I : Integer;... I := A mod B;I := A mod Y; X := A / B;I := A / B;X := A / Y; I := B div 0;I := A mod 0; X := PI div Y; X := A mod (A / B); I := (MaxI - 990) div A; I := A mod (MaxI - 990); SEE TURBO PASCAL COMPILATION ERRORS

PROGRAM Types; CONST PI = ; MaxI = 1000; VAR X, Y : Real; A, B, I : Integer; BEGIN I := A mod B; { I := A mod Y;} X := A / B; { I := A / B; } X := A / Y; { I := B div 0; } I := A mod 0; { X := PI div Y; } { X := A mod (A / B); } I := (MaxI - 990) div A; I := A mod (MaxI - 990); END.

Pascal Mathematical Formulas Rules: –Use the * operator to indicate multiplication. –Use parentheses when required to control the order of operator evaluation. –Never write two arithmetic operators in succession; they must be separated by an operand or an open parenthesis. –All assignment statements must be written in a linear form. Parentheses are often needed to separate numerator from the denominator involved in divisions. Examples: Mathematical formulaPascal expression b 2 - 4ac a + b c + d a =  r 2 a  -(b +c) B * B - 4 * A * C (A + B ) / ( C + D ) A := PI * Radius * Radius A * (- ( B + C) )

Simple Statements Assignment Input Operations: Read, Readln Output Operations: Write, Writeln Output formatting with write/writeln

Assignment Statement Assignment statement: –Instruction that stores a value or a computational result in a variable –Used to perform computations –Syntax: := –Example:Area := PI * Radius * Radius; The variable specified by the result is assigned the value of the expression. Previous value of result is destroyed. Assignment symbol ‘:=‘ is not an algebraic equation. Value being assigned must be assignment compatible with the variable receiving it.

Assignment Statement Examples: –Valid assignments X := Y + Z + 2.0; Sum := Sum + Item; NewX := - X; SqYards := MetersToYards * SqMeters; –Invalid assignments Ch := 5;{ Ch is of char type } Ch := Name;{ Name is of string type } Name := True;{ True is a Boolean literal } BoolVar := ‘False’; {‘ False’ is a string literal }