Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Chapter 2: Basic Elements of C++
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
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.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
OPERATORS.
Object Oriented Programming with C++ Diploma in Computer System Design.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Today Variable declaration Mathematical Operators Input and Output Lab
Arithmetic Expressions
Lecture 3 Java Operators.
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Arithmetic & other operations
C Short Overview Lembit Jürimägi.
Operators and Expressions
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
A First Book of ANSI C Fourth Edition
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Assignment Operators Topics Increment and Decrement Operators
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Assignment Operators Topics Increment and Decrement Operators
DATA TYPES There are four basic data types associated with variables:
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Programming Fundamental-1
Presentation transcript:

Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh

Increment & Decrement Operators –Increment operator, ++ x++  is equivalent to x = x + 1 –Decrement operator, -- y--  is equivalent to y = y – 1

Two Options of Increment & Decrement Operators Post-Increment  x++ –Uses current value of variable, THEN increments it Pre-Increment  ++x –Increments variable first, THEN uses new value

Post/Pre-Increment in Action Post-Increment in Expressions: int n = 2; int y; y = 2 * (n++); cout << y << endl; cout << n << endl; –This code segment produces the output: 4 3 Pre-Increment in Expressions: int n = 2, int y; y = 2 * (++n); cout << y << endl; cout << n << endl; –This code segment produces the output: 6 3

Post/Pre-Decrement in Action Post-Decrement in Expressions: int n = 2; int y; y = 2 * (n--); cout << y << endl; cout << n << endl; –This code segment produces the output: 4 1 Pre-Decrement in Expressions: int n = 2; int y; y = 2 * (--n); cout << y << endl; cout << n << endl; –This code segment produces the output: 2 1

Assignment Operators Operator:Example:Meaning: = x = 5 ;x = 5 ; += x += 5 ;x = x + 5 ; –= x –= 5 ;x = x – 5 ; /= x /= 5 ;x = x / 5 ; *= x *= 5 ;x = x * 5 ; %= x %= 5; x = x % 5 ;

Assignment Operators Example of assignment operators: int a = 4, b = 2, c = 36 ; a += b ; /* This adds b to a, a=?? */ [ Answer: a = a + b, so a = or a = 6 ] c /= a + b ; /* What is value of c now ?? */ [ Answer: c = c / (a + b), and a = 6 now, so c = 36 / (6 + 2), so c = 36 / 8 or c = 4 ]

Explicit type conversion –Programmer specifies conversion with cast operator ( ) –More typical use –Forcing a Type Change Example: int x=1, y=2; double result1 = x/y; // 0.0 double result2 = double(x)/y; // 0.5 double result3 = x/double(y); // 0.5 double result4 = double(x)/double(y);// 0.5 double result5 = double(x/y); // 0.0 int result6 = int(result4*100);// 50

Relational Operators Operator:Meaning: <Less Than >Greater Than <=Less Than or Equal To >=Greater Than or Equal To ==Exactly Equal To !=Not Equal To

Assignment Conversions Example : int m, n; double xx; m = 7; n = 2.5; // 2.5 converted to 2 and assigned to n xx = m/n; //7/2=3 converted to 3.0 and assigned to xx n = xx+m/2; Start from left to right with higher precedence // m/2=3 : integer division // xx+m/2 : double addition because xx is double // convert result of m/2 to double (i.e. 3.0) // xx+m/2=6.0 // convert result of xx+m/2 to int (i.e. 6) //because n is int Example : a = (b = ( c= (d = (e = 4)))); Start from right to left

Logical Operators Truth Tables

Logical Operators !  (not) Ex: a != b is true if a and b are not equal &&  (and) Ex: 5 4 is true, but 5>6 && 7>4 is not true (i.e., false) ||  (or) Ex: 5>6 || 7>4 is true 5<6 || 7<4 is also true

Exponentiation and Square root Operations Exponentiation is not written as x^2 -C/C++ does not have an exponentiation operator. You can use the math function pow (a, b) which raises a to the b power. Example: int a= 2, b=5; cout<<pow(a,b); // result 2^5 = 32 Square Root is not written as √ x double sq= sqrt(36); cout<<sq<<"\n"; -You must put a #include in your source code

Character Given: ‘A’ = 65 in decimal = 41 hex ‘A’ = 97 in decimal = 61 in hex Given char A= ‘F'; cout<< (char) ('A'+3); Answer: ‘D’ Given char A= ‘F'; cout<< ('A'+3); Answer: 68 Given char A= ‘B'; cout<< (char)(A+3); Answer: ‘E’ Given char A= ‘B'; cout<< (A+3); Answer: 69

define#  The purposes of define  symbolic constants  The directives #define MAX_SCORE 100 Causes all occurrences of MAX_SCORE in the C/C++ program to be replaced by 100 BEFORE the program is compiled. It is a convention (متعارف عليه) to name a symbolic constant with upper case letters.

Example use of #define /* To convert length in inches to cm */ #include #define RATIO 2.54 int main() { float inches, cm; printf(“Enter length in inches: ”); scanf(“%f”, &inches); cm = RATIO*inches; printf(“approximately %f in cm\n”, cm); return 0; }

C/C++ Standard Header Files you may want to use Standard Headers you should know about: –stdio.h – file and console IO: printf, open, close, read, write, scanf, etc. -Iostream.h – I/O : cin, cout –math.h – math functions: ceil, exp, floor, sqrt, etc. –string.h - string and byte manipulation: strlen, strcpy, strcat, memcpy, memset, etc.