Computer Programming (1) Lecture Notes PDF

Document Details

HonestGeometry

Uploaded by HonestGeometry

Seijoh University

2023

Dr. Khaled Mohammed bin Abdl

Tags

computer programming C++ operators programming fundamentals

Summary

These detailed lecture notes provide a comprehensive overview of operators in C++ programming. The presentation covers various types of operators (arithmetic, assignment, relational, logical, conditional) and their precedence rules. Examples and exercises are included to help solidify understanding.

Full Transcript

Computer Programming (1) Lecture No.5: Operators Lecturer: Dr. Khaled Mohammed bin Abdl ,Department of Information Technology ,College of Applied Science Seyion University Semester 1, 2023...

Computer Programming (1) Lecture No.5: Operators Lecturer: Dr. Khaled Mohammed bin Abdl ,Department of Information Technology ,College of Applied Science Seyion University Semester 1, 2023 1 Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision making operators (logical and relational)  Conditional operator  Precedence and associativity of operators  Common errors Operators 3  Data connectors within expression or equation  Concept related  Operand: data that operator connects and processes  Resultant: answer when operation is completed  Operators types based on their mission  Assignment  Arithmetic: addition, subtraction, modulo division,...etc  Relational: equal to, less than, grater than, …etc  Logical (decision-making): NOT, AND, OR Operators (cont.) 4  Operators types based on number of operands  Unary operators Have only one operand May be prefix or postfix e.g. ! ++ --  Binary operators Have two operands Infix e.g. + && ==  Ternary operators Have three operands e.g. ? : Assignment operators 5  Assignment statement takes the form below varName = expression;  Binary operators  Expression is evaluated and its value is assigned to the variable on the left side  Shorthand notation  varName = varName operator expression; c = c + 3;  varName operator = expression; c += 3; Assignment operators 6 (cont.)  Assignment between objects of the same type is always supported Assignment Sample Explnation Assigns operator expression Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 c=c+7 10 to c -= d -= 4 d=d–4 1 to d *= e *= 5 e=e*5 20 to e /= f /= 3 f=f /3 2 to f %= g %= 9 g=g%9 3 to g Arithmetic Operators 7  All of them are binary operators C++ operation C++ arithmetic Algebraic C++ expression operator expression Addition + f+7 f + 7 Subtraction - p-c p - c MUltiplication * bm or b. m b * m Division / x / y or x ÷ y x / y Modulus % r mod s r % s  Arithmetic expressions appear in straight-line form  Parentheses () are used to maintain priority of manipulation Arithmetic Operators 8 Precedence  Operators in parentheses evaluated first  Nested/embedded parentheses Operators in innermost pair first  Multiplication, division, modulus applied next  Operators applied from left to right  Addition, subtraction applied last  Operators applied from left to right Arithmetic Operators 9 Precedence Example 10  The statement is written in algebra as z = pr % q + w / (x – y)  How can we write and evaluate the previous statement in C++ ? z = p * r % q + w / (x - y); 6 2 3 5 4 1.Example, count.Example, count Increment and Decrement 13 Operators  Unary operators  Adding 1 to or (subtracting 1 from) variable’s value  Increment operator gives the same result of (c=c+1) or (c+=1)  Decrement operator gives the same result of (c=c-1) or (c-=1) Increment and Decrement Operators (cont.) 14 Operator C alled Sample Explanation expression ++ Preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ Postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1.  Pridecrement --b Decrement b by 1, then use the new value of b in the expression in which b resides.  Postdecrement b-- Use the current value of b in the expression in which b resides, decrement b by 1. Examples 15 15 Example # 3 int x = 10; Example # 1 int x = 10 , y; cout

Use Quizgecko on...
Browser
Browser