CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Unit 12 INTRODUCTION TO ALGEBRA. 2 ALGEBRAIC EXPRESSIONS An algebraic expression is a word statement put into mathematical form by using variables, arithmetic.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Data Types, Expressions and Functions (part I)
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Chapter 3 Arithmatic Operations 3.1 Operators An operator is a symbol or world that represents some operation that is performed on one or more data values.
Recap……Last Time [Variables, Data Types and Constants]
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 3 Assignment and Interactive Input.
Chapter 2 Assignment and Interactive Input
Chapter 7: Expressions and Assignment Statements
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.
Arithmetic & other operations
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
Chapter 2: Basic Elements of Java
A First Book of ANSI C Fourth Edition
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: Java Fundamentals
Data Types and Expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

C ONTENTS Arithmetic Expressions Operators Addition, subtraction,multiplication, division, modulus Binary and unary operator Negation operator 2 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

3 Variables and constants of integral and floating point types can be combined into expressions using arithmetic operators. In C++, we have to represent the algebraic expression by using the valid syntax. A RITHMETIC EXPRESSION CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

4 Is the most basic C++ statement Use to store the value of an expression in a variable Use Assignment Operator (=) Syntax: variable = Expression can be : i.a constant ii.another variable iii.an arithmetic expression iv.a function Assignment statement CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

5 Examples: length = oldLength; width = 50; area = length * width; moredata = true;//a boolean variable x = total ( ); Assignment statement CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

A RITHMETIC OPERATOR Generally, there are three types of operator: Unary Binary Ternary These term reflect the number of operands an operator requires. Unary operators only require a single operand. Ex: -7 Represent the value negative 7 The literal 7 is preceded by the minus sign, is called the negation operator. Since it only requires one operand, it is a unary operator 6

A RITHMETIC OPERATOR CONT … Binary operator work with two operands Ex: total = cost + tax; Average = total / 3; Ternary operator work with three operands Ex: total = num1 + num2 + num3; 7

8 Assignment Operation Variations of Assignment Operation : We can have assignment statement like this : sum = sum + 10; This statement can be written using the following shortcut assignment operators : += -= /=%=*= Hence, the equivalent “ sum = sum + 10 ”, would be : sum += 10; CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

9 A RITHMETIC EXPRESSION Example : ExpressionEquivalent to price = price * rate;price *= rate; count = count + 3count += 3 price *= rate + 1price = price * (rate + 1) but not price = price * rate + 1 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

10 Assignment Operation For a variable to increase or decrease by 1, C++ provides two unary operators : (++) : increment operator (--): decrement operator o Increment & Decrement Operators: ExpressionEquivalent to i = i + 1i++ (postfix increment operator) If x = 5; and y = x++; After the second statement y is 5 and x is 6 i = i - 1i-- CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Arithmetic Operators Defined in C++Example Addition+  = 23.9, or =23 Subtraction-  50 – 10 =40, or = 40.4 Multiplication*  10*3=30, or 10.5*3= 31.5 Division/  10/3 = 3, or 10.0/3= Modulus (remainder) %  10%3=1  Never use the modulus with floating- point values. Operators

12 Mathematical Library Functions Header file #include used in the following form : function_name(argument) Example : sqrt(x) //square root of x pow(a,2)//a 2 fabs(2.3 * 4.6)//absolute value |x| tan(x)//tangent of x floor(x)//largest integer <= x More reference at: CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Arithmetic Operations Examples #include void main (){ int number1, number2; int sum, different, product, quotient, remainder ; number1 = 8; number2 = 4; sum = number1 + number2; different = number1 - number2; product = number1 * number2; quotient = number1 / number2; remainder = number1 % number2; cout<<"sum : " <<sum<<endl; cout<<"different: " <<different<<endl; cout<<"product : " <<product<<endl; cout<<"quotient : " <<quotient<<endl; cout<<"remainder : " <<remainder<<endl; } 13 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Output:

Integer Division IF two integers are divided, the result will be an integer number. Any fraction will be truncated. int / int  int Example: int a = 5, b = 2; float c; c = a / b; //the new value of c is 2 cout << a / b; //2 will be displayed 14 Arithmetic Operations CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

When more than one operator will be executed to determine the result, C++ executes the arithmetic operators in the following order: Operator (s)OperationPrecedence ( )parentheses Evaluated first, inside-out if nested or left to right if same level *,/,% Multiply, Divide, Modulus Evaluated second, left to right +, -Add, SubtractEvaluated last, left-to-right Precedence & Associativity

Example: a) 3 * 7 – * 5 / means ( 3 * 7 ) – 6 + ( ( 2 * 5 ) / 4 ) + 6 = 21 – 6 + ( 10 / 4 ) + 6( evaluate * ) = 21 – ( evaluate / ) = 21 – 6 + 8( evaluate + ) = ( evaluate +) = 23( evaluate += result) Precedence & Associativity

b)3 + 4 * 5 means 3 + ( 4 * 5 ) = ( evaluate * ) = 23( evaluate + ) c)2 * 3 / 2(evaluate operators from left to right) = 6 / 2(evaluate * ) = 3(evaluate / ) Precedence & Associativity

Write a program that does the following : 1. Find the sum, subtract and average of two integers 18 Exercise 1 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Try to write a program : Convert Length Write a program that takes as input given lengths expressed in meters and millimeters. The program should then convert and output the length in centimeters. Assume that the given lengths in meters and milimeters are integers. 19 Exercise 2 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

20 Translate the following flow chart into source code: Begin End Read total Read count Display average average = total / count Exercise 3 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

21 Given the following declaration: int a, b, c, v1, v2; a = 8; c = b = 3; c = c – 1; v1 = a / b; v2 = a / c; What is the final content of a, b, c, v1 and v2? Exercise 4 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING