Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.

Slides:



Advertisements
Similar presentations
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
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.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CH Programming An introduction to programming concepts.
 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.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Completing the Basic (L06)
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Programming: Basic Elements of C++.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
CSC 107 – Programming For Science. The Week’s Goal.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
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.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.
Solution of Mid. Term 2009 Consider the following C++ declarations and assignments. int i, j, k ; float x, y ; char c ; bool test ; i = 35 ; j= 5 ; k =
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
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.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Chapter 7: Expressions and Assignment Statements
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 7: Expressions and Assignment Statements
Arithmetic operations & assignment statement
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (2) CSC 111.
Introduction to C++ Programming
Programming Funamental slides
C Operators, Operands, Expressions & Statements
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Introduction to Programming – 4 Operators
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Relational Operators.
CS 101 First Exam Review.
Operator and Expression
Operator King Saud University
HNDIT11034 More Operators.
Presentation transcript:

Operator

Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation always takes place from right to left and never at the inverse. a = b;

Example int a, b; // a:? b:? a = 10; // a:10 b:? b = 4; // a:10 b:4 a = b; // a:4 b:4 b = 7; // a:4 b:7

Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

Example value += increase; is equivalent to value = value + increase; a -= 5; is equivalent to a = a - 5; a /= b; is equivalent to a = a / b; price *= units + 1; is equivalent to price = price * (units + 1);

Increase and decrease. Another example of saving language when writing code are the increase operator (++) and the decrease operator (--). They increase or reduce by 1 the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus: a++; a+=1; a=a+1; are all equivalent in its functionality: the three increase by 1 the value of a.

Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the language are: +addition -subtraction *multiplication /division %module

Relational Operator ( ==, !=, >, =, <= )

Relational operators ==Equal !=Different >Greater than <Less than >=Greater or equal than <=Less or equal than

Example (7 == 5)would return false. (5 > 4)would return true. (3 != 2)would return true. (6 >= 6)would return true. (5 < 5)would return false.

Example 2 Suppose that a=2, b=3 and c=6: (a == 5)would return false. (a*b >= c)would return true since (2*3 >= 6) is it. (b+4 > a*c)would return false since (3+4 > 2*6) is it. ((b=2) == a)would return true.

Logical Operator ( !, &&, || ).

NOT Logic operators !(5 == 5)returns false because the expression at its right (5 == 5) would be true. !(6 <= 4)returns true because (6 <= 4) would be false. !truereturns false. !falsereturns true.

Logic operators && and || First Operand a Second Operand b result a && b result a || b True False True FalseTrueFalseTrue False

Example ( (5 == 5) && (3 > 6) ) returns false ( true && false ). ( (5 == 5) || (3 > 6)) returns true ( true || false ).

Conditional operator ( ? ) The conditional operator evaluates an expression and returns a different value according to the evaluated expression, depending on whether it is true or false. Its format is: condition ? result1 : result2 if condition is true the expression will return result1, if not it will return result2.

Example 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5. 7==5+2 ? 4 : 3 returns 4 since 7 is equal to >3 ? a : b returns a, since 5 is greater than 3. a>b ? a : b returns the greater one, a or b.

/* Program COMPARES.C */ int main() /* This file will illustrate logical compares */ { int x = 11, y = 11, z = 11; char a = 40, b = 40, c = 40; float r = , s = , t = ; /* First group of compare statements */ if (x == y) z = -13; /* This will set z = -13 */ if (x > z) a = 'A'; /* This will set a = 65 */ if (!(x > z)) a = 'B'; /* This will change nothing */ if (b <= c) r = 0.0; /* This will set r = 0.0 */ if (r != s) t = c/2; /* This will set t = 20 */ /* Second group of compare statements */ if (x = (r != s)) z = 1000; /* This will set x = some positive number and z = 1000 */ if (x = y) z = 222; /* This sets x = y, and z = 222 */ if (x != 0) z = 333; /* This sets z = 333 */ if (x) z = 444; /* This sets z = 444 */ /* Third group of compare statements */ x = y = z = 77; if ((x == y) && (x == 77)) z = 33; /* This sets z = 33 */ if ((x > y) || (z > 12)) z = 22; /* This sets z = 22 */ if (x && y && z) z = 11; /* This sets z = 11 */ if ((x = 1) && (y = 2) && (z = 3)) r = 12.00; /* This sets x = 1, y = 2, z = 3, r = */ if ((x == 2) && (y = 3) && (z = 4)) r = 14.56; /* This doesn't change anything */ /* Fourth group of compares */ if (x == x); z = ; /* z always gets changed */ if (x != x) z = ; /* Nothing gets changed */ if (x = 0) z = ; /* This sets x = 0, z is unchanged */ return 0; } /* Result of execution (No output from this program.) */

// Filename: RELAT1ST.CPP // Prints the results of several relational operations #include void main() { int high = 45; int low = 10; int middle = 25; int answer; answer = high > low; cout low is " << answer << endl; answer = low > high; cout high is " << answer << endl; answer = middle == middle; cout << "Middle == middle is " << answer << endl; answer = high >= middle; cout = middle is " << answer << endl; answer = middle <= low; cout << "Middle <= low is " << answer << endl; answer = 0 == 0; cout << "Bonus relation: 0 == 0 is " << answer << endl; return; }

Output High > low is 1 Low > high is 0 Middle == middle is 1 High >= middle is 1 Middle <= low is 0 Bonus relation: 0 == 0 is 1