ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Similar documents
Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Programming in C++ 5. Integral data types

Operators. Lecture 3 COP 3014 Spring January 16, 2018

UNIT- 3 Introduction to C++

LECTURE 3 C++ Basics Part 2

Lecture 3 Tao Wang 1

Fixed-Point Math and Other Optimizations

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

CS Programming I: Primitives and Expressions

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

CS102: Variables and Expressions

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP Primitive and Class Types. Yi Hong May 14, 2015

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

ANSI C Programming Simple Programs

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Expressions and Casting

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

ISA 563 : Fundamentals of Systems Programming

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Basics of Java Programming

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Operators and Expressions:

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer System and programming in C

CIS133J. Working with Numbers in Java

C introduction: part 1

Information Science 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Full file at

Visual C# Instructor s Manual Table of Contents

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Values, Variables, Types & Arithmetic Expressions. Agenda

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Programming in C++ 6. Floating point data types

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

DEPARTMENT OF MATHS, MJ COLLEGE

ME 461 C review Session Fall 2009 S. Keres

CS112 Lecture: Primitive Types, Operators, Strings

Information Science 1

Operators. Java operators are classified into three categories:

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Variables and Operators 2/20/01 Lecture #

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Chapter 2: Using Data

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 4: Basic C Operators

CS Programming In C

CEN 414 Java Programming

Midterms Save the Dates!

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators & Expressions

1.3b Type Conversion

Lecture Set 4: More About Methods and More About Operators

SECTION II: LANGUAGE BASICS

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Objectives. In this chapter, you will:

Scientific Programming in C VI. Common errors

CS201 Some Important Definitions

Programming for Engineers: Operators, Expressions, and Statem

Operators in C. Staff Incharge: S.Sasirekha

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture Set 4: More About Methods and More About Operators

Chapter 3: Operators, Expressions and Type Conversion

Expression Evaluation and Control Flow. Outline

Unit 3. Operators. School of Science and Technology INTRODUCTION

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

The C++ Language. Arizona State University 1

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Computers Programming Course 5. Iulian Năstac

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Data types, variables, constants

Chapter 3. Fundamental Data Types

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

A First Program - Greeting.cpp

Programming. C++ Basics

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Computer Programming CS F111

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CS110: PROGRAMMING LANGUAGE I

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Transcription:

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I

Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision arithmetic Type casting

Assignment statements An assignment statement gives a value to a variable. The left-hand side of an assignment statement must consist of only one variable. This basically means we can t have an implicit assignment, e.g. y+2 = 5; 7*x+3=10 General form of variable assignment is: variable = expression;

Assignment statements As we just saw, a general variable assignment is of the form: variable = expression;; This means that we should first evaluate the expression on the right, and then assign it to the variable on the left. The variable on the left of the assignment can appear on the right side of the assignment.

Example of variable on both sides of assignment We will see later that having a variable on both sides of an assignment statement is useful in control structures like for and while loops. If we wish to increment the value of a variable x, we have two options: int x=5; int y; y = x+1; We may not want to allocate a new variable just for this. Also may make the code more complicated than needed.

Example of variable on both sides of assignment Instead we can do the following: int x=5; x = x+1; In a mathematical context, this would be impossible. x =x + 1; has no solution! However in C++, this is valid. It means use the current value of x, add 1 to it, and then store the value back in the variable x. This updates the value of x with a value based on its current value.

Back to the drawer representation of variables For example, x=x+1 would take the value of x out of its drawer, and add 1 to it, and then put it back into its drawer again. 3 1 2 tmp deg x

Parameters as variables The parameters of a function can be thought of as variables. We already saw that functions must be declared before they are used, along with their parameters. These parameters are of a certain type. In order to make calling functions easier for the programmer, we can use different names for the variables that are passed as parameters, or just pass values.

Example Calling Functions different ways The parameters of a function are like variables. Consider a function that converts a height in feet and inches to centimetres. The prototype (declaration) of the function could be something like: double convertheight(int feet, int inches);

Example And the definition of the function could be: /* convertheight ***************************************** * * @params: feet - number of feet in person s height @pre >= 0 * inches number of inches in person s height @pre>= 0 * * @returns: number of centimetres in height. **************************************************** ****/ double convertheight(int feet, int inches) { int numinches; // number of inches in height double numcentimetres; // number of cm. in height numinches = 12*feet+ inches; numcentimetres = numinches*2.54; return numcentimetres; }

Example Calling the function We can call the function in different ways Assume that a person s height is 5 feet, 9 inches. Then to convert this height to centimetres, we could call convertheight in at least four different ways: 1. Call with constant values: double numcentimetres; numcentimetres = convertheight(5,9); This works, but may be tricky if we want to code many calls to convertheight.

Example Calling the Function 2. call with variable names (same as prototype): Notice here that the variables have the same name as in the definition of the function. int feet = 5; int inches = 9; double numcentimetres; numcentimetres = convertheight(feet,inches);

Example Calling the Function 3. Call with variable names (different than prototype): int numfeet = 5; int numinches = 9; double numcentimetres; numcentimetres=convertheight(numfeet,numinches );

Example Calling the Function 4. Call with variable names in the wrong order! This is wrong, but this shows a possible error that can happen if the parameters you call the function with are not descriptive: int feet = 5; int inches = 9; double numcentimetres; numcentimetres = convertheight(inches,feet);

Symbolic Constants A symbolic constant is a value that does not change and cannot change throughout a program. General form: const type VARIABLENAME = expression; Examples (from Lecture Notes): const int MONTHS = 12; const double PI = 4 * atan(1.0); const char YES = 'Y'; const int LONG_CONSTANT_NAME = 17; Good to use symbolic constants for values that are used many times and do not change during the program.

Expressions We have seen in variable declaration statements, where the value of the variable is initialized that the general form of the declaration is: type var = expression; We haven t yet explained what an expression is. They are somewhat similar to math expressions, but more general.

Expressions The definition of an expression is (from the Lecture notes): A combination of variables, constants, operators and functions which is progressively evaluated an operation at a time until it is reduced to a final value. An expression is always reduced to a single value (very important). It does not include C++ keywords!

Arithmetic Expressions An expression which uses arithmetic operators. There are different operators based on whether we are using integers or doubles. Operators can be either unary or binary. A unary operator only takes one argument. A binary operator takes two arguments. If we are using doubles, then the usual arithmetic operators that you have learned in math are valid. These are: + - * /

Arithmetic Expressions The operators we are allowed to use depend on the data type. For arithmetic expressions there are two data types that we consider in this course, int and double. For real numbers, we have the usual operators: + - * / + and can either be unary or binary operators. This is because we can have positive and negative numbers, e.g. +98.6 or -25. The unary + sign is strictly not necessary, however is included for consistency with the sign.

Arithmetic Expressions For integers, we have the usual operators, both unary and binary. We also have an extra operator: the % operator. This is the modulus operator. This is like the case for calculating times, e.g. 9 hours after 5 p.m. is 2 a.m. (5+9) % 12 = 2

Integer operators / and % Look at example in Lecture Slides on / and % operators. The / and % operators for int data types are like the results of long division: a / b is the quotient of a and b (always an integer) a % b is the remainder when a is divided by b (also always an integer) Remember that there is no double % operator!

Computer Arithmetic There are two types of number variables in C++ that we use in this course. These are int and double. The compiler knows how to do either integer arithmetic (only adding, multiplying... integers together), or floating point arithmetic (only adding, multiplying... single or double precision numbers together) If we wish to mix up operations with integers and doubles for example, then the compiler automatically converts from one type to the other.

Data type conversions Whenever there is at least one double in an arithmetic expression, all variables in that expression are converted to double precision. Converting from an int to double can be called upcasting, while converting from a double to int can be called downcasting (truncation). Converting from a double to an integer is not rounding! So for example, int x = 5.6; will set the value of x to 5 and not 6.

Type casting Sometimes we wish to force the compiler to switch from one type to the other (e.g. when mixing integers and floating points in an expression). This is called type casting. For example, let us say that we wish to automatically truncate the answer of a double precision floating point number. This can be done with the following piece of code, e.g.: int wholepart; float x = 3.6; wholepart = (int) x;

Example of automatic type conversion See the quadratic example in the Lecture notes.

Composite Assignments Shorthand for assignments like x = x+1; or x =x*9; See Lecture Notes