Introduction to Programming – 4 Operators

Slides:



Advertisements
Similar presentations
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Advertisements

CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
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.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Operators & Expressions
C++ Programming Language Lecture 4 C++ Basics – Part II
CSE 220 – C Programming Expressions.
Chapter 4 – C Program Control
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
Computing Fundamentals
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Variable Declaration, Data types, Expressions
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Expressions and Assignment Statements
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 4 - Program Control
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions and Assignment Statements
Associativity and Prescedence
Expressions.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
Operator and Expression
OPERATORS in C Programming
Operator King Saud University
Expressions and Assignment Statements
OPERATORS in C Programming
Presentation transcript:

Introduction to Programming – 4 Operators Dr. Khizar Hayat Associate Prof. of Computer Science

Operators Arithmetic operators Assignment operator, i.e. = Compound assignment or arithmetic assignment operators Increment and Decrement operators Pre-increment Post-increment Relational operators Logical Operators Bit-wise logical operators

Arithmetic Operators I- Unary, e.g. -5, +7 II- Binary Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulo division (%) For example 11%3=2 or 33%5=3 Using ‘%’ for non-integer operands is a compilation error

Assignment operator The operator ‘=‘ assigns a value to a variable e.g. the statement i=7; would assign a value 7 to variable i. in this case is the i Lvalue (left value) and 7 is the Rvalue (right value) Note that: Lvalue must always be a single variable. The Rvalue may be a constant or a variable or an expression involving multiple variables and constants. The assignment always takes place from right to left (right-to-left rule), e.g. x=y; would assign the value of y to x; the value of y will be unchanged.

Assignment operator a=4 b=4 Output? #include<iostream> using namespace std; int main() { int a,b; a=10; b=4; a=b; cout<<“a="<<a<<endl; cout<<“b="<<b<<endl; return(0); } Output? a=4 b=4

Compound assignment May be arithmetic or bit-wise logical Arithmetic: +=, e.g. i+=3; means i=i+3; -=, e.g. i-=1; means i=i-1; *=, e.g. i*=7; means i=i*7; /=, e.g. i/=2; means i=i/2; %= , e.g. i%=10; means i=i%10; Compound bitwise Logical work the same way but with logical bitwise operators, e.g. i|=77;

Increment/decrement operators The unary Increment operator ++ count++; is the same as, count=count+1; or count+=1; The unary decrement operator -- count--; count=count-1; Count-=1;

Pre-increment and Post-increment When the operator ++ comes before the variable then it is pre-increment. , e.g. ++i; means first increase the value of i by 1 and then execute the statement When the operator ++ comes after the variable then it is post-increment. , e.g. i++; means first execute the statement and then increase the value of i by 1. Same reasoning for pre- and post decrements, i.e. --j; and j--;

Example: Pre-increment and Post-increment #include<iostream> using namespace std; int main() { int c; c=5; cout<<“c="<<c<<endl; cout<<“c="<<c++<<endl; cout<<“c="<<++c<<endl; cout<<c<<“\t"<<c++<<“\t"<<++c<<“\t"<<c++<<endl; //just for fun return(0); }

Relational Operators The relational operators evaluate to either true (1) or false (0) In C++ the relational operators are: == Equal to != Not equal to > Greater than < less than >= Greater than or equal to <= less than or equal to Never confuse == (equal to) with = (assignment or gets)

Examples What would the following expression evaluate to? (7==5) //false 0 (5>4) //true 1 (3!=2) (6>=6)

Examples What would the following expression evaluate to? Let int a=2,b=3,c=6; (a==5) //false (a*b>=c) //true (b+4>a*c) ((b=2)==a)

Logical Operators The logical operators also evaluate to either: true (1) or false (0) In C++ the Logical operators are: ! NOT Unary draw truth table && AND Binary draw truth table || OR Binary draw truth table Bitwise logical operators are: & bitwise AND | bitwise OR ^ bitwise XOR << left shift >> right shift

The ternary operator (? :) if-then-else Also called the conditional operator Use: condition ? result1 : result1 (if true) (if false) Examples: 7==5?4:3; //return 3 7==5+2?4:3; //return 4 a>b?a:b;/*return whichever greater,a or b */

Example ternary operator #include<iostream> using namespace std; int main() { int a,b,c; a=2; b=7; c= (a>b)?a:b; cout<<“c = "<<c<<endl; return(0); }