Www.hndit.com HNDIT11034 More 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

Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Compound Operators 03/07/11. More Operators, First Section 4.4.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
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#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Overview Go over parts of quiz? Another iteration structure for loop.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
Chapter#3 Part1 Structured Program Development in C++
The Assignment operator tMyn1 The Assignment Operator The result of a calculation can be stored in a variable using the assignment operator =. Because.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 CS161 Introduction to Computer Science Topic #6.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
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.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Repetition statements
Decision making If.. else statement.
Operators And Expressions
Section 3 Review Mr. Crone.
Computing Fundamentals
Chapter 2 Assignment and Interactive Input
Variable Declaration, Data types, Expressions
Chapter 5: Loops and Files.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Operators and Expressions
Control Statement Examples
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Compound Assignment Operators in C++
Introduction to Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
Variables Kingdom of Saudi Arabia
Chapter 7 Additional Control Structures
Chapter-3 Operators.
3 Control Statements:.
Chapter 7 Conditional Statements
Decision making If statement.
Selection Control Structure
Repetition Statements (Loops) - 2
CS 101 First Exam Review.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

www.hndit.com HNDIT11034 More Operators

Increment and Decrement Operators www.hndit.com Increment and Decrement Operators Prefix and Postfix notations The increment ope rator, ++, can be used in two ways: As a prefix, in which the operator follows the variable. ++n ; As a postfix, in which the operator follows the variable. n++;

The following code segment differentiates the two notations: var1 =10; www.hndit.com The following code segment differentiates the two notations: var1 =10; var2 = ++var1; The equivalent of this code is: var1 = 10; var1 = var1 + 1; var2 = var1; In this case, both var1 and var2 are set to 11.

However, if the code is written as: www.hndit.com However, if the code is written as: var1 = 10; var2 = var1++; the equivalent code will be: var1 =10; var2 = var1; var1 = var1 +1; Here,var1 is set to 11,but the value of var2 is 10.

www.hndit.com The decrement operator can also be used in both the prefix and postfix forms.

#include <iostream.h> void main() { int j; // declare j as int www.hndit.com #include <iostream.h> void main() { int j; // declare j as int j=1; // initialize j to 1 cout << “j=“ << j << ‘\n’; j++; //increment j j--; //decrement j }

Arithmetic Assignments www.hndit.com Longer form x = x + y x = x - y x = x * y x = x / y x = x % y Short hand x += y x -= y x *= y x /= y x %= y

Conditional Operators www.hndit.com Consider the following statements that find the maximum of two given numbers: if (num1 > num2) { max = num1; } else max= num2;

above program code can be modified using the conditional operator as: www.hndit.com In the above program code, we are determining whether num1 is greater than num2. The variable, max is assigned the value, num1, if the expression, (num1 > num2), evaluates to true, and the value, num2,if the expression evaluates to false. The above program code can be modified using the conditional operator as: max = (num1 > num2)? num1 : num2;

cout << “Please enter a score for the student “; www.hndit.com The?: operator is called the ternary operator since it has three operands. The following example calculates the grade of a student based on his scores. int score =0; cout << “Please enter a score for the student “; cin >> score; char grade = (score > 75)? ‘A’:’B’;

cout << ( score > 50 ? “PASSED”: “FAILED”) << endl; www.hndit.com The following code will display either “PASSED” or “FAILED”, depending on the value in the variable, score. cout << ( score > 50 ? “PASSED”: “FAILED”) << endl;

Bitwise Operators www.hndit.com

www.hndit.com Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A  = 1100 0011

www.hndit.com

cout << "Line 1 - Value of c is : " << c << endl ; int a = 60; // 60 = 0011 1100 int b = 13; // 13 = 0000 1101 int c = 0; c = a & b; // 12 = 0000 1100 cout << "Line 1 - Value of c is : " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - Value of c is: " << c << endl ; c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - Value of c is: " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - Value of c is: " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - Value of c is: " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - Value of c is: " << c << endl ; www.hndit.com

Q & A www.hndit.com