Fundamentals of Programming CS-110. Lecture 3

Similar documents
GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

JAVA OPERATORS GENERAL

Operators & Expressions

Operators. Java operators are classified into three categories:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators in C. Staff Incharge: S.Sasirekha

Operators in java Operator operands.

Programming in C++ 5. Integral data types

Department of Computer Science

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Unit 3. Operators. School of Science and Technology INTRODUCTION

Lecture 3 Operators MIT AITI

Week 4 Lecture 1. Expressions and Functions

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

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

SECTION II: LANGUAGE BASICS

A flow chart is a graphical or symbolic representation of a process.

Chapter 2: Using Data

Informatics Ingeniería en Electrónica y Automática Industrial

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

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.

Expressions and Precedence. Last updated 12/10/18

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Operators And Expressions

Chapter 3: Operators, Expressions and Type Conversion

Operators and Expressions:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 4: Basic C Operators

Data Types and Variables in C language

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Prof. Navrati Saxena TA: Rochak Sachan

C: How to Program. Week /Mar/05

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

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

LECTURE 3 C++ Basics Part 2

UNIT 3 OPERATORS. [Marks- 12]

Constants and Variables

CS313D: ADVANCED PROGRAMMING LANGUAGE

Visual C# Instructor s Manual Table of Contents

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Homework #3 CS2255 Fall 2012

Learning the Language - V

Information Science 1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Lecture 3. More About C

Expression and Operator

Information Science 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

A First Program - Greeting.cpp

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

C/C++ Programming Lecture 7 Name:

CS Kangmei Yang. Page 1

Prepared by: Shraddha Modi

ECE 122 Engineering Problem Solving with Java

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Variables and Operators 2/20/01 Lecture #

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

Chap 6 - Introduction to HDL (b)

Assignment: 1. (Unit-1 Flowchart and Algorithm)

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

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

Chapter 4 Expression & Operators

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

CMPT 125: Lecture 3 Data and Expressions

Lecture 4 CSE July 1992

Arithmetic Operators. Portability: Printing Numbers

Information Science 1

Expressions and operators

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Work relative to other classes

Midterms Save the Dates!

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

C/C++ Programming for Engineers: Working with Integer Variables

Chapter 4. Operations on Data

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

UNIT- 3 Introduction to C++

Chapter 2, Part III Arithmetic Operators and Decision Making

Computer Programming CS F111

Fundamentals of Programming Session 20

Prefix/Infix/Postfix Notation

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

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

Transcription:

Fundamentals of Programming CS-110 Lecture 3

Operators

Operators Operators are words or symbols that cause a program to do something to variables. OPERATOR TYPES: Type Operators Usage Arithmetic + - * / % a+b a-b a*b a/b a%b Arithmetic assignment Increment and decrement += -= *= /= %= a+=b is the same as a=a+b a-=b a*=b a/=b a%=b ++ - - a++ is the same as a=a+1 a-- is the same as a=a-1 Relational < > <= >= ==!= Logical &&

Manipulating Values Mathematical Operators - Common mathematical operators are available in C++ for manipulating values e.g. addition(+), subtraction(-), multiplication(*), division(/), and modulus (%). C++ has many other operators also which we will study in due course.

Arithmetic Expression Evaluation To evaluate an arithmetic expression two concepts needs to be understood - Operator Precedence Operator precedence controls the order in which operations are performed - Operator Associativity The associativity of an operator specifies the order in which operations of the same precedence are performed

Operator Precedence and Associativity Operators Precedence and Associativity for C++ is following 1. *, /, % Do all multiplications, divisions and remainders from left to right. 2. +, - Do additions and subtractions from left to right.

Evaluating an Expression 6 + 2 * 3 / 6 Three operators are in this expression. However, * and / both have the same precedence and + has lower precedence then these two. * and / will be evaluated first but both have the same precedence level. Therefore, operator associatively will be used here to determine the first to get evaluated i.e. left to right. The left most sub expression will be evaluated followed by the next right one and so on. * will be evaluated first then /

Arithemetic Operator Type Operators Usage Arithmetic + - * / % a+b a-b a*b a/b a%b The Modulus Operator % is known as the Modulus Operator or the Remainder Operator. It calculates the remainder of two variables It can only be used with two ints.. 3%2 =1 5%2=1 6%3=0 8%5=3

Arithmetic Operator Precedence and associativity

Polynomials in C++ In algebra m = In c++ a + b + c + d + e 5 m = (a + b + c + d + e)/5 What happens if I don t put the parenthesis ()?

Evaluate In what order will the expression be evaluated? Algebra C++ z = p r%q + Τ w x y

Evaluate In what order will the expression be evaluated? Run Code

2 nd Degree Polynomial y = ax 2 + bx + c

2 nd Degree Polynomial y = ax 2 + bx + c Run Code

Arithmetic Assignment Operators Type Operators Usage Arithmetic assignment += -= *= /= %= a+=b is the same as a=a+b a-=b a*=b a/=b a%=b

Relational and Equality Operators Standard algebraic equality or relational operator C++ equality or relational operator Sample C++ condition Meaning of C++ condition Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y!= x!= y x is not equal to y (Deitel and Deitel (5 th Ed), fig 2.12)

Logical Operators Type Operators Usage Logical && Logical operators are carried out on statements, e.g. statement1 && statement 2, etc. Logical AND (&&) false && false= false false && true = false true && false= false true && true = true Logical OR ( )

Logical Operators Logical OR ( ) false false = false false true = true true false = true true true = true Logical NOT (!)!false = true!true = false

Expression True or False (6 <= 6) && (5 < 3) False (6 <= 6) (5 < 3) True (5!= 6) True (5 < 3) && (6 <= 6) (5!= 6) True (5 < 3) && ((6 <= 6) (5!= 6)) False!((5 < 3) && ((6 <= 6) (5!= 6))) True

Unary Increment and Decrement Operators Type Operators Usage Increment and decrement ++ - - a++ is the same as a=a+1 a-- is the same as a=a-1 ++a; --a; Postfix a++; a--; Prefix

Precedence of Operators OPERATORS TYPE ++ -- unary * / % multiplicative + - additive < > <= >= relational ==!= equality && = *= /= %= += -= logical assignment

Home Task Precedence of Operators a += b-- + ++d * c % e / f Consider all variables to be equal to 2 and calculate the answer a =?