Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)

Slides:



Advertisements
Similar presentations
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Advertisements

Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 2: Basic Elements of C++
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.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CH Programming An introduction to programming concepts.
Chapter 2 part #4 Operator
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1.  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.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Learners Support Publications Introduction to C++
1 CS161 Introduction to Computer Science Topic #6.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to Computer Programming. MS Sadia Ejaz CIIT ATTOCK Identifiers The identifiers are the names used to represent variable, constants, types,
Chapter 02 (Part II) Introduction to C++ Programming.
Basic Elements Skill Area 313 Part B. Lecture Overview Basic Input/Output Statements Whitespaces Expression Operators IF Statements Logical Operators.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
What Actions Do We Have Part 1
Electrical Engineering Department 1st Year Programming Languages
Chapter 1.2 Introduction to C++ Programming
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Operators and Expressions
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to C++ Programming
Programming Funamental slides
Variables Kingdom of Saudi Arabia
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Summary Two basic concepts: variables and assignments Basic types:
Introduction to Programming – 4 Operators
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Language Lecture 4 C++ Basics – Part II
Operator King Saud University
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)

LECTURE OUTLINES Operators Assignment Operators Arithmetic Operators Compound Assignment Operators Increment/Decrement Operators Logical Operators Relational Operators Conditional Operators Comma Operators Basic Output/Input insertion Introduction of Visual Basic (Civil Engineering Department)

OPERATORS Introduction of Visual Basic (Civil Engineering Department) C ++ integrates operators. Unlike other languages whose operators are mainly keywords, operators in C++ are mostly made of signs that are not part of the alphabet but are available in all keyboards. This makes C++ code shorter and more international, since it relies less on English words, but requires a little of learning effort in the beginning.  Assignment (=)  Arithmetic operators ( +, -, *, /, % )  Compound assignment  Increase and decrease (++, --)  Relational and equality operators ( ==, !=, >, =, <= )  Logical operators ( !, &&, || )  Conditional operator ( ? )  Comma operator (, )

ASSIGNMENT OPERATOR (=) Introduction of Visual Basic (Civil Engineering Department) The assignment operator assigns a value to a variable. a = 5; This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. The most important rule when assigning is the right-to- left rule: The assignment operation always takes place from right to left, and never the other way: a = b;

ASSIGNMENT OPERATOR (=) Introduction of Visual Basic (Civil Engineering Department) #include int main () { int a, b; cout << "a:"; a = 10; cout << a; b = 4; cout << " b:"; a = b; cout << b; b = 7; return 0; } a:4 b:7

ARITHMETIC OPERATORS ( +, -, *, /, % ) Introduction of Visual Basic (Civil Engineering Department) The five arithmetical operations supported by the C++ language are: + addition - subtraction * multiplication / division % modulo Modulo is the operation that gives the remainder of a division of two values. For example a = 11 % 3;

COMPOUND ASSIGNMENT (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) Introduction of Visual Basic (Civil Engineering Department) When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators: Expression is equivalent to value += increase; value = value + increase; a -= 5; a = a - 5; a /= b; a = a / b; price *= units + 1;price = price * (units + 1);

COMPOUND ASSIGNMENT (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) Introduction of Visual Basic (Civil Engineering Department) #include int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; }

INCREASE AND DECREASE (++, --) Introduction of Visual Basic (Civil Engineering Department) The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus: 1c++; 2c+=1; 3 c=c+1;

RELATIONAL AND EQUALITY OPERATORS ( ==, !=, >, =, <= ) Introduction of Visual Basic (Civil Engineering Department) In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. = Equal to !=Not equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to

LOGICAL OPERATORS ( !, &&, || ) Introduction of Visual Basic (Civil Engineering Department) The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. && OPERATOR aba && b Truetruetrue Truefalsefalse Falsetruefalse Falsefalsefalse

LOGICAL OPERATORS ( !, &&, || ) Introduction of Visual Basic (Civil Engineering Department) The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b: || OPERATOR aba || b Truetruetrue Truefalsetrue Falsetruetrue Falsefalsefalse

CONDITIONAL OPERATOR ( ? ) Introduction of Visual Basic (Civil Engineering Department) The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: condition ? result1 : result2 If condition is true the expression will return result1, if it is not it will return result2.

CONDITIONAL OPERATOR ( ? ) Introduction of Visual Basic (Civil Engineering Department) 1)7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5 2)7==5+2 ? 4 : 3 // returns 4, since 7 is equal to )5>3 ? a : b // returns the value of a, since 5 is greater than 3. 4)a>b ? a : b // returns whichever is greater, a or b.

CONDITIONAL OPERATOR ( ? ) Introduction of Visual Basic (Civil Engineering Department) #include int main () { int a,b,c; a=2; b=7; c = (a>b) ? a : b; cout << c; return 0; } (output= 7)

COMMA OPERATOR (, ) Introduction of Visual Basic (Civil Engineering Department) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered. For example, the following code: a=(b=3,b+2); Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.

BASIC INPUT/OUTPUT Introduction of Visual Basic (Civil Engineering Department) C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen or the keyboard. A stream is an object where a program can either insert or extract characters to/from it. The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. Standard Output (cout) cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs).

BASIC INPUT/OUTPUT Introduction of Visual Basic (Civil Engineering Department) 1 cout << "Output sentence"; // prints Output sentence on screen 2 cout << 120; // prints number 120 on screen 3 cout << x; // prints the content of x on screen 4 cout << "Hello"; // prints Hello 5 cout << Hello; // prints the content of Hello variable The insertion operator (<<) may be used more than once in a single statement: cout << "Hello, " << "I am " << "a C++ statement";

BASIC INPUT/OUTPUT Introduction of Visual Basic (Civil Engineering Department) Standard Input (cin) standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. int age; cin >> age;

BASIC INPUT/OUTPUT Introduction of Visual Basic (Civil Engineering Department) #include void main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } Please enter an integer value: 702 The value you entered is 702 and its double is 1404.

BASIC INPUT/OUTPUT Introduction of Visual Basic (Civil Engineering Department) #include void main () { float price; int quantity; cout << "Enter price: "; cin>> price; cout << "Enter quantity: "; cin>> quantity; cout << "Total price: " << price*quantity << endl; } Enter price: Enter quantity: 7 Total price: